From 7a5f1983f9189422ad5e12afde11d11bec30a3f1 Mon Sep 17 00:00:00 2001 From: Tomasz Chojnacki Date: Fri, 22 Dec 2023 18:31:14 +0100 Subject: Solve part 1 of day 20 --- aoc-2020-gleam/src/util/grid.gleam | 22 ++++++++++++++++++++++ aoc-2020-gleam/src/util/pos2.gleam | 8 ++++++++ 2 files changed, 30 insertions(+) create mode 100644 aoc-2020-gleam/src/util/grid.gleam (limited to 'aoc-2020-gleam/src/util') 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]) -- cgit v1.2.3