Skip to content

Yacht

Score a throw of five dice in one of the twelve categories of the dice game Yacht.

Adapted from Exercism’s problem specification (MIT). Listed under Games & Simulation.

// FILE: yacht.nomi
//# Scores a throw of five dice in the dice game Yacht.

/// The twelve ways to score a throw of five dice.
pub enum Category
    Ones
    Twos
    Threes
    Fours
    Fives
    Sixes
    FullHouse
    FourOfAKind
    LittleStraight
    BigStraight
    Choice
    Yacht
end

/// The score of five `dice` in `category`: 0 when they do not make it.
pub fn score(dice: List<Int>, category: Category) -> Int
    case category
        .Ones           -> face_total(dice, 1)
        .Twos           -> face_total(dice, 2)
        .Threes         -> face_total(dice, 3)
        .Fours          -> face_total(dice, 4)
        .Fives          -> face_total(dice, 5)
        .Sixes          -> face_total(dice, 6)
        .FullHouse      -> full_house(dice)
        .FourOfAKind    -> four_of_a_kind(dice)
        .LittleStraight -> straight(dice, from: 1)
        .BigStraight    -> straight(dice, from: 2)
        .Choice         -> Iter.sum(dice)
        .Yacht          -> yacht(dice)
    end
end

/// The total of the dice showing `face`.
fn face_total(dice: List<Int>, face: Int) -> Int
    count = Iter.count_by(dice, &(&1 == face))
    face * count
end

/// All the dice when three show one face and two another, or 0.
fn full_house(dice: List<Int>) -> Int
    if group_sizes(dice) == [2, 3] then Iter.sum(dice) else 0
end

/// Four times the face that at least four dice show, or 0.
fn four_of_a_kind(dice: List<Int>) -> Int
    dice
    |> Iter.frequencies()
    |> Iter.find(fn (_face, count) -> count >= 4)
    |> Maybe.map(fn (face, _count) -> 4 * face)
    |> Maybe.with_default(0)
end

/// 30 when the dice are the five faces counting up `from`, or 0.
fn straight(dice: List<Int>, from: Int) -> Int
    if Iter.sort(dice) == Iter.to_list(from..from + 5) then 30 else 0
end

/// 50 when all five dice show one face, or 0.
fn yacht(dice: List<Int>) -> Int
    if group_sizes(dice) == [5] then 50 else 0
end

/// How many dice show each face that comes up, smallest group first: a full
/// house is `[2, 3]`, a yacht `[5]`.
fn group_sizes(dice: List<Int>) -> List<Int>
    dice
    |> Iter.frequencies()
    |> Map.values()
    |> Iter.sort()
end

// FILE: yacht_test.nomi
import yacht.{self, Category}

test "yacht"
    [
        ("Yacht",                                   [5, 5, 5, 5, 5], Category.Yacht,  50),
        ("Not Yacht",                               [1, 3, 3, 2, 5], .Yacht,          0),
        ("Ones",                                    [1, 1, 1, 3, 5], .Ones,           3),
        ("Ones, out of order",                      [3, 1, 1, 5, 1], .Ones,           3),
        ("No ones",                                 [4, 3, 6, 5, 5], .Ones,           0),
        ("Twos",                                    [2, 3, 4, 5, 6], .Twos,           2),
        ("Fours",                                   [1, 4, 1, 4, 1], .Fours,          8),
        ("Yacht counted as threes",                 [3, 3, 3, 3, 3], .Threes,         15),
        ("Yacht of 3s counted as fives",            [3, 3, 3, 3, 3], .Fives,          0),
        ("Fives",                                   [1, 5, 3, 5, 3], .Fives,          10),
        ("Sixes",                                   [2, 3, 4, 5, 6], .Sixes,          6),
        ("Full house two small, three big",         [2, 2, 4, 4, 4], .FullHouse,      16),
        ("Full house three small, two big",         [5, 3, 3, 5, 3], .FullHouse,      19),
        ("Two pair is not a full house",            [2, 2, 4, 4, 5], .FullHouse,      0),
        ("Four of a kind is not a full house",      [1, 4, 4, 4, 4], .FullHouse,      0),
        ("Yacht is not a full house",               [2, 2, 2, 2, 2], .FullHouse,      0),
        ("Four of a Kind",                          [6, 6, 4, 6, 6], .FourOfAKind,    24),
        ("Yacht can be scored as Four of a Kind",   [3, 3, 3, 3, 3], .FourOfAKind,    12),
        ("Full house is not Four of a Kind",        [3, 3, 3, 5, 5], .FourOfAKind,    0),
        ("Little Straight",                         [3, 5, 4, 1, 2], .LittleStraight, 30),
        ("Little Straight as Big Straight",         [1, 2, 3, 4, 5], .BigStraight,    0),
        ("Four in order but not a little straight", [1, 1, 2, 3, 4], .LittleStraight, 0),
        ("No pairs but not a little straight",      [1, 2, 3, 4, 6], .LittleStraight, 0),
        (
            "Minimum is 1, maximum is 5, but not a little straight",
            [1, 1, 3, 4, 5],
            .LittleStraight,
            0,
        ),
        ("Big Straight",                    [4, 6, 2, 5, 3], .BigStraight,    30),
        ("Big Straight as little straight", [6, 5, 4, 3, 2], .LittleStraight, 0),
        ("No pairs but not a big straight", [6, 5, 4, 3, 1], .BigStraight,    0),
        ("Choice",                          [3, 3, 5, 6, 6], .Choice,         23),
        ("Yacht as choice",                 [2, 2, 2, 2, 2], .Choice,         10),
    ]
    |> subtests (description, dice, category, expected), description
        assert yacht.score(dice, category) == expected
    end
end
  • The categories are an enum, Category, and score is one case over it, so a category without a score is a missing arm. The canonical strings ("full house") are the test table’s data, written as variants: the first row qualifies Category.Yacht and the rows after it are bare.
  • The dice are a List<Int>, as the canonical data gives them.
  • Several categories ask for a shape, not particular faces. group_sizes answers how many dice show each face that comes up, sorted: Iter.frequencies, then the map’s values, then Iter.sort. A full house is [2, 3] and a yacht [5], whatever the faces are.
  • The straights compare the sorted dice with the five faces counting up from 1 or from 2, built as a range, Iter.to_list(from..from + 5); from is a named argument at the call, straight(dice, from: 1).
  • Four of a kind finds the face that four or more dice show with Iter.find over the frequencies, so a yacht scores as four of a kind too. No such face is a real answer, scored 0, so Maybe.with_default(0) is the rule, not a guess.
  • Each category that is made or not is its own function, so every arm of score is one call and stays on one line; an if in an arm breaks past the formatter’s one-line width and takes every arm with it.
  • The 29 canonical cases are one subtests table named by the description column.