Skip to content

Commit e5749de

Browse files
committed
[#722] New practice exercise yacht
1 parent 51697e4 commit e5749de

File tree

14 files changed

+672
-0
lines changed

14 files changed

+672
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Score a single throw of dice in *Yacht*
2+
3+
The dice game [Yacht](https://en.wikipedia.org/wiki/Yacht_(dice_game)) is from
4+
the same family as Poker Dice, Generala and particularly Yahtzee, of which it
5+
is a precursor. In the game, five dice are rolled and the result can be entered
6+
in any of twelve categories. The score of a throw of the dice depends on
7+
category chosen.
8+
9+
## Scores in Yacht
10+
11+
| Category | Score | Description | Example |
12+
| -------- | ----- | ----------- | ------- |
13+
| Ones | 1 × number of ones | Any combination | 1 1 1 4 5 scores 3 |
14+
| Twos | 2 × number of twos | Any combination | 2 2 3 4 5 scores 4 |
15+
| Threes | 3 × number of threes | Any combination | 3 3 3 3 3 scores 15 |
16+
| Fours | 4 × number of fours | Any combination | 1 2 3 3 5 scores 0 |
17+
| Fives | 5 × number of fives| Any combination | 5 1 5 2 5 scores 15 |
18+
| Sixes | 6 × number of sixes | Any combination | 2 3 4 5 6 scores 6 |
19+
| Full House | Total of the dice | Three of one number and two of another | 3 3 3 5 5 scores 19 |
20+
| Four of a Kind | Total of the four dice | At least four dice showing the same face | 4 4 4 4 6 scores 16 |
21+
| Little Straight | 30 points | 1-2-3-4-5 | 1 2 3 4 5 scores 30 |
22+
| Big Straight | 30 points | 2-3-4-5-6 | 2 3 4 5 6 scores 30 |
23+
| Choice | Sum of the dice | Any combination | 2 3 3 4 6 scores 18 |
24+
| Yacht | 50 points | All five dice showing the same face | 4 4 4 4 4 scores 50 |
25+
26+
If the dice do not satisfy the requirements of a category, the score is zero.
27+
If, for example, *Four Of A Kind* is entered in the *Yacht* category, zero
28+
points are scored. A *Yacht* scores zero if entered in the *Full House* category.
29+
30+
## Task
31+
Given a list of values for five dice and a category, your solution should return
32+
the score of the dice for that category. If the dice do not satisfy the requirements
33+
of the category your solution should return 0. You can assume that five values
34+
will always be presented, and the value of each will be between one and six
35+
inclusively. You should not assume that the dice are ordered.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Used by "mix format"
2+
[
3+
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"]
4+
]
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# The directory Mix will write compiled artifacts to.
2+
/_build/
3+
4+
# If you run "mix test --cover", coverage assets end up here.
5+
/cover/
6+
7+
# The directory Mix downloads your dependencies sources to.
8+
/deps/
9+
10+
# Where third-party dependencies like ExDoc output generated docs.
11+
/doc/
12+
13+
# Ignore .fetch files in case you like to edit your project deps locally.
14+
/.fetch
15+
16+
# If the VM crashes, it generates a dump, let's ignore it too.
17+
erl_crash.dump
18+
19+
# Also ignore archive artifacts (built via "mix archive.build").
20+
*.ez
21+
22+
# Ignore package tarball (built via "mix hex.build").
23+
yacht-*.tar
24+
25+
# Temporary files, for example, from tests.
26+
/tmp/
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"blurb": "Score a single throw of dice in the game Yacht." ,
3+
"authors": [
4+
"elibdev"
5+
],
6+
"contributors": [
7+
"angelikatyborska"
8+
],
9+
"files": {
10+
"solution": [
11+
"lib/yacht.ex"
12+
],
13+
"test": [
14+
"test/yacht_test.exs"
15+
],
16+
"example": [
17+
".meta/example.ex"
18+
]
19+
},
20+
"source": "James Kilfiger, using wikipedia",
21+
"source_url": "https://en.wikipedia.org/wiki/Yacht_(dice_game)"
22+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
defmodule Yacht do
2+
@doc """
3+
Calculate the score of the list of 5 dice rolls using the given category.
4+
"""
5+
@spec score(category :: String.t(), dice :: [integer]) :: integer
6+
def score(category, dice)
7+
8+
def score("ones", dice), do: score(1, dice)
9+
def score("twos", dice), do: score(2, dice)
10+
def score("threes", dice), do: score(3, dice)
11+
def score("fours", dice), do: score(4, dice)
12+
def score("fives", dice), do: score(5, dice)
13+
def score("sixes", dice), do: score(6, dice)
14+
15+
def score(number, dice) when is_integer(number) do
16+
Enum.count(dice, &(&1 == number)) * number
17+
end
18+
19+
def score("full house", dice) do
20+
full_house =
21+
Enum.frequencies(dice)
22+
|> Map.values()
23+
|> MapSet.new() ==
24+
MapSet.new([3, 2])
25+
26+
if full_house do
27+
Enum.sum(dice)
28+
else
29+
0
30+
end
31+
end
32+
33+
def score("four of a kind", dice) do
34+
frequencies =
35+
Enum.frequencies(dice)
36+
|> Enum.to_list()
37+
|> Enum.filter(fn {_, freq} -> freq >= 4 end)
38+
39+
case frequencies do
40+
[{number, _freq}] ->
41+
number * 4
42+
43+
_ ->
44+
0
45+
end
46+
end
47+
48+
def score("little straight", dice) do
49+
if MapSet.new(dice) == MapSet.new([1, 2, 3, 4, 5]) do
50+
30
51+
else
52+
0
53+
end
54+
end
55+
56+
def score("big straight", dice) do
57+
if MapSet.new(dice) == MapSet.new([2, 3, 4, 5, 6]) do
58+
30
59+
else
60+
0
61+
end
62+
end
63+
64+
def score("choice", dice) do
65+
Enum.sum(dice)
66+
end
67+
68+
def score("yacht", dice) do
69+
unique_dice = MapSet.size(MapSet.new(dice))
70+
71+
case unique_dice do
72+
1 -> 50
73+
_ -> 0
74+
end
75+
end
76+
end
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# Generate tests.toml file from canonical-data.json
2+
# $ mix run .meta/gen_tests_toml.exs priv/canonical-data.json .meta/tests.toml
3+
4+
Mix.install([{:jason, "~> 1.2"}])
5+
6+
args = System.argv()
7+
8+
if length(args) != 2 do
9+
IO.puts("""
10+
This script requires two positional arguments:
11+
12+
1. the path to the `canonical-data.json` file to read the tests from
13+
2. the path to the `tests.toml` file to write to
14+
15+
$ mix run .meta/gen_tests_toml.exs priv/canonical-data.json .meta/tests.toml
16+
""")
17+
18+
System.halt(1)
19+
end
20+
21+
[data_file, test_file] = args
22+
23+
tests =
24+
File.read!(data_file)
25+
|> Jason.decode!()
26+
|> Map.get("cases")
27+
28+
intro = """
29+
# This is an auto-generated file. Regular comments will be removed when this
30+
# file is regenerated. Regenerating will not touch any manually added keys,
31+
# so comments can be added in a "comment" key.
32+
33+
"""
34+
35+
tests_toml =
36+
Enum.reduce(tests, intro, fn test, tests_toml ->
37+
uuid = test["uuid"]
38+
desc = test["description"]
39+
40+
tests_toml <>
41+
"""
42+
[#{uuid}]
43+
description = "#{desc}"
44+
45+
"""
46+
end)
47+
48+
File.write!(test_file, tests_toml)
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
# This is an auto-generated file. Regular comments will be removed when this
2+
# file is regenerated. Regenerating will not touch any manually added keys,
3+
# so comments can be added in a "comment" key.
4+
5+
[3060e4a5-4063-4deb-a380-a630b43a84b6]
6+
description = "Yacht"
7+
8+
[15026df2-f567-482f-b4d5-5297d57769d9]
9+
description = "Not Yacht"
10+
11+
[36b6af0c-ca06-4666-97de-5d31213957a4]
12+
description = "Ones"
13+
14+
[023a07c8-6c6e-44d0-bc17-efc5e1b8205a]
15+
description = "Ones, out of order"
16+
17+
[7189afac-cccd-4a74-8182-1cb1f374e496]
18+
description = "No ones"
19+
20+
[793c4292-dd14-49c4-9707-6d9c56cee725]
21+
description = "Twos"
22+
23+
[dc41bceb-d0c5-4634-a734-c01b4233a0c6]
24+
description = "Fours"
25+
26+
[f6125417-5c8a-4bca-bc5b-b4b76d0d28c8]
27+
description = "Yacht counted as threes"
28+
29+
[464fc809-96ed-46e4-acb8-d44e302e9726]
30+
description = "Yacht of 3s counted as fives"
31+
32+
[e8a036e0-9d21-443a-8b5f-e15a9e19a761]
33+
description = "Sixes"
34+
35+
[51cb26db-6b24-49af-a9ff-12f53b252eea]
36+
description = "Full house two small, three big"
37+
38+
[1822ca9d-f235-4447-b430-2e8cfc448f0c]
39+
description = "Full house three small, two big"
40+
41+
[b208a3fc-db2e-4363-a936-9e9a71e69c07]
42+
description = "Two pair is not a full house"
43+
44+
[b90209c3-5956-445b-8a0b-0ac8b906b1c2]
45+
description = "Four of a kind is not a full house"
46+
47+
[32a3f4ee-9142-4edf-ba70-6c0f96eb4b0c]
48+
description = "Yacht is not a full house"
49+
50+
[b286084d-0568-4460-844a-ba79d71d79c6]
51+
description = "Four of a Kind"
52+
53+
[f25c0c90-5397-4732-9779-b1e9b5f612ca]
54+
description = "Yacht can be scored as Four of a Kind"
55+
56+
[9f8ef4f0-72bb-401a-a871-cbad39c9cb08]
57+
description = "Full house is not Four of a Kind"
58+
59+
[b4743c82-1eb8-4a65-98f7-33ad126905cd]
60+
description = "Little Straight"
61+
62+
[7ac08422-41bf-459c-8187-a38a12d080bc]
63+
description = "Little Straight as Big Straight"
64+
65+
[97bde8f7-9058-43ea-9de7-0bc3ed6d3002]
66+
description = "Four in order but not a little straight"
67+
68+
[cef35ff9-9c5e-4fd2-ae95-6e4af5e95a99]
69+
description = "No pairs but not a little straight"
70+
71+
[fd785ad2-c060-4e45-81c6-ea2bbb781b9d]
72+
description = "Minimum is 1, maximum is 5, but not a little straight"
73+
74+
[35bd74a6-5cf6-431a-97a3-4f713663f467]
75+
description = "Big Straight"
76+
77+
[87c67e1e-3e87-4f3a-a9b1-62927822b250]
78+
description = "Big Straight as little straight"
79+
80+
[c1fa0a3a-40ba-4153-a42d-32bc34d2521e]
81+
description = "No pairs but not a big straight"
82+
83+
[207e7300-5d10-43e5-afdd-213e3ac8827d]
84+
description = "Choice"
85+
86+
[b524c0cf-32d2-4b40-8fb3-be3500f3f135]
87+
description = "Yacht as choice"
88+

exercises/practice/yacht/README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Yacht
2+
3+
**TODO: Add description**
4+
5+
## Installation
6+
7+
If [available in Hex](https://hex.pm/docs/publish), the package can be installed
8+
by adding `yacht` to your list of dependencies in `mix.exs`:
9+
10+
```elixir
11+
def deps do
12+
[
13+
{:yacht, "~> 0.1.0"}
14+
]
15+
end
16+
```
17+
18+
Documentation can be generated with [ExDoc](https://github.com/elixir-lang/ex_doc)
19+
and published on [HexDocs](https://hexdocs.pm). Once published, the docs can
20+
be found at [https://hexdocs.pm/yacht](https://hexdocs.pm/yacht).
21+
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
defmodule Yacht do
2+
@doc """
3+
Calculate the score of the list of 5 dice rolls using the given category.
4+
"""
5+
@spec score(category :: String.t(), dice :: [integer]) :: integer
6+
def score(category, dice) do
7+
end
8+
end

exercises/practice/yacht/mix.exs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
defmodule Yacht.MixProject do
2+
use Mix.Project
3+
4+
def project do
5+
[
6+
app: :yacht,
7+
version: "0.1.0",
8+
# elixir: "~> 1.12",
9+
start_permanent: Mix.env() == :prod,
10+
deps: deps()
11+
]
12+
end
13+
14+
# Run "mix help compile.app" to learn about applications.
15+
def application do
16+
[
17+
extra_applications: [:logger]
18+
]
19+
end
20+
21+
# Run "mix help deps" to learn about dependencies.
22+
defp deps do
23+
[
24+
{:jason, "~> 1.2"}
25+
]
26+
end
27+
end

0 commit comments

Comments
 (0)