diff options
Diffstat (limited to 'aoc-2020-gleam/src/util/hex.gleam')
-rw-r--r-- | aoc-2020-gleam/src/util/hex.gleam | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/aoc-2020-gleam/src/util/hex.gleam b/aoc-2020-gleam/src/util/hex.gleam index 21db192..070b6e4 100644 --- a/aoc-2020-gleam/src/util/hex.gleam +++ b/aoc-2020-gleam/src/util/hex.gleam @@ -1,3 +1,8 @@ +import gleam/bool +import gleam/list +import gleam/set.{type Set} +import ext/setx + pub opaque type Hex { Hex(q: Int, r: Int, s: Int) } @@ -19,3 +24,25 @@ pub const ne = Hex(q: 1, r: -1, s: 0) pub fn add(a: Hex, b: Hex) -> Hex { Hex(q: a.q + b.q, r: a.r + b.r, s: a.s + b.s) } + +fn directions7() -> Set(Hex) { + set.from_list({ + use q <- list.flat_map([-1, 0, 1]) + use r <- list.flat_map([-1, 0, 1]) + use s <- list.flat_map([-1, 0, 1]) + use <- bool.guard(when: q + r + s != 0, return: []) + [Hex(q, r, s)] + }) +} + +fn directions6() -> Set(Hex) { + set.delete(from: directions7(), this: zero) +} + +pub fn neighbours6(h: Hex) -> Set(Hex) { + setx.map(directions6(), with: add(h, _)) +} + +pub fn neighbours7(h: Hex) -> Set(Hex) { + setx.map(directions7(), with: add(h, _)) +} |