-
-
Notifications
You must be signed in to change notification settings - Fork 181
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add exercise yacht #972
Add exercise yacht #972
Changes from 4 commits
fe1d62c
8170c68
c96a427
34756a0
37f12f5
ed62aaa
4fb8565
6f57054
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# Instructions | ||
|
||
The dice game [Yacht][yacht] is from the same family as Poker Dice, Generala and particularly Yahtzee, of which it is a precursor. | ||
In the game, five dice are rolled and the result can be entered in any of twelve categories. | ||
The score of a throw of the dice depends on category chosen. | ||
|
||
## Scores in Yacht | ||
|
||
| Category | Score | Description | Example | | ||
| --------------- | ---------------------- | ---------------------------------------- | ------------------- | | ||
| Ones | 1 × number of ones | Any combination | 1 1 1 4 5 scores 3 | | ||
| Twos | 2 × number of twos | Any combination | 2 2 3 4 5 scores 4 | | ||
| Threes | 3 × number of threes | Any combination | 3 3 3 3 3 scores 15 | | ||
| Fours | 4 × number of fours | Any combination | 1 2 3 3 5 scores 0 | | ||
| Fives | 5 × number of fives | Any combination | 5 1 5 2 5 scores 15 | | ||
| Sixes | 6 × number of sixes | Any combination | 2 3 4 5 6 scores 6 | | ||
| Full House | Total of the dice | Three of one number and two of another | 3 3 3 5 5 scores 19 | | ||
| Four of a Kind | Total of the four dice | At least four dice showing the same face | 4 4 4 4 6 scores 16 | | ||
| Little Straight | 30 points | 1-2-3-4-5 | 1 2 3 4 5 scores 30 | | ||
| Big Straight | 30 points | 2-3-4-5-6 | 2 3 4 5 6 scores 30 | | ||
| Choice | Sum of the dice | Any combination | 2 3 3 4 6 scores 18 | | ||
| Yacht | 50 points | All five dice showing the same face | 4 4 4 4 4 scores 50 | | ||
|
||
If the dice do not satisfy the requirements of a category, the score is zero. | ||
If, for example, _Four Of A Kind_ is entered in the _Yacht_ category, zero points are scored. | ||
A _Yacht_ scores zero if entered in the _Full House_ category. | ||
|
||
## Task | ||
|
||
Given a list of values for five dice and a category, your solution should return the score of the dice for that category. | ||
If the dice do not satisfy the requirements of the category your solution should return 0. | ||
You can assume that five values will always be presented, and the value of each will be between one and six inclusively. | ||
You should not assume that the dice are ordered. | ||
|
||
[yacht]: https://en.wikipedia.org/wiki/Yacht_(dice_game) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
{ | ||
"authors": [ | ||
"ahans" | ||
], | ||
"files": { | ||
"solution": [ | ||
"yacht.c", | ||
"yacht.h" | ||
], | ||
"test": [ | ||
"test_yacht.c" | ||
], | ||
"example": [ | ||
".meta/example.c", | ||
".meta/example.h" | ||
] | ||
}, | ||
"blurb": "Score a single throw of dice in the game Yacht.", | ||
"source": "James Kilfiger, using wikipedia", | ||
"source_url": "https://en.wikipedia.org/wiki/Yacht_(dice_game)" | ||
} |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,108 @@ | ||||||
#include "yacht.h" | ||||||
|
||||||
#include <assert.h> | ||||||
#include <stdbool.h> | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
I think this is unused There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A leftover from when |
||||||
#include <stdint.h> | ||||||
|
||||||
static int number_of_x(dice_t dice, int x) | ||||||
{ | ||||||
int sum = 0; | ||||||
for (int i = 0; i < 5; ++i) { | ||||||
if (dice.faces[i] == x) | ||||||
sum += x; | ||||||
} | ||||||
return sum; | ||||||
} | ||||||
|
||||||
typedef struct { | ||||||
uint8_t faces[6]; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There's definitely nothing wrong with a fixed sized integer, but it's inconsistent with the typing of the rest of the solution. If we change this to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you'd rather use fixed-width types consistently, that's also fine with me. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Where exactly is this inconsistent? I use My main motivation here, though, was efficiency. 6 bytes will easily be passed via a register when doing function calls. 24 bytes could be a different story. I can change it to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There are several places in this that use |
||||||
} count_t; | ||||||
|
||||||
static count_t count(const dice_t dice) | ||||||
{ | ||||||
count_t counts = { 0 }; | ||||||
for (int i = 0; i < 5; ++i) { | ||||||
assert(dice.faces[i] >= 1 && dice.faces[i] <= 6); | ||||||
wolf99 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
counts.faces[dice.faces[i] - 1] += 1; | ||||||
} | ||||||
return counts; | ||||||
} | ||||||
|
||||||
static int has_count(count_t counts, int n) | ||||||
{ | ||||||
for (int i = 0; i < 6; ++i) { | ||||||
if (counts.faces[i] == n) { | ||||||
return i + 1; | ||||||
} | ||||||
} | ||||||
return 0; | ||||||
} | ||||||
|
||||||
static int sum(dice_t dice) | ||||||
{ | ||||||
int s = 0; | ||||||
for (int i = 0; i < 5; ++i) { | ||||||
s += dice.faces[i]; | ||||||
} | ||||||
return s; | ||||||
} | ||||||
|
||||||
int score(dice_t dice, category_t category) | ||||||
{ | ||||||
switch (category) { | ||||||
case ONES: | ||||||
return number_of_x(dice, 1); | ||||||
case TWOS: | ||||||
return number_of_x(dice, 2); | ||||||
case THREES: | ||||||
return number_of_x(dice, 3); | ||||||
case FOURS: | ||||||
return number_of_x(dice, 4); | ||||||
case FIVES: | ||||||
return number_of_x(dice, 5); | ||||||
case SIXES: | ||||||
return number_of_x(dice, 6); | ||||||
case FULL_HOUSE: { | ||||||
const count_t counts = count(dice); | ||||||
if (has_count(counts, 3) && has_count(counts, 2)) { | ||||||
return sum(dice); | ||||||
} | ||||||
return 0; | ||||||
} | ||||||
case FOUR_OF_A_KIND: { | ||||||
const count_t counts = count(dice); | ||||||
const int face_with_count_4 = has_count(counts, 4); | ||||||
const int face_with_count_1 = has_count(counts, 1); | ||||||
const int face_with_count_5 = has_count(counts, 5); | ||||||
if ((face_with_count_4 && face_with_count_1) || face_with_count_5) { | ||||||
return 4 * (face_with_count_4 + face_with_count_5); | ||||||
} | ||||||
return 0; | ||||||
} | ||||||
case LITTLE_STRAIGHT: { | ||||||
const count_t counts = count(dice); | ||||||
for (int face = 0; face < 5; ++face) { | ||||||
if (counts.faces[face] != 1) | ||||||
return 0; | ||||||
} | ||||||
return 30; | ||||||
} | ||||||
case BIG_STRAIGHT: { | ||||||
const count_t counts = count(dice); | ||||||
for (int face = 1; face < 6; ++face) { | ||||||
if (counts.faces[face] != 1) | ||||||
return 0; | ||||||
} | ||||||
return 30; | ||||||
} | ||||||
case CHOICE: | ||||||
return sum(dice); | ||||||
case YACHT: { | ||||||
const count_t counts = count(dice); | ||||||
if (has_count(counts, 5)) | ||||||
return 50; | ||||||
return 0; | ||||||
} | ||||||
} | ||||||
return 0; | ||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
#ifndef YACHT_H | ||
#define YACHT_H | ||
|
||
#include <stdint.h> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this include can probably be removed now? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, right! Hadn't had a chance to look at the diff myself yet! This one is gone now as well. |
||
|
||
typedef enum { | ||
ONES, | ||
TWOS, | ||
THREES, | ||
FOURS, | ||
FIVES, | ||
SIXES, | ||
FULL_HOUSE, | ||
FOUR_OF_A_KIND, | ||
LITTLE_STRAIGHT, | ||
BIG_STRAIGHT, | ||
CHOICE, | ||
YACHT | ||
} category_t; | ||
|
||
typedef struct { | ||
uint8_t faces[5]; | ||
} dice_t; | ||
|
||
int score(dice_t dice, category_t category); | ||
|
||
#endif |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
# This is an auto-generated file. | ||
# | ||
# Regenerating this file via `configlet sync` will: | ||
# - Recreate every `description` key/value pair | ||
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications | ||
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion) | ||
# - Preserve any other key/value pair | ||
# | ||
# As user-added comments (using the # character) will be removed when this file | ||
# is regenerated, comments can be added via a `comment` key. | ||
|
||
[3060e4a5-4063-4deb-a380-a630b43a84b6] | ||
description = "Yacht" | ||
|
||
[15026df2-f567-482f-b4d5-5297d57769d9] | ||
description = "Not Yacht" | ||
|
||
[36b6af0c-ca06-4666-97de-5d31213957a4] | ||
description = "Ones" | ||
|
||
[023a07c8-6c6e-44d0-bc17-efc5e1b8205a] | ||
description = "Ones, out of order" | ||
|
||
[7189afac-cccd-4a74-8182-1cb1f374e496] | ||
description = "No ones" | ||
|
||
[793c4292-dd14-49c4-9707-6d9c56cee725] | ||
description = "Twos" | ||
|
||
[dc41bceb-d0c5-4634-a734-c01b4233a0c6] | ||
description = "Fours" | ||
|
||
[f6125417-5c8a-4bca-bc5b-b4b76d0d28c8] | ||
description = "Yacht counted as threes" | ||
|
||
[464fc809-96ed-46e4-acb8-d44e302e9726] | ||
description = "Yacht of 3s counted as fives" | ||
|
||
[d054227f-3a71-4565-a684-5c7e621ec1e9] | ||
description = "Fives" | ||
|
||
[e8a036e0-9d21-443a-8b5f-e15a9e19a761] | ||
description = "Sixes" | ||
|
||
[51cb26db-6b24-49af-a9ff-12f53b252eea] | ||
description = "Full house two small, three big" | ||
|
||
[1822ca9d-f235-4447-b430-2e8cfc448f0c] | ||
description = "Full house three small, two big" | ||
|
||
[b208a3fc-db2e-4363-a936-9e9a71e69c07] | ||
description = "Two pair is not a full house" | ||
|
||
[b90209c3-5956-445b-8a0b-0ac8b906b1c2] | ||
description = "Four of a kind is not a full house" | ||
|
||
[32a3f4ee-9142-4edf-ba70-6c0f96eb4b0c] | ||
description = "Yacht is not a full house" | ||
|
||
[b286084d-0568-4460-844a-ba79d71d79c6] | ||
description = "Four of a Kind" | ||
|
||
[f25c0c90-5397-4732-9779-b1e9b5f612ca] | ||
description = "Yacht can be scored as Four of a Kind" | ||
|
||
[9f8ef4f0-72bb-401a-a871-cbad39c9cb08] | ||
description = "Full house is not Four of a Kind" | ||
|
||
[b4743c82-1eb8-4a65-98f7-33ad126905cd] | ||
description = "Little Straight" | ||
|
||
[7ac08422-41bf-459c-8187-a38a12d080bc] | ||
description = "Little Straight as Big Straight" | ||
|
||
[97bde8f7-9058-43ea-9de7-0bc3ed6d3002] | ||
description = "Four in order but not a little straight" | ||
|
||
[cef35ff9-9c5e-4fd2-ae95-6e4af5e95a99] | ||
description = "No pairs but not a little straight" | ||
|
||
[fd785ad2-c060-4e45-81c6-ea2bbb781b9d] | ||
description = "Minimum is 1, maximum is 5, but not a little straight" | ||
|
||
[35bd74a6-5cf6-431a-97a3-4f713663f467] | ||
description = "Big Straight" | ||
|
||
[87c67e1e-3e87-4f3a-a9b1-62927822b250] | ||
description = "Big Straight as little straight" | ||
|
||
[c1fa0a3a-40ba-4153-a42d-32bc34d2521e] | ||
description = "No pairs but not a big straight" | ||
|
||
[207e7300-5d10-43e5-afdd-213e3ac8827d] | ||
description = "Choice" | ||
|
||
[b524c0cf-32d2-4b40-8fb3-be3500f3f135] | ||
description = "Yacht as choice" |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
### If you wish to use extra libraries (math.h for instance), | ||
### add their flags here (-lm in our case) in the "LIBS" variable. | ||
|
||
LIBS = -lm | ||
|
||
### | ||
CFLAGS = -std=c99 | ||
CFLAGS += -g | ||
CFLAGS += -Wall | ||
CFLAGS += -Wextra | ||
CFLAGS += -pedantic | ||
CFLAGS += -Werror | ||
CFLAGS += -Wmissing-declarations | ||
CFLAGS += -DUNITY_SUPPORT_64 -DUNITY_OUTPUT_COLOR | ||
|
||
ASANFLAGS = -fsanitize=address | ||
ASANFLAGS += -fno-common | ||
ASANFLAGS += -fno-omit-frame-pointer | ||
|
||
.PHONY: test | ||
test: tests.out | ||
@./tests.out | ||
|
||
.PHONY: memcheck | ||
memcheck: ./*.c ./*.h | ||
@echo Compiling $@ | ||
@$(CC) $(ASANFLAGS) $(CFLAGS) test-framework/unity.c ./*.c -o memcheck.out $(LIBS) | ||
@./memcheck.out | ||
@echo "Memory check passed" | ||
|
||
.PHONY: clean | ||
clean: | ||
rm -rf *.o *.out *.out.dSYM | ||
|
||
tests.out: ./*.c ./*.h | ||
@echo Compiling $@ | ||
@$(CC) $(CFLAGS) test-framework/unity.c ./*.c -o tests.out $(LIBS) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about a 4? We have Eliud's eggs rated as a 2 and that's a fair bit less complicated.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Whatever you find reasonable. Difficulties are all over the place.
spiral-matrix
is also a 4 and I would say that's more difficult than this. I updated it to 4 now.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed, many of these need to be revisited