aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--aoc2023-other/day-16/day-16.rkt70
-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
5 files changed, 260 insertions, 4 deletions
diff --git a/aoc2023-other/day-16/day-16.rkt b/aoc2023-other/day-16/day-16.rkt
new file mode 100644
index 0000000..4a70de8
--- /dev/null
+++ b/aoc2023-other/day-16/day-16.rkt
@@ -0,0 +1,70 @@
+#lang racket
+
+(require advent-of-code
+ threading)
+
+(struct posn (r c) #:transparent)
+(struct light (posn dir) #:transparent)
+
+(define input (fetch-aoc-input (find-session) 2023 16 #:cache #true))
+
+(define grid
+ (for*/hash ([(row r) (in-indexed (string-split input "\n"))]
+ [(cell c) (in-indexed (in-string row))])
+ (values (posn r c) cell)))
+
+(define/match (move _l)
+ [((light (posn r c) 'up)) (light (posn (sub1 r) c) 'up)]
+ [((light (posn r c) 'right)) (light (posn r (add1 c)) 'right)]
+ [((light (posn r c) 'left)) (light (posn r (sub1 c)) 'left)]
+ [((light (posn r c) 'down)) (light (posn (add1 r) c) 'down)])
+
+(define/match (transform l _cell)
+ [(_ #\.) l]
+ [(_ 'off) '()]
+
+ [((light _ (or 'up 'down)) #\|) l]
+ [((light _ (or 'left 'right)) #\-) l]
+
+ [((light p 'left) #\/) (light p 'down)]
+ [((light p 'down) #\/) (light p 'left)]
+ [((light p 'right) #\/) (light p 'up)]
+ [((light p 'up) #\/) (light p 'right)]
+
+ [((light p 'left) #\\) (light p 'up)]
+ [((light p 'up) #\\) (light p 'left)]
+ [((light p 'right) #\\) (light p 'down)]
+ [((light p 'down) #\\) (light p 'right)]
+
+ [((light p (or 'left 'right)) #\|) (list (light p 'up) (light p 'down))]
+ [((light p (or 'up 'down)) #\-) (list (light p 'left) (light p 'right))])
+
+;; part 1
+(define (get-energized start)
+ (for/fold ([energized (set)]
+ [previously-visited (set)]
+ [beam-tips (set start)]
+ #:result (set-count energized))
+ ([_ (in-naturals)])
+ (define next-positions
+ (list->set
+ (flatten (for/list ([b (in-set beam-tips)])
+ (~> b move ((λ (b) (transform b (hash-ref grid (light-posn b) 'off)))))))))
+ (define all-visited (set-union previously-visited next-positions))
+ (define next-energized (set-union energized (list->set (set-map next-positions light-posn))))
+ #:break (equal? previously-visited all-visited)
+ (values next-energized all-visited (set-subtract next-positions previously-visited))))
+
+(get-energized (light (posn 0 -1) 'right))
+
+;; part 2
+(define rows (~> input (string-split "\n") length))
+(define cols (~> input (string-split #rx"\n.*") first string-length))
+
+(define all-starting-positions
+ (append (map (λ (r) (light (posn r -1) 'right)) (range rows))
+ (map (λ (r) (light (posn r cols) 'left)) (range rows))
+ (map (λ (c) (light (posn -1 c) 'down)) (range cols))
+ (map (λ (c) (light (posn rows c) 'up)) (range cols))))
+
+(get-energized (argmax get-energized all-starting-positions)) \ No newline at end of file
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)
+}