Skip to content

Flower Field

Write the count of adjacent flowers into each empty square of a garden.

Adapted from Exercism’s problem specification (MIT). Listed under Grids & Matrices.

// FILE: flower_field.nomi
//# Annotates a flower field with the number of flowers next to each empty
//# square.

once neighbours = [(-1, -1), (-1, 0), (-1, 1), (0, -1), (0, 1), (1, -1), (1, 0), (1, 1)]

/// Writes the count of adjacent flowers into each empty square of a board of
/// `*` flowers and ` ` spaces. A square next to no flower stays a space.
pub fn annotate(garden: List<String>) -> List<String>
    flowers =
        garden
        |> Iter.with_index()
        |> Iter.flat_map(fn (row, line) -> flowers_in_row(row, line))
        |> Iter.to_set()

    garden
    |> Iter.with_index()
    |> Iter.map(fn (row, line) ->
        line
        |> Iter.with_index()
        |> Iter.map(fn (column, square) -> annotate_square(flowers, (row, column), square))
        |> String.join()
    end)
    |> Iter.to_list()
end

fn flowers_in_row(row: Int, line: String) -> Iter<(Int, Int)>
    line
    |> Iter.with_index()
    |> Iter.filter(fn (_column, square) -> square == "*")
    |> Iter.map(fn (column, _square) -> (row, column))
end

fn annotate_square(flowers: Set<(Int, Int)>, (row, column): (Int, Int), square: String) -> String
    if square == "*" then return square

    count = Iter.count_by(neighbours, fn (dr, dc) -> (row + dr, column + dc) in flowers)

    if count == 0 then " " else Int.to_string(count)
end

// FILE: flower_field_test.nomi
import flower_field

test "no rows"
    assert flower_field.annotate([]) == []
end

test "no columns"
    garden = [""]

    assert flower_field.annotate(garden) == [""]
end

test "no flowers"
    garden = [
        "   ",
        "   ",
        "   ",
    ]

    assert flower_field.annotate(garden) == [
        "   ",
        "   ",
        "   ",
    ]
end

test "garden full of flowers"
    garden = [
        "***",
        "***",
        "***",
    ]

    assert flower_field.annotate(garden) == [
        "***",
        "***",
        "***",
    ]
end

test "flower surrounded by spaces"
    garden = [
        "   ",
        " * ",
        "   ",
    ]

    assert flower_field.annotate(garden) == [
        "111",
        "1*1",
        "111",
    ]
end

test "space surrounded by flowers"
    garden = [
        "***",
        "* *",
        "***",
    ]

    assert flower_field.annotate(garden) == [
        "***",
        "*8*",
        "***",
    ]
end

test "horizontal line"
    garden = [" * * "]

    assert flower_field.annotate(garden) == ["1*2*1"]
end

test "horizontal line, flowers at edges"
    garden = ["*   *"]

    assert flower_field.annotate(garden) == ["*1 1*"]
end

test "vertical line"
    garden = [
        " ",
        "*",
        " ",
        "*",
        " ",
    ]

    assert flower_field.annotate(garden) == [
        "1",
        "*",
        "2",
        "*",
        "1",
    ]
end

test "vertical line, flowers at edges"
    garden = [
        "*",
        " ",
        " ",
        " ",
        "*",
    ]

    assert flower_field.annotate(garden) == [
        "*",
        "1",
        " ",
        "1",
        "*",
    ]
end

test "cross"
    garden = [
        "  *  ",
        "  *  ",
        "*****",
        "  *  ",
        "  *  ",
    ]

    assert flower_field.annotate(garden) == [
        " 2*2 ",
        "25*52",
        "*****",
        "25*52",
        " 2*2 ",
    ]
end

test "large garden"
    garden = [
        " *  * ",
        "  *   ",
        "    * ",
        "   * *",
        " *  * ",
        "      ",
    ]

    assert flower_field.annotate(garden) == [
        "1*22*1",
        "12*322",
        " 123*2",
        "112*4*",
        "1*22*2",
        "111111",
    ]
end

test "multiple adjacent flowers"
    garden = [" ** "]

    assert flower_field.annotate(garden) == ["1**1"]
end
  • The garden stays List<String> in and out. Flowers are collected once into a Set<(Int, Int)> of coordinates, so counting neighbours is eight set lookups and there is no grid indexing or bounds check.
  • Rows are iterated as strings of graphemes (Iter over String), comparing squares with "*", not as codepoints; style §20 keeps text in strings.
  • The eight offsets are a once literal rather than generated from -1..=1 twice with the centre removed.
  • Each canonical case is its own test, with the garden bound to garden before the assertion.
  • Every garden of more than one row, input and expected output, is written one row per line. nomi fmt keeps a list broken after its [ that way even when it would fit on one line.