Skip to content

Commit

Permalink
Add yacht exercise
Browse files Browse the repository at this point in the history
  • Loading branch information
kahgoh committed Jun 11, 2024
1 parent 7d281be commit b1a8bf7
Show file tree
Hide file tree
Showing 8 changed files with 400 additions and 0 deletions.
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -758,6 +758,14 @@
"practices": [],
"prerequisites": [],
"difficulty": 8
},
{
"slug": "yacht",
"name": "Yacht",
"uuid": "67c5ced9-9664-4b9f-a33e-d37f7341cc16",
"practices": [],
"prerequisites": [],
"difficulty": 4
}
]
},
Expand Down
30 changes: 30 additions & 0 deletions exercises/practice/yacht/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -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.
11 changes: 11 additions & 0 deletions exercises/practice/yacht/.docs/introduction.md
Original file line number Diff line number Diff line change
@@ -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)
19 changes: 19 additions & 0 deletions exercises/practice/yacht/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"authors": [
"kahgoh"
],
"files": {
"solution": [
"yacht.v"
],
"test": [
"run_test.v"
],
"example": [
".meta/example.v"
]
},
"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)"
}
96 changes: 96 additions & 0 deletions exercises/practice/yacht/.meta/example.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
module main

import arrays

enum Category as u8 {
yacht
ones
twos
threes
fours
fives
sixes
full_house
four_of_a_kind
little_straight
big_straight
choice
}

fn score(category Category, rolls []u8) int {
return match category {
.yacht { score_yacht(rolls) }
.ones { score_num(rolls, 1) }
.twos { score_num(rolls, 2) }
.threes { score_num(rolls, 3) }
.fours { score_num(rolls, 4) }
.fives { score_num(rolls, 5) }
.sixes { score_num(rolls, 6) }
.full_house { score_full_house(rolls) }
.four_of_a_kind { score_four_of_a_kind(rolls) }
.little_straight { score_ordered_matches(rolls, [u8(1), 2, 3, 4, 5]) }
.big_straight { score_ordered_matches(rolls, [u8(2), 3, 4, 5, 6]) }
.choice { sum(rolls) }
}
}

fn score_yacht(rolls []u8) int {
unique := arrays.uniq(rolls)
if (unique.len > 1) {
return 0
}
return 50
}

fn score_num(rolls []u8, req u8) int {
mut score := 0
for r in rolls {
if r == req {
score += r
}
}
return score
}

fn score_full_house(rolls []u8) int {
roll_counts := counts(rolls).values()
if roll_counts != [u8(2), 3] && roll_counts != [u8(3), 2] {
return 0
}

return sum(rolls)
}

fn score_four_of_a_kind(rolls []u8) int {
mut score := 0
roll_counts := counts(rolls)
for n, count in roll_counts {
if count >= 4 {
score += 4 * n
}
}
return score
}

fn score_ordered_matches(rolls []u8, req []u8) int {
if rolls.sorted() != req {
return 0
}
return 30
}

fn sum(nums []u8) int {
mut sum := 0
for n in nums {
sum += n
}
return sum
}

fn counts(rolls []u8) map[u8]u8 {
mut counts := map[u8]u8{}
for r in rolls {
counts[r] += 1
}
return counts
}
97 changes: 97 additions & 0 deletions exercises/practice/yacht/.meta/tests.toml
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"
117 changes: 117 additions & 0 deletions exercises/practice/yacht/run_test.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
module main

fn test_yacht() {
assert score(Category.yacht, [u8(5), 5, 5, 5, 5]) == 50
}

fn test_not_yacht() {
assert score(Category.yacht, [u8(1), 3, 3, 2, 5]) == 0
}

fn test_ones() {
assert score(Category.ones, [u8(1), 1, 1, 3, 5]) == 3
}

fn test_ones_out_of_order() {
assert score(Category.ones, [u8(3), 1, 1, 5, 1]) == 3
}

fn test_no_ones() {
assert score(Category.ones, [u8(4), 3, 6, 5, 5]) == 0
}

fn test_twos() {
assert score(Category.twos, [u8(2), 3, 4, 5, 6]) == 2
}

fn test_fours() {
assert score(Category.fours, [u8(1), 4, 1, 4, 1]) == 8
}

fn test_yacht_counted_as_threes() {
assert score(Category.threes, [u8(3), 3, 3, 3, 3]) == 15
}

fn test_yacht_of_3s_counted_as_fives() {
assert score(Category.fives, [u8(3), 3, 3, 3, 3]) == 0
}

fn test_fives() {
assert score(Category.fives, [u8(1), 5, 3, 5, 3]) == 10
}

fn test_sixes() {
assert score(Category.sixes, [u8(2), 3, 4, 5, 6]) == 6
}

fn test_full_house_two_small_three_big() {
assert score(Category.full_house, [u8(2), 2, 4, 4, 4]) == 16
}

fn test_full_house_three_small_two_big() {
assert score(Category.full_house, [u8(5), 3, 3, 5, 3]) == 19
}

fn test_two_pair_is_not_a_full_house() {
assert score(Category.full_house, [u8(2), 2, 4, 4, 5]) == 0
}

fn test_four_of_a_kind_is_not_a_full_house() {
assert score(Category.full_house, [u8(1), 4, 4, 4, 4]) == 0
}

fn test_yacht_is_not_a_full_house() {
assert score(Category.full_house, [u8(2), 2, 2, 2, 2]) == 0
}

fn test_four_of_a_kind() {
assert score(Category.four_of_a_kind, [u8(6), 6, 4, 6, 6]) == 24
}

fn test_yacht_can_be_scored_as_four_of_a_kind() {
assert score(Category.four_of_a_kind, [u8(3), 3, 3, 3, 3]) == 12
}

fn test_full_house_is_not_four_of_a_kind() {
assert score(Category.four_of_a_kind, [u8(3), 3, 3, 5, 5]) == 0
}

fn test_little_straight() {
assert score(Category.little_straight, [u8(3), 5, 4, 1, 2]) == 30
}

fn test_little_straight_as_big_straight() {
assert score(Category.big_straight, [u8(1), 2, 3, 4, 5]) == 0
}

fn test_four_in_order_but_not_a_little_straight() {
assert score(Category.little_straight, [u8(1), 1, 2, 3, 4]) == 0
}

fn test_no_pairs_but_not_a_little_straight() {
assert score(Category.little_straight, [u8(1), 2, 3, 4, 6]) == 0
}

fn test_minimum_is_1_maximum_is_5_but_not_a_little_straight() {
assert score(Category.little_straight, [u8(1), 1, 3, 4, 5]) == 0
}

fn test_big_straight() {
assert score(Category.big_straight, [u8(4), 6, 2, 5, 3]) == 30
}

fn test_big_straight_as_little_straight() {
assert score(Category.little_straight, [u8(6), 5, 4, 3, 2]) == 0
}

fn test_no_pairs_but_not_a_big_straight() {
assert score(Category.big_straight, [u8(6), 5, 4, 3, 1]) == 0
}

fn test_choice() {
assert score(Category.choice, [u8(3), 3, 5, 6, 6]) == 23
}

fn test_yacht_as_choice() {
assert score(Category.choice, [u8(2), 2, 2, 2, 2]) == 10
}
Loading

0 comments on commit b1a8bf7

Please sign in to comment.