Skip to content

Commit

Permalink
Convert minefield to List (List U8) upfront
Browse files Browse the repository at this point in the history
  • Loading branch information
ageron committed Sep 2, 2024
1 parent d5f7117 commit 9d7d41e
Showing 1 changed file with 8 additions and 10 deletions.
18 changes: 8 additions & 10 deletions exercises/practice/minesweeper/.meta/Example.roc
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
module [annotate]

isBomb : List Str, I64, I64 -> Result Bool [OutOfBounds]
isBomb : List (List U8), I64, I64 -> Result Bool [OutOfBounds]
isBomb = \rows, nx, ny ->
y = Num.toU64Checked? ny
x = Num.toU64Checked? nx
y = Num.toU64Checked? ny
rows
|> List.get? y
|> Str.toUtf8
|> List.get? x
|> Bool.isEq '*'
|> Ok
|> List.get? y
|> List.get? x
|> Bool.isEq '*'
|> Ok

countNeighbors : List Str, U64, U64 -> Num *
countNeighbors : List (List U8), U64, U64 -> Num *
countNeighbors = \rows, x, y ->
[-1, 0, 1]
|> List.map \dy ->
Expand All @@ -25,12 +24,11 @@ countNeighbors = \rows, x, y ->

annotate : Str -> Str
annotate = \minefield ->
rows = minefield |> Str.split "\n"
rows = minefield |> Str.split "\n" |> List.map Str.toUtf8
annotated =
rows
|> List.mapWithIndex \row, y ->
row
|> Str.toUtf8
|> List.mapWithIndex \cell, x ->
if cell == '*' then
'*'
Expand Down

0 comments on commit 9d7d41e

Please sign in to comment.