aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorH.J <thechairman@thechairman.info>2024-06-14 17:14:05 -0400
committerH.J <thechairman@thechairman.info>2024-06-14 17:14:05 -0400
commit29d7ae5048297a754b157a45ad65e7bbc18dc977 (patch)
tree8dfedba7ea6d32743d6b662e31ada52a37f602b4
parentb338ab2601f7fb10a3500a0bf8e5ba53c1f228e9 (diff)
downloadgleam_aoc-29d7ae5048297a754b157a45ad65e7bbc18dc977.tar.gz
gleam_aoc-29d7ae5048297a754b157a45ad65e7bbc18dc977.zip
gleam 2017 day 12 started
-rw-r--r--aoc2017-gleam/src/aoc_2017/day_11.gleam76
-rw-r--r--aoc2017-gleam/src/aoc_2017/day_12.gleam29
2 files changed, 101 insertions, 4 deletions
diff --git a/aoc2017-gleam/src/aoc_2017/day_11.gleam b/aoc2017-gleam/src/aoc_2017/day_11.gleam
index a0a1145..7d3df0b 100644
--- a/aoc2017-gleam/src/aoc_2017/day_11.gleam
+++ b/aoc2017-gleam/src/aoc_2017/day_11.gleam
@@ -1,7 +1,75 @@
-pub fn pt_1(input: String) {
- todo as "part 1 not implemented"
+import gleam/int
+import gleam/list
+import gleam/string
+
+pub type Direction {
+ North
+ Northeast
+ Northwest
+ South
+ Southeast
+ Southwest
+}
+
+type HexPosition {
+ HexPosition(x: Int, y: Int, z: Int)
+}
+
+const start = HexPosition(0, 0, 0)
+
+fn to_direction(str: String) -> Direction {
+ case str {
+ "n" -> North
+ "ne" -> Northeast
+ "nw" -> Northwest
+ "s" -> South
+ "se" -> Southeast
+ "sw" -> Southwest
+ _ -> panic as "unrecognized direction"
+ }
+}
+
+fn distance(hp1: HexPosition, hp2: HexPosition) -> Int {
+ {
+ int.absolute_value(hp1.x - hp2.x)
+ + int.absolute_value(hp1.y - hp2.y)
+ + int.absolute_value(hp1.z - hp2.z)
+ }
+ / 2
+}
+
+fn move(p, direction) -> HexPosition {
+ case direction {
+ North -> HexPosition(..p, y: p.y + 1, z: p.z - 1)
+ South -> HexPosition(..p, y: p.y - 1, z: p.z + 1)
+ Northeast -> HexPosition(..p, x: p.x + 1, z: p.z - 1)
+ Southwest -> HexPosition(..p, x: p.x - 1, z: p.z + 1)
+ Southeast -> HexPosition(..p, x: p.x + 1, y: p.y - 1)
+ Northwest -> HexPosition(..p, x: p.x - 1, y: p.y + 1)
+ }
+}
+
+pub fn parse(input: String) -> List(Direction) {
+ input
+ |> string.split(",")
+ |> list.map(to_direction)
+}
+
+pub fn pt_1(input: List(Direction)) {
+ do_walk(input, start, 0).0
+}
+
+pub fn pt_2(input: List(Direction)) {
+ do_walk(input, start, 0).1
}
-pub fn pt_2(input: String) {
- todo as "part 2 not implemented"
+fn do_walk(steps, position, max) {
+ case steps {
+ [] -> #(distance(position, HexPosition(0, 0, 0)), max)
+ [next, ..rest] -> {
+ let new_position = move(position, next)
+ let new_max = int.max(max, distance(new_position, start))
+ do_walk(rest, new_position, new_max)
+ }
+ }
}
diff --git a/aoc2017-gleam/src/aoc_2017/day_12.gleam b/aoc2017-gleam/src/aoc_2017/day_12.gleam
new file mode 100644
index 0000000..87747d0
--- /dev/null
+++ b/aoc2017-gleam/src/aoc_2017/day_12.gleam
@@ -0,0 +1,29 @@
+import gleam/dict.{type Dict}
+import gleam/int
+import gleam/list
+import gleam/result
+import gleam/string
+
+type Pipes =
+ Dict(Int, List(Int))
+
+pub fn parse(input: String) -> Pipes {
+ {
+ use row <- list.map(string.split(input, "\n"))
+ let assert Ok(#(from, to)) = string.split_once(row, " <-> ")
+
+ let assert Ok(from) = int.parse(from)
+ let to = to |> string.split(", ") |> list.map(int.parse) |> result.values
+
+ #(from, to)
+ }
+ |> dict.from_list
+}
+
+pub fn pt_1(input: Pipes) {
+ input
+}
+
+pub fn pt_2(input: Pipes) {
+ todo as "part 2 not implemented"
+}