diff options
author | Tomasz Chojnacki <tomaszchojnacki2001@gmail.com> | 2023-12-23 13:33:34 +0100 |
---|---|---|
committer | Tomasz Chojnacki <tomaszchojnacki2001@gmail.com> | 2023-12-23 13:33:34 +0100 |
commit | 8bafdaf384fd144a61a7d23661a6cc8d56b5d810 (patch) | |
tree | fe8eb9c334c86056bebeb3c6cba6f9069a472f09 /aoc-2020-gleam/src/util/hex.gleam | |
parent | 5aa24ac572ab5451e187f9accfa6833634c58215 (diff) | |
download | gleam_aoc2020-8bafdaf384fd144a61a7d23661a6cc8d56b5d810.tar.gz gleam_aoc2020-8bafdaf384fd144a61a7d23661a6cc8d56b5d810.zip |
Finish day 24
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, _)) +} |