-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
180 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
\..................-...|..............................|./................-.....................\.../.......... | ||
.-....../.....-..........|..-.........................|............/.......................\....\......-...... | ||
........|...\..............|/.........|...../...............................\../............../............... | ||
...................-.............-.|......-................\........-...............-....\..../\....|..\...... | ||
.............|............\....................../..........\..../\........................................... | ||
..............................|.............../..|......-......./........|..............................|..-.\ | ||
..............-............/|...\..\./.\............./.............-.......\............./../............-.... | ||
.........................................|.........../................................/..........|......|../.. | ||
......|.........\..\..............................|.....-\.-.\...........|...............-....-....\.......... | ||
...........................\\....|......|..............|....-../.....\|....|\........../..-................... | ||
...........-..../.|.......-|...-.......................|.......-.|..../........................\/............. | ||
.....................-.............\.............../............................................\\............ | ||
......-..........-.-..|...........|...........................\...............................|...........|... | ||
................--...\........\.................../................................../............-........... | ||
...............|../............/...../......./..\............|..../-...........|..................\.-......... | ||
|....................|-.\...........\...\.............................................../................\.... | ||
\.|................\.\...................\....-./....\......../...................-.....................-..... | ||
...................................................|........../......\.................|..\.............../... | ||
................/.............|......|...........-......................|...-.................../../..|.....-. | ||
.................|-..|.........................-............\....-./......-......../............/.....|....... | ||
.....|.........-.....-.....-................/\..........-........|...\../..|.......-.........-...........-.... | ||
......................./.........../........|............\............../........./.....\....-/............... | ||
/........./.......|........|..../../........|............................\........./-..-..............|.....|. | ||
...........|.........-/........-...|/.............................-....|.....\........................|....... | ||
.|..\............/.-..........\........-.............../.......\...........\..............\../-..-|.|-........ | ||
..\-|.......................\.......\.\.....\............-...../.......-....../.............................-. | ||
.-........................|........../.....................................-....\............................. | ||
..../.............-.................\.|........................./............/.|..../../................\-.... | ||
........../-................./........|......-../........|..-........./........./...........\............\.... | ||
......../.......................\..-............-...................\.....................\/........\...../... | ||
....../....|..........-.....-/.................-...\.......................................................... | ||
/.........\.............-....|.-..\...../../......./|............................................./...../..... | ||
.......-..|............./.............../|....../..................\....--..-./............./............./..- | ||
-...........|............/.........-..|..................../....................../...........|...\\.......... | ||
/...-........../....................................|.......-.....|............|........././..\............... | ||
....................../....\........|.....|....\.......-............|\....\../.....--.....-.|................. | ||
..../........-.....\........\.....\...............|/..................|........./...-......|.................| | ||
.......................-.......//..................|...........|-....-..-...........-.......-....\...../....\. | ||
..................-..../......./-............/.../...\...\../............\\.|................................. | ||
........../.........|...-..........--....................../.......................-......../......\.......... | ||
..|........../...........|............./...............|........-....\.....\...............-.....-....../.-... | ||
...../.-..|\...|...............................-...|....\......|.............../.\............\............\.. | ||
................\/.....|..........\.................../........\./...................-........................ | ||
.........\...../..|............|...............|.-.....................\....\\.|........../.......|........... | ||
......................\\..../.....\.....\..........|....................\..................................... | ||
.\......./............-\........../.........-...../....../....-..............-........................\.....|. | ||
...........\.......-.....\...\/.......-..............-...........\...................../.................\.... | ||
............-.../.....|..............-.............|......|.......|..|.............../...|.....\.............. | ||
................-..................\..|............./.................\....|..//.......|.../.................. | ||
........|.../...|....../...\....................../....-...............-........|..........................|.. | ||
..|...../..............................-............|..........-........|.........../..|..|................... | ||
.....-..........-.....-.........................................|.........\....-.....\...........-..../...|... | ||
.-.-........../.................../...........-................|..-............../..-..-...................... | ||
/-\.........................-|.........-......|....|-...........-...................................-...\..... | ||
...\.........|.............././...........|./.................-........\.........\.../|........./....\\....... | ||
..|.................-......\./....||.........\....|..........-......|.................\..........\.....-../... | ||
......../.|................................../...................|..\\.....--...................-..|.......... | ||
.-........-......./...\....|....../..........-./......./..|................\/......../......../....../........ | ||
...-....../.|........./.............\.....|../.-./.......................-....|.........\......|/..........|.. | ||
.....|................//.\......|.\/.................................|-...//........./...-.-............\..... | ||
.............../...............\..-....................-..../........../....-...............|-.......\../...\. | ||
..|......\........./../..................|....|................|\.|....\....-.|.......-../......||.-.|....\./. | ||
......|./.................\............|.......................\........-.....-...../..........\......-.../..| | ||
.....|.\....\..............-......-./......./......................../............|..............|............ | ||
..............................\..................../................|.................-......./.........../... | ||
.......-.....-..........\..-...../.....\.\..............\..\....../.|.../...............-..................\.. | ||
.................|..................-.\....................................\.....-............................ | ||
...-.......|........-....-.......-....-..........-.-..\.......................................\.-.../......../ | ||
.............-......\................-.....-.....-/......./...\\../.................\....-......./..-.....-|.. | ||
....\.............|..-......./-......|............\.........-........\..../...\.......|.|..-...\.............. | ||
.....................-....................................../.......\....\........................../\..../\.. | ||
........-............\....\./...........\.-........./........./..\.........................../....-..|......|. | ||
..................../.........../.\..............-............\.........\/...\..................-............. | ||
.-...\......\.............................................................-.............\.\.-......|.......... | ||
................|..............|.-........../......\.............|............./.|..-|..........|............. | ||
...-........../....\.../.../.........................................................../.........|.-......-... | ||
..-....................-..........................|..../.......|........|../......./....../...-......../....-. | ||
........\............................-.......\...../\......|\......-............./.........-.................. | ||
...../...../|..............|.-.....\....-/...........\...............\.\.......................-.-............ | ||
......\.......\.....................|................................\.....\..............................\./. | ||
\...............................-.............-../-............................./...............\.|........... | ||
......|......|................-|..-....\/....\...................-...........|...../....................\\.... | ||
.............../..............-...|....................\\..............-....\..........\...............|....|. | ||
...\.-.......|..-/...................../|............\......-.../...................../....................... | ||
........../........./.........-....\........................|./......./..............-...-.................... | ||
....-|-..............\.......................\.................|.......\.........../..........-......./....... | ||
..-...........................|\.................\................/\................................../.|..... | ||
......\.................................|........\./............-..............\..........-.........../..-.... | ||
................-..........|.............|.....|....................-..........\......|-\....|...........-.... | ||
...|.........../\............./.........\........\....................|...........................-.\......... | ||
../.|...................././......-..............-\.....-........-.|...........\......-./..-.................. | ||
......\.......-..-..............................-................|.................../........................ | ||
..\..-................/..-....-........./..........................-.../...........................-.......... | ||
............./-./.\........./....-....................././.....|............-...............|./...........-|.. | ||
...................-...\.....\....................../............--....................../...........\........ | ||
.............-.....\......\./........................../.....................-...................\.........-|. | ||
.....\.........\.../......................\..................|.............................\.|........./.....| | ||
.....\.....\./..............-............../..............\.......|........../..//.-...-.\......../.........\. | ||
..............\...\..........................\...\./.-...|.........|.......|..\../.........................|.. | ||
.\..|......\.\\.........../....../....../......./................../..|................/....|................. | ||
./|............./...................................\.-....\..\|./.-.............................\.......\.... | ||
\./........./.....\............................/.../...\....................|..............|.|............/|.. | ||
..\............\............\.....-..........\.......|../.|......./|......-...--...-..\......|./.-......-..... | ||
...............\........|............\|....|..\.......-................/.........|....\-.|.|..../-.......|.... | ||
...................|/.............|............/................|..|.\.|.........|...\........................ | ||
..................|../..../.......-................................-......................-.........|.......\. | ||
...............\...../......\./...../............/...........-..........\....../.....\.....\......|........... | ||
../........\.....|..................\......./.......-../.......\.../.|....................................|... | ||
.|.|..\....\.|./.../........................./........-..\.../.............\.............................|.... | ||
..................|...\................-..-/..................-...|./..................-..|................... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
.|...\.... | ||
|.-.\..... | ||
.....|-... | ||
........|. | ||
.......... | ||
.........\ | ||
..../.\\.. | ||
.-.-/..|.. | ||
.|....-|.\ | ||
..//.|.... |