Skip to content

Commit

Permalink
day16.clj
Browse files Browse the repository at this point in the history
  • Loading branch information
narimiran committed Dec 16, 2023
1 parent a038217 commit b440311
Show file tree
Hide file tree
Showing 5 changed files with 180 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ Day 00: Helper file | [aoc.clj](clojure/aoc.clj) |
[Day 13](http://adventofcode.com/2023/day/13) | [day13.clj](clojure/day13.clj) | My helper functions to the rescue.
[Day 14](http://adventofcode.com/2023/day/14) | [day14.clj](clojure/day14.clj) | Very slow solution. There must be a better way.
[Day 15](http://adventofcode.com/2023/day/15) | [day15.clj](clojure/day15.clj) | Advent of Reading.
<!-- [Day 16](http://adventofcode.com/2023/day/16) | [day16.clj](clojure/day16.clj) | -->
[Day 16](http://adventofcode.com/2023/day/16) | [day16.clj](clojure/day16.clj) | Is this a cousin of Day 10?
<!-- [Day 17](http://adventofcode.com/2023/day/17) | [day17.clj](clojure/day17.clj) | -->
<!-- [Day 18](http://adventofcode.com/2023/day/18) | [day18.clj](clojure/day18.clj) | -->
<!-- [Day 19](http://adventofcode.com/2023/day/19) | [day19.clj](clojure/day19.clj) | -->
Expand Down
57 changes: 57 additions & 0 deletions clojure/day16.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
(ns day16
(:require aoc))


(defn conj' [col [a b :as xs]]
; to make the main function a bit nicer
(if (int? a)
(conj col xs)
(conj (conj col a) b)))


(defn traverse [contraption x y dx dy]
(let [size (count contraption)]
(loop [seen #{}
energized #{}
stack [[x y dx dy]]]
(if-let [[x y dx dy :as current] (peek stack)]
(let [h (hash current)]
(if (or (not (< -1 x size))
(not (< -1 y size))
(seen h))
(recur seen energized (pop stack))
(recur
(conj seen h)
(conj energized (+ x (* size y)))
(conj' (pop stack)
(case ((contraption y) x)
\. [(+ x dx) (+ y dy) dx dy]
\/ [(- x dy) (- y dx) (- dy) (- dx)]
\\ [(+ x dy) (+ y dx) dy dx]
\| (if (zero? dx)
[x (+ y dy) 0 dy]
[[x (dec y) 0 -1] [x (inc y) 0 1]])
\- (if (zero? dy)
[(+ x dx) y dx 0]
[[(dec x) y -1 0] [(inc x) y 1 0]]))))))
(count energized)))))


(defn max-energy [contraption]
(let [size (count contraption)]
(->> (pmap (juxt #(traverse contraption % 0 0 1)
#(traverse contraption % (dec size) 0 -1)
#(traverse contraption 0 % 1 0)
#(traverse contraption (dec size) % -1 0))
(range size))
flatten
(reduce max))))


(defn solve [input-file]
(let [contraption (aoc/read-input input-file :chars)]
[(traverse contraption 0 0 1 0)
(max-energy contraption)]))


(solve 16)
3 changes: 2 additions & 1 deletion clojure/tests/solutions_tests.clj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
day01 day02 day03 day04 day05
day06 day07 day08 day09 day10
day11 day12 day13 day14 day15
; day16 day17 day18 day19 day20
day16 ;day17 day18 day19 day20
; day21 day22 day23 day24 day25
[clojure.test :refer [deftest is run-tests successful?]]))

Expand Down Expand Up @@ -39,6 +39,7 @@
(check-day 13 [405 400] [32723 34536])
(check-day 14 [136 64] [108857 95273])
(check-day 15 [1320 145] [498538 286278])
(check-day 16 [46 51] [6921 7594])

(let [summary (run-tests)]
(when-not (successful? summary)
Expand Down
110 changes: 110 additions & 0 deletions inputs/16.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
\..................-...|..............................|./................-.....................\.../..........
.-....../.....-..........|..-.........................|............/.......................\....\......-......
........|...\..............|/.........|...../...............................\../............../...............
...................-.............-.|......-................\........-...............-....\..../\....|..\......
.............|............\....................../..........\..../\...........................................
..............................|.............../..|......-......./........|..............................|..-.\
..............-............/|...\..\./.\............./.............-.......\............./../............-....
.........................................|.........../................................/..........|......|../..
......|.........\..\..............................|.....-\.-.\...........|...............-....-....\..........
...........................\\....|......|..............|....-../.....\|....|\........../..-...................
...........-..../.|.......-|...-.......................|.......-.|..../........................\/.............
.....................-.............\.............../............................................\\............
......-..........-.-..|...........|...........................\...............................|...........|...
................--...\........\.................../................................../............-...........
...............|../............/...../......./..\............|..../-...........|..................\.-.........
|....................|-.\...........\...\.............................................../................\....
\.|................\.\...................\....-./....\......../...................-.....................-.....
...................................................|........../......\.................|..\.............../...
................/.............|......|...........-......................|...-.................../../..|.....-.
.................|-..|.........................-............\....-./......-......../............/.....|.......
.....|.........-.....-.....-................/\..........-........|...\../..|.......-.........-...........-....
......................./.........../........|............\............../........./.....\....-/...............
/........./.......|........|..../../........|............................\........./-..-..............|.....|.
...........|.........-/........-...|/.............................-....|.....\........................|.......
.|..\............/.-..........\........-.............../.......\...........\..............\../-..-|.|-........
..\-|.......................\.......\.\.....\............-...../.......-....../.............................-.
.-........................|........../.....................................-....\.............................
..../.............-.................\.|........................./............/.|..../../................\-....
........../-................./........|......-../........|..-........./........./...........\............\....
......../.......................\..-............-...................\.....................\/........\...../...
....../....|..........-.....-/.................-...\..........................................................
/.........\.............-....|.-..\...../../......./|............................................./...../.....
.......-..|............./.............../|....../..................\....--..-./............./............./..-
-...........|............/.........-..|..................../....................../...........|...\\..........
/...-........../....................................|.......-.....|............|........././..\...............
....................../....\........|.....|....\.......-............|\....\../.....--.....-.|.................
..../........-.....\........\.....\...............|/..................|........./...-......|.................|
.......................-.......//..................|...........|-....-..-...........-.......-....\...../....\.
..................-..../......./-............/.../...\...\../............\\.|.................................
........../.........|...-..........--....................../.......................-......../......\..........
..|........../...........|............./...............|........-....\.....\...............-.....-....../.-...
...../.-..|\...|...............................-...|....\......|.............../.\............\............\..
................\/.....|..........\.................../........\./...................-........................
.........\...../..|............|...............|.-.....................\....\\.|........../.......|...........
......................\\..../.....\.....\..........|....................\.....................................
.\......./............-\........../.........-...../....../....-..............-........................\.....|.
...........\.......-.....\...\/.......-..............-...........\...................../.................\....
............-.../.....|..............-.............|......|.......|..|.............../...|.....\..............
................-..................\..|............./.................\....|..//.......|.../..................
........|.../...|....../...\....................../....-...............-........|..........................|..
..|...../..............................-............|..........-........|.........../..|..|...................
.....-..........-.....-.........................................|.........\....-.....\...........-..../...|...
.-.-........../.................../...........-................|..-............../..-..-......................
/-\.........................-|.........-......|....|-...........-...................................-...\.....
...\.........|.............././...........|./.................-........\.........\.../|........./....\\.......
..|.................-......\./....||.........\....|..........-......|.................\..........\.....-../...
......../.|................................../...................|..\\.....--...................-..|..........
.-........-......./...\....|....../..........-./......./..|................\/......../......../....../........
...-....../.|........./.............\.....|../.-./.......................-....|.........\......|/..........|..
.....|................//.\......|.\/.................................|-...//........./...-.-............\.....
.............../...............\..-....................-..../........../....-...............|-.......\../...\.
..|......\........./../..................|....|................|\.|....\....-.|.......-../......||.-.|....\./.
......|./.................\............|.......................\........-.....-...../..........\......-.../..|
.....|.\....\..............-......-./......./......................../............|..............|............
..............................\..................../................|.................-......./.........../...
.......-.....-..........\..-...../.....\.\..............\..\....../.|.../...............-..................\..
.................|..................-.\....................................\.....-............................
...-.......|........-....-.......-....-..........-.-..\.......................................\.-.../......../
.............-......\................-.....-.....-/......./...\\../.................\....-......./..-.....-|..
....\.............|..-......./-......|............\.........-........\..../...\.......|.|..-...\..............
.....................-....................................../.......\....\........................../\..../\..
........-............\....\./...........\.-........./........./..\.........................../....-..|......|.
..................../.........../.\..............-............\.........\/...\..................-.............
.-...\......\.............................................................-.............\.\.-......|..........
................|..............|.-........../......\.............|............./.|..-|..........|.............
...-........../....\.../.../.........................................................../.........|.-......-...
..-....................-..........................|..../.......|........|../......./....../...-......../....-.
........\............................-.......\...../\......|\......-............./.........-..................
...../...../|..............|.-.....\....-/...........\...............\.\.......................-.-............
......\.......\.....................|................................\.....\..............................\./.
\...............................-.............-../-............................./...............\.|...........
......|......|................-|..-....\/....\...................-...........|...../....................\\....
.............../..............-...|....................\\..............-....\..........\...............|....|.
...\.-.......|..-/...................../|............\......-.../...................../.......................
........../........./.........-....\........................|./......./..............-...-....................
....-|-..............\.......................\.................|.......\.........../..........-......./.......
..-...........................|\.................\................/\................................../.|.....
......\.................................|........\./............-..............\..........-.........../..-....
................-..........|.............|.....|....................-..........\......|-\....|...........-....
...|.........../\............./.........\........\....................|...........................-.\.........
../.|...................././......-..............-\.....-........-.|...........\......-./..-..................
......\.......-..-..............................-................|.................../........................
..\..-................/..-....-........./..........................-.../...........................-..........
............./-./.\........./....-....................././.....|............-...............|./...........-|..
...................-...\.....\....................../............--....................../...........\........
.............-.....\......\./........................../.....................-...................\.........-|.
.....\.........\.../......................\..................|.............................\.|........./.....|
.....\.....\./..............-............../..............\.......|........../..//.-...-.\......../.........\.
..............\...\..........................\...\./.-...|.........|.......|..\../.........................|..
.\..|......\.\\.........../....../....../......./................../..|................/....|.................
./|............./...................................\.-....\..\|./.-.............................\.......\....
\./........./.....\............................/.../...\....................|..............|.|............/|..
..\............\............\.....-..........\.......|../.|......./|......-...--...-..\......|./.-......-.....
...............\........|............\|....|..\.......-................/.........|....\-.|.|..../-.......|....
...................|/.............|............/................|..|.\.|.........|...\........................
..................|../..../.......-................................-......................-.........|.......\.
...............\...../......\./...../............/...........-..........\....../.....\.....\......|...........
../........\.....|..................\......./.......-../.......\.../.|....................................|...
.|.|..\....\.|./.../........................./........-..\.../.............\.............................|....
..................|...\................-..-/..................-...|./..................-..|...................
10 changes: 10 additions & 0 deletions inputs/16_test.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
.|...\....
|.-.\.....
.....|-...
........|.
..........
.........\
..../.\\..
.-.-/..|..
.|....-|.\
..//.|....

0 comments on commit b440311

Please sign in to comment.