diff options
Diffstat (limited to 'aoc-2020-gleam/src/util/pos2.gleam')
-rw-r--r-- | aoc-2020-gleam/src/util/pos2.gleam | 21 |
1 files changed, 10 insertions, 11 deletions
diff --git a/aoc-2020-gleam/src/util/pos2.gleam b/aoc-2020-gleam/src/util/pos2.gleam index 3f478ac..976088e 100644 --- a/aoc-2020-gleam/src/util/pos2.gleam +++ b/aoc-2020-gleam/src/util/pos2.gleam @@ -1,7 +1,7 @@ import gleam/int -import gleam/bool import gleam/list import gleam/set.{type Set} +import ext/setx pub type Pos2 = #(Int, Int) @@ -16,16 +16,18 @@ pub fn y(pos: Pos2) -> Int { pos.1 } -pub fn directions8() -> Set(Pos2) { +fn directions9() -> Set(Pos2) { set.from_list({ - use x <- list.flat_map(over: [-1, 0, 1]) - use y <- list.flat_map(over: [-1, 0, 1]) - let pos = #(x, y) - use <- bool.guard(when: pos == zero, return: []) - [pos] + use x <- list.flat_map([-1, 0, 1]) + use y <- list.map([-1, 0, 1]) + #(x, y) }) } +pub fn directions8() -> Set(Pos2) { + set.delete(from: directions9(), this: zero) +} + pub fn add(p1: Pos2, p2: Pos2) -> Pos2 { #(p1.0 + p2.0, p1.1 + p2.1) } @@ -39,10 +41,7 @@ pub fn mul(p: Pos2, by scalar: Int) -> Pos2 { } pub fn neighbours8(p: Pos2) -> Set(Pos2) { - directions8() - |> set.to_list - |> list.map(with: add(p, _)) - |> set.from_list + setx.map(directions8(), with: add(p, _)) } pub fn manhattan_dist(from p1: Pos2, to p2: Pos2) -> Int { |