Skip to content

Commit c31f108

Browse files
authored
yacht-exercise: Added new exercise. (#132)
1 parent c574fe4 commit c31f108

File tree

10 files changed

+530
-0
lines changed

10 files changed

+530
-0
lines changed

config.json

+8
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,14 @@
423423
"prerequisites": [],
424424
"difficulty": 7
425425
},
426+
{
427+
"slug": "yacht",
428+
"name": "Yacht",
429+
"uuid": "135ead8d-cd69-4336-a885-20e56359cea8",
430+
"practices": [],
431+
"prerequisites": [],
432+
"difficulty": 3
433+
},
426434
{
427435
"slug": "pig-latin",
428436
"name": "Pig-Latin",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Instructions
2+
3+
Given five dice and a category, calculate the score of the dice for that category.
4+
5+
~~~~exercism/note
6+
You'll always be presented with five dice.
7+
Each dice's value will be between one and six inclusively.
8+
The dice may be unordered.
9+
~~~~
10+
11+
## Scores in Yacht
12+
13+
| Category | Score | Description | Example |
14+
| --------------- | ---------------------- | ---------------------------------------- | ------------------- |
15+
| Ones | 1 × number of ones | Any combination | 1 1 1 4 5 scores 3 |
16+
| Twos | 2 × number of twos | Any combination | 2 2 3 4 5 scores 4 |
17+
| Threes | 3 × number of threes | Any combination | 3 3 3 3 3 scores 15 |
18+
| Fours | 4 × number of fours | Any combination | 1 2 3 3 5 scores 0 |
19+
| Fives | 5 × number of fives | Any combination | 5 1 5 2 5 scores 15 |
20+
| Sixes | 6 × number of sixes | Any combination | 2 3 4 5 6 scores 6 |
21+
| Full House | Total of the dice | Three of one number and two of another | 3 3 3 5 5 scores 19 |
22+
| Four of a Kind | Total of the four dice | At least four dice showing the same face | 4 4 4 4 6 scores 16 |
23+
| Little Straight | 30 points | 1-2-3-4-5 | 1 2 3 4 5 scores 30 |
24+
| Big Straight | 30 points | 2-3-4-5-6 | 2 3 4 5 6 scores 30 |
25+
| Choice | Sum of the dice | Any combination | 2 3 3 4 6 scores 18 |
26+
| Yacht | 50 points | All five dice showing the same face | 4 4 4 4 4 scores 50 |
27+
28+
If the dice do **not** satisfy the requirements of a category, the score is zero.
29+
If, for example, _Four Of A Kind_ is entered in the _Yacht_ category, zero points are scored.
30+
A _Yacht_ scores zero if entered in the _Full House_ category.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Introduction
2+
3+
Each year, something new is "all the rage" in your high school.
4+
This year it is a dice game: [Yacht][yacht].
5+
6+
The game of Yacht is from the same family as Poker Dice, Generala and particularly Yahtzee, of which it is a precursor.
7+
The game consists of twelve rounds.
8+
In each, five dice are rolled and the player chooses one of twelve categories.
9+
The chosen category is then used to score the throw of the dice.
10+
11+
[yacht]: https://en.wikipedia.org/wiki/Yacht_(dice_game)
+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"authors": [
3+
"SimaDovakin"
4+
],
5+
"files": {
6+
"solution": [
7+
"yacht.u"
8+
],
9+
"test": [
10+
"yacht.test.u"
11+
],
12+
"example": [
13+
".meta/examples/yacht.example.u"
14+
]
15+
},
16+
"blurb": "Score a single throw of dice in the game Yacht.",
17+
"source": "James Kilfiger, using Wikipedia",
18+
"source_url": "https://en.wikipedia.org/wiki/Yacht_(dice_game)"
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
unique type yacht.Category
2+
= Ones
3+
| Twos
4+
| Threes
5+
| Fours
6+
| Fives
7+
| Sixes
8+
| FullHouse
9+
| FourOfAKind
10+
| LittleStraight
11+
| BigStraight
12+
| Choice
13+
| Yacht
14+
15+
yacht.yacht : Category -> [Nat] -> Nat
16+
yacht.yacht category dice =
17+
match category with
18+
Ones -> calculateNumericCategories 1 dice
19+
Twos -> calculateNumericCategories 2 dice
20+
Threes -> calculateNumericCategories 3 dice
21+
Fours -> calculateNumericCategories 4 dice
22+
Fives -> calculateNumericCategories 5 dice
23+
Sixes -> calculateNumericCategories 6 dice
24+
FullHouse -> fullHouse dice
25+
FourOfAKind -> fourOfAKind dice
26+
LittleStraight -> littleStraight dice
27+
BigStraight -> bigStraight dice
28+
Choice -> sum dice
29+
Yacht -> yacht' dice
30+
31+
yacht.calculateNumericCategories : Nat -> [Nat] -> Nat
32+
yacht.calculateNumericCategories element dice = element * countElement element dice
33+
34+
yacht.fullHouse : [Nat] -> Nat
35+
yacht.fullHouse dice =
36+
diceFrequencies = frequencies dice
37+
match values diceFrequencies with
38+
[2, 3] -> sum dice
39+
[3, 2] -> sum dice
40+
_ -> 0
41+
42+
yacht.fourOfAKind : [Nat] -> Nat
43+
yacht.fourOfAKind dice =
44+
diceFrequencies = frequencies dice
45+
match toList diceFrequencies with
46+
[_, (die, 4)] -> die * 4
47+
[(die, 4), _] -> die * 4
48+
[(die, 5)] -> die * 4
49+
_ -> 0
50+
51+
yacht.littleStraight : [Nat] -> Nat
52+
yacht.littleStraight dice =
53+
match List.sort dice with
54+
[1, 2, 3, 4, 5] -> 30
55+
_ -> 0
56+
57+
yacht.bigStraight : [Nat] -> Nat
58+
yacht.bigStraight dice =
59+
match List.sort dice with
60+
[2, 3, 4, 5, 6] -> 30
61+
_ -> 0
62+
63+
yacht.yacht' : [Nat] -> Nat
64+
yacht.yacht' dice =
65+
diceFrequencies = frequencies dice
66+
match size diceFrequencies with
67+
1 -> 50
68+
_ -> 0
69+
70+
yacht.frequencies : [Nat] -> Map Nat Nat
71+
yacht.frequencies =
72+
updateFrequency = cases
73+
Some die -> Some (die + 1)
74+
None -> Some 1
75+
List.foldLeft (acc die -> Map.alter updateFrequency die acc) Map.empty
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
[
2+
{
3+
"name": "yacht.yacht.tests.ex1",
4+
"test_code": "expect (50 == yacht.yacht Yacht [5, 5, 5, 5, 5])\n |> Test.label \"Yacht\""
5+
},
6+
{
7+
"name": "yacht.yacht.tests.ex2",
8+
"test_code": "expect (0 == yacht.yacht Yacht [1, 3, 3, 2, 5])\n |> Test.label \"Not Yacht\""
9+
},
10+
{
11+
"name": "yacht.yacht.tests.ex3",
12+
"test_code": "expect (3 == yacht.yacht Ones [1, 1, 1, 3, 5])\n |> Test.label \"Ones\""
13+
},
14+
{
15+
"name": "yacht.yacht.tests.ex4",
16+
"test_code": "expect (3 == yacht.yacht Ones [3, 1, 1, 5, 1])\n |> Test.label \"Ones, out of order\""
17+
},
18+
{
19+
"name": "yacht.yacht.tests.ex5",
20+
"test_code": "expect (0 == yacht.yacht Ones [4, 3, 6, 5, 5])\n |> Test.label \"No ones\""
21+
},
22+
{
23+
"name": "yacht.yacht.tests.ex6",
24+
"test_code": "expect (2 == yacht.yacht Twos [2, 3, 4, 5, 6])\n |> Test.label \"Twos\""
25+
},
26+
{
27+
"name": "yacht.yacht.tests.ex7",
28+
"test_code": "expect (8 == yacht.yacht Fours [1, 4, 1, 4, 1])\n |> Test.label \"Fours\""
29+
},
30+
{
31+
"name": "yacht.yacht.tests.ex8",
32+
"test_code": "expect (15 == yacht.yacht Threes [3, 3, 3, 3, 3])\n |> Test.label \"Yacht counted as threes\""
33+
},
34+
{
35+
"name": "yacht.yacht.tests.ex9",
36+
"test_code": "expect (0 == yacht.yacht Fives [3, 3, 3, 3, 3])\n |> Test.label \"Yacht of 3s counted as fives\""
37+
},
38+
{
39+
"name": "yacht.yacht.tests.ex10",
40+
"test_code": "expect (10 == yacht.yacht Fives [1, 5, 3, 5, 3])\n |> Test.label \"Fives\""
41+
},
42+
{
43+
"name": "yacht.yacht.tests.ex11",
44+
"test_code": "expect (6 == yacht.yacht Sixes [2, 3, 4, 5, 6])\n |> Test.label \"Sixes\""
45+
},
46+
{
47+
"name": "yacht.yacht.tests.ex12",
48+
"test_code": "expect (16 == yacht.yacht FullHouse [2, 2, 4, 4, 4])\n |> Test.label \"Full house two small, three big\""
49+
},
50+
{
51+
"name": "yacht.yacht.tests.ex13",
52+
"test_code": "expect (19 == yacht.yacht FullHouse [5, 3, 3, 5, 3])\n |> Test.label \"Full house three small, two big\""
53+
},
54+
{
55+
"name": "yacht.yacht.tests.ex14",
56+
"test_code": "expect (0 == yacht.yacht FullHouse [2, 2, 4, 4, 5])\n |> Test.label \"Two pair is not a full house\""
57+
},
58+
{
59+
"name": "yacht.yacht.tests.ex15",
60+
"test_code": "expect (0 == yacht.yacht FullHouse [1, 4, 4, 4, 4])\n |> Test.label \"Four of a kind is not a full house\""
61+
},
62+
{
63+
"name": "yacht.yacht.tests.ex16",
64+
"test_code": "expect (0 == yacht.yacht FullHouse [2, 2, 2, 2, 2])\n |> Test.label \"Yacht is not a full house\""
65+
},
66+
{
67+
"name": "yacht.yacht.tests.ex17",
68+
"test_code": "expect (24 == yacht.yacht FourOfAKind [6, 6, 4, 6, 6])\n |> Test.label \"Four of a Kind\""
69+
},
70+
{
71+
"name": "yacht.yacht.tests.ex18",
72+
"test_code": "expect (12 == yacht.yacht FourOfAKind [3, 3, 3, 3, 3])\n |> Test.label \"Yacht can be scored as Four of a Kind\""
73+
},
74+
{
75+
"name": "yacht.yacht.tests.ex19",
76+
"test_code": "expect (0 == yacht.yacht FourOfAKind [3, 3, 3, 5, 5])\n |> Test.label \"Full house is not Four of a Kind\""
77+
},
78+
{
79+
"name": "yacht.yacht.tests.ex20",
80+
"test_code": "expect (30 == yacht.yacht LittleStraight [3, 5, 4, 1, 2])\n |> Test.label \"Little Straight\""
81+
},
82+
{
83+
"name": "yacht.yacht.tests.ex21",
84+
"test_code": "expect (0 == yacht.yacht BigStraight [1, 2, 3, 4, 5])\n |> Test.label \"Little Straight as Big Straight\""
85+
},
86+
{
87+
"name": "yacht.yacht.tests.ex22",
88+
"test_code": "expect (0 == yacht.yacht LittleStraight [1, 1, 2, 3, 4])\n |> Test.label \"Four in order but not a little straight\""
89+
},
90+
{
91+
"name": "yacht.yacht.tests.ex23",
92+
"test_code": "expect (0 == yacht.yacht LittleStraight [1, 2, 3, 4, 6])\n |> Test.label \"No pairs but not a little straight\""
93+
},
94+
{
95+
"name": "yacht.yacht.tests.ex24",
96+
"test_code": "expect (0 == yacht.yacht LittleStraight [1, 1, 3, 4, 5])\n |> Test.label \"Minimum is 1, maximum is 5, but not a little straight\""
97+
},
98+
{
99+
"name": "yacht.yacht.tests.ex25",
100+
"test_code": "expect (30 == yacht.yacht BigStraight [4, 6, 2, 5, 3])\n |> Test.label \"Big Straight\""
101+
},
102+
{
103+
"name": "yacht.yacht.tests.ex26",
104+
"test_code": "expect (0 == yacht.yacht LittleStraight [6, 5, 4, 3, 2])\n |> Test.label \"Big Straight as little straight\""
105+
},
106+
{
107+
"name": "yacht.yacht.tests.ex27",
108+
"test_code": "expect (0 == yacht.yacht BigStraight [6, 5, 4, 3, 1])\n |> Test.label \"No pairs but not a big straight\""
109+
},
110+
{
111+
"name": "yacht.yacht.tests.ex28",
112+
"test_code": "expect (23 == yacht.yacht Choice [3, 3, 5, 6, 6])\n |> Test.label \"Choice\""
113+
},
114+
{
115+
"name": "yacht.yacht.tests.ex29",
116+
"test_code": "expect (10 == yacht.yacht Choice [2, 2, 2, 2, 2])\n |> Test.label \"Yacht as choice\""
117+
}
118+
]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Testing transcript for yacht exercise
2+
3+
```ucm
4+
.> load ./yacht.u
5+
.> add
6+
.> load ./yacht.test.u
7+
.> add
8+
.> move.term yacht.tests tests
9+
```
+97
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
# This is an auto-generated file.
2+
#
3+
# Regenerating this file via `configlet sync` will:
4+
# - Recreate every `description` key/value pair
5+
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
6+
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
7+
# - Preserve any other key/value pair
8+
#
9+
# As user-added comments (using the # character) will be removed when this file
10+
# is regenerated, comments can be added via a `comment` key.
11+
12+
[3060e4a5-4063-4deb-a380-a630b43a84b6]
13+
description = "Yacht"
14+
15+
[15026df2-f567-482f-b4d5-5297d57769d9]
16+
description = "Not Yacht"
17+
18+
[36b6af0c-ca06-4666-97de-5d31213957a4]
19+
description = "Ones"
20+
21+
[023a07c8-6c6e-44d0-bc17-efc5e1b8205a]
22+
description = "Ones, out of order"
23+
24+
[7189afac-cccd-4a74-8182-1cb1f374e496]
25+
description = "No ones"
26+
27+
[793c4292-dd14-49c4-9707-6d9c56cee725]
28+
description = "Twos"
29+
30+
[dc41bceb-d0c5-4634-a734-c01b4233a0c6]
31+
description = "Fours"
32+
33+
[f6125417-5c8a-4bca-bc5b-b4b76d0d28c8]
34+
description = "Yacht counted as threes"
35+
36+
[464fc809-96ed-46e4-acb8-d44e302e9726]
37+
description = "Yacht of 3s counted as fives"
38+
39+
[d054227f-3a71-4565-a684-5c7e621ec1e9]
40+
description = "Fives"
41+
42+
[e8a036e0-9d21-443a-8b5f-e15a9e19a761]
43+
description = "Sixes"
44+
45+
[51cb26db-6b24-49af-a9ff-12f53b252eea]
46+
description = "Full house two small, three big"
47+
48+
[1822ca9d-f235-4447-b430-2e8cfc448f0c]
49+
description = "Full house three small, two big"
50+
51+
[b208a3fc-db2e-4363-a936-9e9a71e69c07]
52+
description = "Two pair is not a full house"
53+
54+
[b90209c3-5956-445b-8a0b-0ac8b906b1c2]
55+
description = "Four of a kind is not a full house"
56+
57+
[32a3f4ee-9142-4edf-ba70-6c0f96eb4b0c]
58+
description = "Yacht is not a full house"
59+
60+
[b286084d-0568-4460-844a-ba79d71d79c6]
61+
description = "Four of a Kind"
62+
63+
[f25c0c90-5397-4732-9779-b1e9b5f612ca]
64+
description = "Yacht can be scored as Four of a Kind"
65+
66+
[9f8ef4f0-72bb-401a-a871-cbad39c9cb08]
67+
description = "Full house is not Four of a Kind"
68+
69+
[b4743c82-1eb8-4a65-98f7-33ad126905cd]
70+
description = "Little Straight"
71+
72+
[7ac08422-41bf-459c-8187-a38a12d080bc]
73+
description = "Little Straight as Big Straight"
74+
75+
[97bde8f7-9058-43ea-9de7-0bc3ed6d3002]
76+
description = "Four in order but not a little straight"
77+
78+
[cef35ff9-9c5e-4fd2-ae95-6e4af5e95a99]
79+
description = "No pairs but not a little straight"
80+
81+
[fd785ad2-c060-4e45-81c6-ea2bbb781b9d]
82+
description = "Minimum is 1, maximum is 5, but not a little straight"
83+
84+
[35bd74a6-5cf6-431a-97a3-4f713663f467]
85+
description = "Big Straight"
86+
87+
[87c67e1e-3e87-4f3a-a9b1-62927822b250]
88+
description = "Big Straight as little straight"
89+
90+
[c1fa0a3a-40ba-4153-a42d-32bc34d2521e]
91+
description = "No pairs but not a big straight"
92+
93+
[207e7300-5d10-43e5-afdd-213e3ac8827d]
94+
description = "Choice"
95+
96+
[b524c0cf-32d2-4b40-8fb3-be3500f3f135]
97+
description = "Yacht as choice"

0 commit comments

Comments
 (0)