diff options
author | Tomasz Chojnacki <tomaszchojnacki2001@gmail.com> | 2023-12-22 18:31:14 +0100 |
---|---|---|
committer | Tomasz Chojnacki <tomaszchojnacki2001@gmail.com> | 2023-12-22 18:31:14 +0100 |
commit | 7a5f1983f9189422ad5e12afde11d11bec30a3f1 (patch) | |
tree | 46a02028e2712beaad7cf0886696ff7cd37798cf /aoc-2020-gleam/src/util | |
parent | d8e183f02f67522d94deafa328e19b3081ca41be (diff) | |
download | gleam_aoc2020-7a5f1983f9189422ad5e12afde11d11bec30a3f1.tar.gz gleam_aoc2020-7a5f1983f9189422ad5e12afde11d11bec30a3f1.zip |
Solve part 1 of day 20
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]) |