diff options
Diffstat (limited to 'aoc-2020-gleam/src/util')
-rw-r--r-- | aoc-2020-gleam/src/util/grid.gleam | 22 | ||||
-rw-r--r-- | aoc-2020-gleam/src/util/pos2.gleam | 8 |
2 files changed, 30 insertions, 0 deletions
diff --git a/aoc-2020-gleam/src/util/grid.gleam b/aoc-2020-gleam/src/util/grid.gleam new file mode 100644 index 0000000..a091be5 --- /dev/null +++ b/aoc-2020-gleam/src/util/grid.gleam @@ -0,0 +1,22 @@ +import gleam/list +import gleam/string as str +import gleam/set.{type Set} + +pub fn parse_grid(lines: String, with constructor: fn(Int, Int) -> a) -> Set(a) { + lines + |> str.split("\n") + |> list.index_map(with: fn(line, y) { + line + |> str.to_graphemes + |> list.index_map(with: fn(grapheme, x) { + case grapheme { + "#" -> [constructor(x, y)] + "." -> [] + _ -> panic + } + }) + |> list.flatten + }) + |> list.flatten + |> set.from_list +} diff --git a/aoc-2020-gleam/src/util/pos2.gleam b/aoc-2020-gleam/src/util/pos2.gleam index 0b6256c..3f478ac 100644 --- a/aoc-2020-gleam/src/util/pos2.gleam +++ b/aoc-2020-gleam/src/util/pos2.gleam @@ -8,6 +8,14 @@ pub type Pos2 = pub const zero = #(0, 0) +pub fn x(pos: Pos2) -> Int { + pos.0 +} + +pub fn y(pos: Pos2) -> Int { + pos.1 +} + pub fn directions8() -> Set(Pos2) { set.from_list({ use x <- list.flat_map(over: [-1, 0, 1]) |