aboutsummaryrefslogtreecommitdiff
path: root/aoc2023
diff options
context:
space:
mode:
authorHJ <thechairman@thechairman.info>2023-12-16 10:42:12 -0500
committerHJ <thechairman@thechairman.info>2023-12-16 10:42:12 -0500
commit66ef0cabda52a27ebd52411f6d1fa1b8ac0ec9da (patch)
tree87e8b8c2e0f8fff554dc6c08ea3749902a7edcae /aoc2023
parentc92cd5ba942286f90a75c4121e0035e078def2fe (diff)
downloadgleam_aoc-66ef0cabda52a27ebd52411f6d1fa1b8ac0ec9da.tar.gz
gleam_aoc-66ef0cabda52a27ebd52411f6d1fa1b8ac0ec9da.zip
day 16 complete
Diffstat (limited to 'aoc2023')
-rw-r--r--aoc2023/src/day16/.gitignore1
-rw-r--r--aoc2023/src/day16/solve.gleam119
-rw-r--r--aoc2023/src/utilities/array2d.gleam8
-rw-r--r--aoc2023/test/day16/day16_test.gleam66
4 files changed, 190 insertions, 4 deletions
diff --git a/aoc2023/src/day16/.gitignore b/aoc2023/src/day16/.gitignore
new file mode 100644
index 0000000..ae40cea
--- /dev/null
+++ b/aoc2023/src/day16/.gitignore
@@ -0,0 +1 @@
+input.txt \ No newline at end of file
diff --git a/aoc2023/src/day16/solve.gleam b/aoc2023/src/day16/solve.gleam
new file mode 100644
index 0000000..65ce36b
--- /dev/null
+++ b/aoc2023/src/day16/solve.gleam
@@ -0,0 +1,119 @@
+import adglent.{First, Second}
+import gleam/bool
+import gleam/dict.{type Dict}
+import gleam/io
+import gleam/list
+import gleam/result
+import gleam/set.{type Set}
+import utilities/array2d.{type Posn, Posn}
+
+type Direction {
+ Up
+ Right
+ Down
+ Left
+}
+
+type Light {
+ Light(posn: Posn, dir: Direction)
+}
+
+fn move(l: Light) -> Light {
+ let Light(p, dir) = l
+ case dir {
+ Up -> Light(..l, posn: Posn(..p, r: p.r - 1))
+ Down -> Light(..l, posn: Posn(..p, r: p.r + 1))
+ Left -> Light(..l, posn: Posn(..p, c: p.c - 1))
+ Right -> Light(..l, posn: Posn(..p, c: p.c + 1))
+ }
+}
+
+fn transform(l: Light, cell: Result(String, Nil)) -> List(Light) {
+ use <- bool.guard(result.is_error(cell), [])
+ let assert Ok(c) = cell
+ let Light(p, dir) = l
+ case dir, c {
+ // no change
+ _, "." | Up, "|" | Down, "|" | Left, "-" | Right, "-" -> [l]
+ // diagonal mirrors
+ Left, "/" -> [Light(p, Down)]
+ Down, "/" -> [Light(p, Left)]
+ Right, "/" -> [Light(p, Up)]
+ Up, "/" -> [Light(p, Right)]
+ Left, "\\" -> [Light(p, Up)]
+ Up, "\\" -> [Light(p, Left)]
+ Right, "\\" -> [Light(p, Down)]
+ Down, "\\" -> [Light(p, Right)]
+ // splitters
+ Left, "|" | Right, "|" -> [Light(p, Up), Light(p, Down)]
+ Up, "-" | Down, "-" -> [Light(p, Left), Light(p, Right)]
+ _, _ -> panic as "unrecognized cell type"
+ }
+}
+
+fn energize(lights: List(Light), visited: Set(Light), grid: Dict(Posn, String)) {
+ let next_positions =
+ lights
+ |> list.flat_map(fn(l) {
+ let next = move(l)
+ transform(next, dict.get(grid, next.posn))
+ })
+ |> list.filter(fn(l) { !set.contains(visited, l) })
+ let all_visited = set.union(set.from_list(next_positions), visited)
+ case visited == all_visited {
+ True ->
+ set.fold(visited, set.new(), fn(acc, l) { set.insert(acc, l.posn) })
+ |> set.to_list
+ |> list.length
+ False -> energize(next_positions, all_visited, grid)
+ }
+}
+
+pub fn part1(input: String) {
+ let grid = array2d.parse_grid(input)
+
+ [Light(Posn(0, -1), Right)]
+ |> energize(set.new(), grid)
+}
+
+pub fn part2(input: String) {
+ let grid = array2d.parse_grid(input)
+
+ let Posn(rows, cols) = {
+ use acc, p <- list.fold(dict.keys(grid), Posn(0, 0))
+ case acc.r + acc.c > p.r + p.c {
+ True -> acc
+ False -> p
+ }
+ }
+
+ let all_starts =
+ list.concat([
+ list.map(list.range(0, rows), fn(r) { Light(Posn(r, -1), Right) }),
+ list.map(list.range(0, rows), fn(r) { Light(Posn(r, cols + 1), Left) }),
+ list.map(list.range(0, cols), fn(c) { Light(Posn(-1, c), Down) }),
+ list.map(list.range(0, cols), fn(c) { Light(Posn(rows + 1, c), Up) }),
+ ])
+
+ use acc, p <- list.fold(all_starts, 0)
+ let energized = energize([p], set.new(), grid)
+ case acc > energized {
+ True -> acc
+ False -> energized
+ }
+}
+
+pub fn main() {
+ let assert Ok(part) = adglent.get_part()
+ let assert Ok(input) = adglent.get_input("16")
+ case part {
+ First ->
+ part1(input)
+ |> adglent.inspect
+ |> io.println
+ Second ->
+ part2(input)
+ |> adglent.inspect
+ |> io.println
+ }
+}
diff --git a/aoc2023/src/utilities/array2d.gleam b/aoc2023/src/utilities/array2d.gleam
index 1ad824c..d7d05f8 100644
--- a/aoc2023/src/utilities/array2d.gleam
+++ b/aoc2023/src/utilities/array2d.gleam
@@ -3,7 +3,7 @@ import gleam/dict.{type Dict}
import gleam/string
pub type Posn {
- Posn(x: Int, y: Int)
+ Posn(r: Int, c: Int)
}
pub type Array2D(a) =
@@ -11,9 +11,9 @@ pub type Array2D(a) =
pub fn to_2d_array(xss: List(List(a))) -> Array2D(a) {
{
- use x, row <- list.index_map(xss)
- use y, cell <- list.index_map(row)
- #(Posn(x, y), cell)
+ use r, row <- list.index_map(xss)
+ use c, cell <- list.index_map(row)
+ #(Posn(r, c), cell)
}
|> list.flatten
|> dict.from_list
diff --git a/aoc2023/test/day16/day16_test.gleam b/aoc2023/test/day16/day16_test.gleam
new file mode 100644
index 0000000..036504e
--- /dev/null
+++ b/aoc2023/test/day16/day16_test.gleam
@@ -0,0 +1,66 @@
+import gleam/list
+import showtime/tests/should
+import adglent.{type Example, Example}
+import day16/solve
+
+type Problem1AnswerType =
+ Int
+
+type Problem2AnswerType =
+ Int
+
+/// Add examples for part 1 here:
+/// ```gleam
+///const part1_examples: List(Example(Problem1AnswerType)) = [Example("some input", "")]
+/// ```
+const part1_examples: List(Example(Problem1AnswerType)) = [
+ Example(
+ ".|...\\....
+|.-.\\.....
+.....|-...
+........|.
+..........
+.........\\
+..../.\\\\..
+.-.-/..|..
+.|....-|.\\
+..//.|....",
+ 46,
+ ),
+]
+
+/// Add examples for part 2 here:
+/// ```gleam
+///const part2_examples: List(Example(Problem2AnswerType)) = [Example("some input", "")]
+/// ```
+const part2_examples: List(Example(Problem2AnswerType)) = [
+ Example(
+ ".|...\\....
+|.-.\\.....
+.....|-...
+........|.
+..........
+.........\\
+..../.\\\\..
+.-.-/..|..
+.|....-|.\\
+..//.|....",
+ 51,
+ ),
+]
+
+pub fn part1_test() {
+ part1_examples
+ |> should.not_equal([])
+ use example <- list.map(part1_examples)
+ solve.part1(example.input)
+ |> should.equal(example.answer)
+}
+
+pub fn part2_test() {
+ part2_examples
+ |> should.not_equal([])
+ use example <- list.map(part2_examples)
+ solve.part2(example.input)
+ |> should.equal(example.answer)
+}