From a7ce7a1d80a811a9e086d506bc877e2bd9467e36 Mon Sep 17 00:00:00 2001 From: Tomasz Chojnacki Date: Sun, 14 May 2023 23:47:20 +0200 Subject: Finish day 17, rename Pos --- aoc-2020-gleam/src/util/pos3.gleam | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 aoc-2020-gleam/src/util/pos3.gleam (limited to 'aoc-2020-gleam/src/util/pos3.gleam') diff --git a/aoc-2020-gleam/src/util/pos3.gleam b/aoc-2020-gleam/src/util/pos3.gleam new file mode 100644 index 0000000..5525607 --- /dev/null +++ b/aoc-2020-gleam/src/util/pos3.gleam @@ -0,0 +1,30 @@ +import gleam/list +import gleam/bool +import gleam/set.{Set} + +pub type Pos3 = + #(Int, Int, Int) + +pub const zero = #(0, 0, 0) + +fn directions26() -> Set(Pos3) { + set.from_list({ + use x <- list.flat_map(over: [-1, 0, 1]) + use y <- list.flat_map(over: [-1, 0, 1]) + use z <- list.flat_map(over: [-1, 0, 1]) + let pos = #(x, y, z) + use <- bool.guard(when: pos == zero, return: []) + [pos] + }) +} + +pub fn add(p1: Pos3, p2: Pos3) -> Pos3 { + #(p1.0 + p2.0, p1.1 + p2.1, p1.2 + p2.2) +} + +pub fn neighbours26(p: Pos3) -> Set(Pos3) { + directions26() + |> set.to_list + |> list.map(with: add(p, _)) + |> set.from_list +} -- cgit v1.2.3