diff options
author | Tomasz Chojnacki <tomaszchojnacki2001@gmail.com> | 2023-05-14 23:47:20 +0200 |
---|---|---|
committer | Tomasz Chojnacki <tomaszchojnacki2001@gmail.com> | 2023-05-14 23:47:20 +0200 |
commit | a7ce7a1d80a811a9e086d506bc877e2bd9467e36 (patch) | |
tree | 3eb7c2efdf569e9bf9a2d2c12709b5e80ba2777c /aoc-2020-gleam/src/util | |
parent | 1c23ee26b48e2536ce059ae23a22814071fc6de2 (diff) | |
download | gleam_aoc2020-a7ce7a1d80a811a9e086d506bc877e2bd9467e36.tar.gz gleam_aoc2020-a7ce7a1d80a811a9e086d506bc877e2bd9467e36.zip |
Finish day 17, rename Pos
Diffstat (limited to 'aoc-2020-gleam/src/util')
-rw-r--r-- | aoc-2020-gleam/src/util/dir.gleam | 4 | ||||
-rw-r--r-- | aoc-2020-gleam/src/util/pos.gleam | 47 | ||||
-rw-r--r-- | aoc-2020-gleam/src/util/pos2.gleam | 48 | ||||
-rw-r--r-- | aoc-2020-gleam/src/util/pos3.gleam | 30 | ||||
-rw-r--r-- | aoc-2020-gleam/src/util/pos4.gleam | 31 |
5 files changed, 111 insertions, 49 deletions
diff --git a/aoc-2020-gleam/src/util/dir.gleam b/aoc-2020-gleam/src/util/dir.gleam index 6f637d9..194d712 100644 --- a/aoc-2020-gleam/src/util/dir.gleam +++ b/aoc-2020-gleam/src/util/dir.gleam @@ -2,7 +2,7 @@ import gleam/int import gleam/iterator as iter import ext/resultx as resx import ext/iteratorx as iterx -import util/pos.{Pos} +import util/pos2.{Pos2} pub type Dir { North @@ -11,7 +11,7 @@ pub type Dir { West } -pub fn offset(direction: Dir) -> Pos { +pub fn offset(direction: Dir) -> Pos2 { case direction { North -> #(0, 1) East -> #(1, 0) diff --git a/aoc-2020-gleam/src/util/pos.gleam b/aoc-2020-gleam/src/util/pos.gleam deleted file mode 100644 index dd3d01d..0000000 --- a/aoc-2020-gleam/src/util/pos.gleam +++ /dev/null @@ -1,47 +0,0 @@ -import gleam/int -import gleam/list -import gleam/set.{Set} - -pub type Pos = - #(Int, Int) - -pub const zero = #(0, 0) - -pub const directions8 = [ - #(1, 0), - #(1, 1), - #(0, 1), - #(-1, 1), - #(-1, 0), - #(-1, -1), - #(0, -1), - #(1, -1), -] - -pub fn add(p1: Pos, p2: Pos) -> Pos { - #(p1.0 + p2.0, p1.1 + p2.1) -} - -pub fn sub(p1: Pos, p2: Pos) -> Pos { - #(p1.0 - p2.0, p1.1 - p2.1) -} - -pub fn mul(p: Pos, by scalar: Int) -> Pos { - #(p.0 * scalar, p.1 * scalar) -} - -pub fn neighbours8(p: Pos) -> Set(Pos) { - directions8 - |> list.map(with: add(p, _)) - |> set.from_list -} - -pub fn manhattan_dist(from p1: Pos, to p2: Pos) -> Int { - int.absolute_value(p1.0 - p2.0) + int.absolute_value(p1.1 - p2.1) -} - -pub fn rotate_around_origin(this p: Pos, by times: Int) -> Pos { - let assert Ok(sin) = list.at([0, -1, 0, 1], times) - let assert Ok(cos) = list.at([1, 0, -1, 0], times) - #(p.0 * cos - p.1 * sin, p.0 * sin + p.1 * cos) -} diff --git a/aoc-2020-gleam/src/util/pos2.gleam b/aoc-2020-gleam/src/util/pos2.gleam new file mode 100644 index 0000000..4de2ab9 --- /dev/null +++ b/aoc-2020-gleam/src/util/pos2.gleam @@ -0,0 +1,48 @@ +import gleam/int +import gleam/bool +import gleam/list +import gleam/set.{Set} + +pub type Pos2 = + #(Int, Int) + +pub const zero = #(0, 0) + +pub fn directions8() -> 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] + }) +} + +pub fn add(p1: Pos2, p2: Pos2) -> Pos2 { + #(p1.0 + p2.0, p1.1 + p2.1) +} + +pub fn sub(p1: Pos2, p2: Pos2) -> Pos2 { + #(p1.0 - p2.0, p1.1 - p2.1) +} + +pub fn mul(p: Pos2, by scalar: Int) -> Pos2 { + #(p.0 * scalar, p.1 * scalar) +} + +pub fn neighbours8(p: Pos2) -> Set(Pos2) { + directions8() + |> set.to_list + |> list.map(with: add(p, _)) + |> set.from_list +} + +pub fn manhattan_dist(from p1: Pos2, to p2: Pos2) -> Int { + int.absolute_value(p1.0 - p2.0) + int.absolute_value(p1.1 - p2.1) +} + +pub fn rotate_around_origin(this p: Pos2, by times: Int) -> Pos2 { + let assert Ok(sin) = list.at([0, -1, 0, 1], times) + let assert Ok(cos) = list.at([1, 0, -1, 0], times) + #(p.0 * cos - p.1 * sin, p.0 * sin + p.1 * cos) +} 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 +} diff --git a/aoc-2020-gleam/src/util/pos4.gleam b/aoc-2020-gleam/src/util/pos4.gleam new file mode 100644 index 0000000..3eda4c5 --- /dev/null +++ b/aoc-2020-gleam/src/util/pos4.gleam @@ -0,0 +1,31 @@ +import gleam/list +import gleam/bool +import gleam/set.{Set} + +pub type Pos4 = + #(Int, Int, Int, Int) + +pub const zero = #(0, 0, 0, 0) + +fn directions80() -> Set(Pos4) { + 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]) + use w <- list.flat_map(over: [-1, 0, 1]) + let pos = #(x, y, z, w) + use <- bool.guard(when: pos == zero, return: []) + [pos] + }) +} + +pub fn add(p1: Pos4, p2: Pos4) -> Pos4 { + #(p1.0 + p2.0, p1.1 + p2.1, p1.2 + p2.2, p1.3 + p2.3) +} + +pub fn neighbours80(p: Pos4) -> Set(Pos4) { + directions80() + |> set.to_list + |> list.map(with: add(p, _)) + |> set.from_list +} |