Skip to content

Commit 6d274eb

Browse files
authored
Add exercise (#884)
1 parent 182e338 commit 6d274eb

File tree

11 files changed

+470
-0
lines changed

11 files changed

+470
-0
lines changed

config.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -815,6 +815,19 @@
815815
"prerequisites": [],
816816
"difficulty": 3
817817
},
818+
{
819+
"slug": "yacht",
820+
"name": "Yacht",
821+
"uuid": "b7909c33-071f-4d8d-806b-34ffcce5c415",
822+
"practices": [],
823+
"prerequisites": [],
824+
"difficulty": 3,
825+
"topics": [
826+
"higher-order-functions",
827+
"conditionals",
828+
"conditionals-switch"
829+
]
830+
},
818831
{
819832
"slug": "acronym",
820833
"name": "Acronym",
Lines changed: 30 additions & 0 deletions
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.
Lines changed: 11 additions & 0 deletions
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)
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
2+
enum YachtCategory: String {
3+
case ones
4+
case twos
5+
case threes
6+
case fours
7+
case fives
8+
case sixes
9+
case fullHouse = "full house"
10+
case fourOfAKind = "four of a kind"
11+
case littleStraight = "little straight"
12+
case bigStraight = "big straight"
13+
case choice
14+
case yacht
15+
}
16+
17+
func score(_ scores: [Int], category: YachtCategory) -> Int {
18+
switch category {
19+
case .ones: return simple(scores, category: 1)
20+
case .twos: return simple(scores, category: 2)
21+
case .threes: return simple(scores, category: 3)
22+
case .fours: return simple(scores, category: 4)
23+
case .fives: return simple(scores, category: 5)
24+
case .sixes: return simple(scores, category: 6)
25+
case .fullHouse: return fullHouse(scores)
26+
case .fourOfAKind: return fourOfAKind(scores)
27+
case .littleStraight: return littleStraight(scores)
28+
case .bigStraight: return bigStraight(scores)
29+
case .choice: return choice(scores)
30+
case .yacht: return yacht(scores)
31+
}
32+
}
33+
34+
fileprivate func simple(_ scores: [Int], category: Int) -> Int {
35+
return category * scores.count { $0 == category }
36+
}
37+
38+
fileprivate func fullHouse(_ scores: [Int]) -> Int {
39+
let counts = scores.reduce(into: [Int: Int]()) { result, element in
40+
result[element, default: 0] += 1
41+
}
42+
if counts.count == 2 && (counts.values.contains(2) && counts.values.contains(3)) {
43+
return scores.reduce(0, +)
44+
}
45+
return 0
46+
}
47+
48+
fileprivate func fourOfAKind(_ scores: [Int]) -> Int {
49+
let counts = scores.reduce(into: [Int: Int]()) { result, element in
50+
result[element, default: 0] += 1
51+
}
52+
return counts.first { $0.value >= 4 }.map { $0.key * 4 } ?? 0
53+
}
54+
55+
fileprivate func littleStraight(_ scores: [Int]) -> Int {
56+
return Set(scores) == [1, 2, 3, 4, 5] ? 30 : 0
57+
}
58+
59+
fileprivate func bigStraight(_ scores: [Int]) -> Int {
60+
return Set(scores) == [2, 3, 4, 5, 6] ? 30 : 0
61+
}
62+
63+
fileprivate func choice(_ scores: [Int]) -> Int {
64+
return scores.reduce(0, +)
65+
}
66+
67+
fileprivate func yacht(_ scores: [Int]) -> Int {
68+
return scores.allSatisfy { $0 == scores.first } ? 50 : 0
69+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"authors": [
3+
"Sencudra"
4+
],
5+
"files": {
6+
"solution": [
7+
"Sources/Yacht/Yacht.swift"
8+
],
9+
"test": [
10+
"Tests/YachtTests/YachtTests.swift"
11+
],
12+
"example": [
13+
".meta/Sources/Yacht/YachtExample.swift"
14+
],
15+
"editor": [
16+
"Sources/Yacht/YachtCategory.swift"
17+
]
18+
},
19+
"blurb": "Score a single throw of dice in the game Yacht.",
20+
"source": "James Kilfiger, using Wikipedia",
21+
"source_url": "https://en.wikipedia.org/wiki/Yacht_(dice_game)"
22+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import Testing
2+
import Foundation
3+
@testable import {{ exercise|camelCase }}
4+
5+
let RUNALL = Bool(ProcessInfo.processInfo.environment["RUNALL", default: "false"]) ?? false
6+
7+
@Suite struct {{ exercise|camelCase }}Tests {
8+
{% for case in cases %}
9+
{% if forloop.first -%}
10+
@Test("{{case.description}}")
11+
{% else -%}
12+
@Test("{{case.description}}", .enabled(if: RUNALL))
13+
{% endif -%}
14+
func test{{ case.description|camelCase }}() {
15+
#expect({{ case.property }}({{ case.input.dice }}, category: category("{{ case.input.category }}")) == {{case.expected}})
16+
}
17+
{% endfor -%}
18+
}
19+
20+
func category(_ raw: String) -> YachtCategory {
21+
guard let category = YachtCategory(rawValue: raw) else {
22+
#expect(Bool(false), "Invalid test category: \(raw)")
23+
return .ones
24+
}
25+
return category
26+
}
Lines changed: 97 additions & 0 deletions
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"
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// swift-tools-version:6.0
2+
3+
import PackageDescription
4+
5+
let package = Package(
6+
name: "Yacht",
7+
products: [
8+
.library(
9+
name: "Yacht",
10+
targets: ["Yacht"])
11+
],
12+
dependencies: [],
13+
targets: [
14+
.target(
15+
name: "Yacht",
16+
dependencies: []),
17+
.testTarget(
18+
name: "YachtTests",
19+
dependencies: ["Yacht"]),
20+
]
21+
)
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2+
func score(_ scores: [Int], category: YachtCategory) -> Int {
3+
// Write your code for the 'Yacht' exercise in this file.
4+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
2+
enum YachtCategory: String {
3+
case ones
4+
case twos
5+
case threes
6+
case fours
7+
case fives
8+
case sixes
9+
case fullHouse = "full house"
10+
case fourOfAKind = "four of a kind"
11+
case littleStraight = "little straight"
12+
case bigStraight = "big straight"
13+
case choice
14+
case yacht
15+
}

0 commit comments

Comments
 (0)