From cb179f8bc7c2210fea0a0f95681b85dedf8332a1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A1s=20B=20Nagy?= <20251272+BNAndras@users.noreply.github.com> Date: Mon, 17 Jun 2024 22:40:36 -0700 Subject: [PATCH] add yacht --- config.json | 8 ++ .../practice/yacht/.docs/instructions.md | 30 ++++++ .../practice/yacht/.docs/introduction.md | 11 +++ exercises/practice/yacht/.gitignore | 11 +++ exercises/practice/yacht/.meta/config.json | 19 ++++ exercises/practice/yacht/.meta/example.lfe | 56 +++++++++++ exercises/practice/yacht/.meta/tests.toml | 97 +++++++++++++++++++ exercises/practice/yacht/Makefile | 21 ++++ exercises/practice/yacht/rebar.config | 11 +++ exercises/practice/yacht/rebar.lock | 8 ++ exercises/practice/yacht/src/yacht.app.src | 11 +++ exercises/practice/yacht/src/yacht.lfe | 5 + exercises/practice/yacht/test/yacht-tests.lfe | 93 ++++++++++++++++++ 13 files changed, 381 insertions(+) create mode 100644 exercises/practice/yacht/.docs/instructions.md create mode 100644 exercises/practice/yacht/.docs/introduction.md create mode 100644 exercises/practice/yacht/.gitignore create mode 100644 exercises/practice/yacht/.meta/config.json create mode 100644 exercises/practice/yacht/.meta/example.lfe create mode 100644 exercises/practice/yacht/.meta/tests.toml create mode 100644 exercises/practice/yacht/Makefile create mode 100644 exercises/practice/yacht/rebar.config create mode 100644 exercises/practice/yacht/rebar.lock create mode 100644 exercises/practice/yacht/src/yacht.app.src create mode 100644 exercises/practice/yacht/src/yacht.lfe create mode 100644 exercises/practice/yacht/test/yacht-tests.lfe diff --git a/config.json b/config.json index 88a48842..cbb8ac7c 100644 --- a/config.json +++ b/config.json @@ -370,6 +370,14 @@ "prerequisites": [], "difficulty": 2 }, + { + "slug": "yacht", + "name": "Yacht", + "uuid": "5c2f4d41-79bc-4c86-96ef-f70b2c2993c6", + "practices": [], + "prerequisites": [], + "difficulty": 2 + }, { "slug": "allergies", "name": "Allergies", diff --git a/exercises/practice/yacht/.docs/instructions.md b/exercises/practice/yacht/.docs/instructions.md new file mode 100644 index 00000000..519b7a68 --- /dev/null +++ b/exercises/practice/yacht/.docs/instructions.md @@ -0,0 +1,30 @@ +# Instructions + +Given five dice and a category, calculate the score of the dice for that category. + +~~~~exercism/note +You'll always be presented with five dice. +Each dice's value will be between one and six inclusively. +The dice may be unordered. +~~~~ + +## 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. diff --git a/exercises/practice/yacht/.docs/introduction.md b/exercises/practice/yacht/.docs/introduction.md new file mode 100644 index 00000000..5b541f56 --- /dev/null +++ b/exercises/practice/yacht/.docs/introduction.md @@ -0,0 +1,11 @@ +# Introduction + +Each year, something new is "all the rage" in your high school. +This year it is a dice game: [Yacht][yacht]. + +The game of Yacht is from the same family as Poker Dice, Generala and particularly Yahtzee, of which it is a precursor. +The game consists of twelve rounds. +In each, five dice are rolled and the player chooses one of twelve categories. +The chosen category is then used to score the throw of the dice. + +[yacht]: https://en.wikipedia.org/wiki/Yacht_(dice_game) diff --git a/exercises/practice/yacht/.gitignore b/exercises/practice/yacht/.gitignore new file mode 100644 index 00000000..6dd20ff0 --- /dev/null +++ b/exercises/practice/yacht/.gitignore @@ -0,0 +1,11 @@ +## -*- conf -*- +.rebar3 +_build/ +ebin/ +erl_crash.dump +rebar3.crashdump + +tmp +bin/configlet +bin/configlet.exe +CHECKLIST diff --git a/exercises/practice/yacht/.meta/config.json b/exercises/practice/yacht/.meta/config.json new file mode 100644 index 00000000..23dc06ae --- /dev/null +++ b/exercises/practice/yacht/.meta/config.json @@ -0,0 +1,19 @@ +{ + "authors": [ + "BNAndras" + ], + "files": { + "solution": [ + "src/yacht.lfe" + ], + "test": [ + "test/yacht-tests.lfe" + ], + "example": [ + ".meta/example.lfe" + ] + }, + "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)" +} diff --git a/exercises/practice/yacht/.meta/example.lfe b/exercises/practice/yacht/.meta/example.lfe new file mode 100644 index 00000000..9e935650 --- /dev/null +++ b/exercises/practice/yacht/.meta/example.lfe @@ -0,0 +1,56 @@ +(defmodule yacht + (export (score 2))) + +(include-lib "lfe/include/clj.lfe") + +(defun score + ((dice 'yacht) + (if (=:= (length (lists:uniq dice)) 1) 50 0)) + ((dice 'ones) + (sum dice 1)) + ((dice 'twos) + (sum dice 2)) + ((dice 'threes) + (sum dice 3)) + ((dice 'fours) + (sum dice 4)) + ((dice 'fives) + (sum dice 5)) + ((dice 'sixes) + (sum dice 6)) + ((dice 'full-house) + (if (=:= (lists:sort (maps:values (counter dice))) '(2 3)) + (lists:sum dice) + 0)) + ((dice 'four-of-a-kind) + (let ((match (maps:filter (lambda (_ val) (or (=:= val 4) + (=:= val 5))) + (counter dice)))) + (if (not (clj:empty? match)) + (* 4 (car (maps:keys match))) + 0))) + ((dice 'little-straight) + (if (=:= (lists:sort dice) '(1 2 3 4 5)) + 30 + 0)) + ((dice 'big-straight) + (if (=:= (lists:sort dice) '(2 3 4 5 6)) + 30 + 0)) + ((dice 'choice) + (lists:sum dice)) + ((_ _) + 0)) + +(defun counter (values) + (lists:foldl + (lambda (val counts) + (let ((count (+ 1 (maps:get val counts 0)))) + (maps:put val count counts))) + (map) + values)) + +(defun sum (dice n) + (let ((matches (lists:filter (lambda (die) (=:= die n)) dice))) + (* n (length matches)))) + diff --git a/exercises/practice/yacht/.meta/tests.toml b/exercises/practice/yacht/.meta/tests.toml new file mode 100644 index 00000000..b9d92037 --- /dev/null +++ b/exercises/practice/yacht/.meta/tests.toml @@ -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" diff --git a/exercises/practice/yacht/Makefile b/exercises/practice/yacht/Makefile new file mode 100644 index 00000000..fbb5a7de --- /dev/null +++ b/exercises/practice/yacht/Makefile @@ -0,0 +1,21 @@ +ERL := $(shell which erl) +REBAR3 := $(shell which rebar3) + +null := +space := $(null) # +comma := , + +ifeq ($(ERL),) + $(error Can't find Erlang executable 'erl') +else ifeq ($(REBAR3),) + $(error Can't find rebar3) +endif + +compile: ; $(REBAR3) compile + +clean: ; $(REBAR3) clean + +.PHONY: test +test: + $(REBAR3) eunit \ + -m $(subst $(space),$(comma),$(basename $(notdir $(wildcard test/*.lfe)))) diff --git a/exercises/practice/yacht/rebar.config b/exercises/practice/yacht/rebar.config new file mode 100644 index 00000000..30257891 --- /dev/null +++ b/exercises/practice/yacht/rebar.config @@ -0,0 +1,11 @@ +{plugins, [{rebar3_lfe, "0.4.10"}]}. + +{provider_hooks, [{post, [{compile, {lfe, compile}}]}]}. + +{deps, [{lfe, "2.1.3"}]}. + +{profiles, + [{test, + [{eunit_compile_opts, [{src_dirs, ["src", "test"]}]}, + {deps, + [{ltest, "0.13.8"}]}]}]}. diff --git a/exercises/practice/yacht/rebar.lock b/exercises/practice/yacht/rebar.lock new file mode 100644 index 00000000..45836829 --- /dev/null +++ b/exercises/practice/yacht/rebar.lock @@ -0,0 +1,8 @@ +{"1.2.0", +[{<<"lfe">>,{pkg,<<"lfe">>,<<"2.1.3">>},0}]}. +[ +{pkg_hash,[ + {<<"lfe">>, <<"6EFCB2BBC1FFC21DC5D1C092F00EFDB397EAC889474AC5C86EDF78A3557CC730">>}]}, +{pkg_hash_ext,[ + {<<"lfe">>, <<"4E4BAD515A169AE418FEB7374EA1C8D741FAEA9D95E266CE343B45BCC377F55B">>}]} +]. diff --git a/exercises/practice/yacht/src/yacht.app.src b/exercises/practice/yacht/src/yacht.app.src new file mode 100644 index 00000000..8c6bcceb --- /dev/null +++ b/exercises/practice/yacht/src/yacht.app.src @@ -0,0 +1,11 @@ +%% -*- erlang -*- +{application, 'yacht', + [{description, "a"}, + {vsn, "0.0.1"}, + {modules, + ['yacht']}, + {registered, []}, + {applications, + [kernel, stdlib]}, + {included_applications, []}, + {env, []}]}. diff --git a/exercises/practice/yacht/src/yacht.lfe b/exercises/practice/yacht/src/yacht.lfe new file mode 100644 index 00000000..e22e26fd --- /dev/null +++ b/exercises/practice/yacht/src/yacht.lfe @@ -0,0 +1,5 @@ +(defmodule yacht + (export (score 2))) + + ; Please implement the `score` function + diff --git a/exercises/practice/yacht/test/yacht-tests.lfe b/exercises/practice/yacht/test/yacht-tests.lfe new file mode 100644 index 00000000..253cec3b --- /dev/null +++ b/exercises/practice/yacht/test/yacht-tests.lfe @@ -0,0 +1,93 @@ +(defmodule yacht-tests + (behaviour ltest-unit) + (export all)) + +(include-lib "ltest/include/ltest-macros.lfe") + +(deftest yacht + (is-equal 50 (yacht:score '(5 5 5 5 5) 'yacht))) + +(deftest not-yacht + (is-equal 0 (yacht:score '(1 3 3 2 5) 'yacht))) + +(deftest ones + (is-equal 3 (yacht:score '(1 1 1 3 5) 'ones))) + +(deftest ones-out-of-order + (is-equal 3 (yacht:score '(3 1 1 5 1) 'ones))) + +(deftest no-ones + (is-equal 0 (yacht:score '(4 3 6 5 5) 'ones))) + +(deftest twos + (is-equal 2 (yacht:score '(2 3 4 5 6) 'twos))) + +(deftest fours + (is-equal 8 (yacht:score '(1 4 1 4 1) 'fours))) + +(deftest yacht-counted-as-threes + (is-equal 15 (yacht:score '(3 3 3 3 3) 'threes))) + +(deftest yacht-of-threes-counted-as-fives + (is-equal 0 (yacht:score '(3 3 3 3 3) 'fives))) + +(deftest fives + (is-equal 10 (yacht:score '(1 5 3 5 3) 'fives))) + +(deftest sixes + (is-equal 6 (yacht:score '(2 3 4 5 6) 'sixes))) + +(deftest full-house-two-small-three-big + (is-equal 16 (yacht:score '(2 2 4 4 4) 'full-house))) + +(deftest full-house-three-small-two-big + (is-equal 19 (yacht:score '(5 3 3 5 3) 'full-house))) + +(deftest two-pair-is-not-a-full-house + (is-equal 0 (yacht:score '(2 2 4 4 5) 'full-house))) + +(deftest four-of-a-kind-is-not-a-full-house + (is-equal 0 (yacht:score '(1 4 4 4 4) 'full-house))) + +(deftest yacht-is-not-a-full-house + (is-equal 0 (yacht:score '(2 2 2 2 2) 'full-house))) + +(deftest four-of-a-kind + (is-equal 24 (yacht:score '(6 6 4 6 6) 'four-of-a-kind))) + +(deftest yacht-can-be-scored-as-four-of-a-kind + (is-equal 12 (yacht:score '(3 3 3 3 3) 'four-of-a-kind))) + +(deftest full-house-is-not-four-of-a-kind + (is-equal 0 (yacht:score '(3 3 3 5 5) 'four-of-a-kind))) + +(deftest little-straight + (is-equal 30 (yacht:score '(3 5 4 1 2) 'little-straight))) + +(deftest little-straight-as-big-straight + (is-equal 0 (yacht:score '(1 2 3 4 5) 'big-straight))) + +(deftest four-in-order-but-not-a-little-straight + (is-equal 0 (yacht:score '(1 1 2 3 4) 'little-straight))) + +(deftest no-pairs-but-not-a-little-straight + (is-equal 0 (yacht:score '(1 2 3 4 6) 'little-straight))) + +(deftest minimum-is-1-maximum-is-5-but-not-a-little-straight + (is-equal 0 (yacht:score '(1 1 3 4 5) 'little-straight))) + +(deftest big-straight + (is-equal 30 (yacht:score '(4 6 2 5 3) 'big-straight))) + +(deftest big-straight-as-little-straight + (is-equal 0 (yacht:score '(6 5 4 3 2) 'little-straight))) + +(deftest no-pairs-but-not-a-big-straight + (is-equal 0 (yacht:score '(6 5 4 3 1) 'big-straight))) + +(deftest choice + (is-equal 23 (yacht:score '(3 3 5 6 6) 'choice))) + +(deftest yacht-as-choice + (is-equal 10 (yacht:score '(2 2 2 2 2) 'choice))) +