diff --git a/projects/aoc/src/year2021/Solutions2021.kiss b/projects/aoc/src/year2021/Solutions2021.kiss index 2d701205..fc0aa5d2 100644 --- a/projects/aoc/src/year2021/Solutions2021.kiss +++ b/projects/aoc/src/year2021/Solutions2021.kiss @@ -69,7 +69,12 @@ (assert (= 462693 (apply + (map (map (Util.readLines "src/year2021/inputs/day10.txt") getLineType) score)))) (let [completionScores (sort (filter (map (map (Util.readLines "src/year2021/inputs/day10.txt") getLineType) completionScore)))] (assert (= 3094671161 (nth completionScores (Math.floor (/ completionScores.length 2))))))) - (dayTodo 11) + (day 11 + (load "day11.kiss") + (assert (= 35 (flashesAfter 2 "src/year2021/inputs/day11-example.txt"))) + (assert (= 1656 (flashesAfter 100 "src/year2021/inputs/day11-example.txt"))) + (assert (= 1723 (flashesAfter 100 "src/year2021/inputs/day11.txt"))) + ~(firstSimultaneousFlash "src/year2021/inputs/day11.txt")) (day 12 (load "day12.kiss") (assert (= 10 .length (allPaths "start" "end" "src/year2021/inputs/day12-example.txt"))) diff --git a/projects/aoc/src/year2021/day11.kiss b/projects/aoc/src/year2021/day11.kiss new file mode 100644 index 00000000..460c09e6 --- /dev/null +++ b/projects/aoc/src/year2021/day11.kiss @@ -0,0 +1,52 @@ +(function readGrid [file] + (for line (Util.readLines file) (map (line.split "") Std.parseInt))) + +(function :Array> neighboringPoints [:Array point] + (let [[col row] point] + (apply concat (for c (range (- col 1) (+ col 2)) + (for r (range (- row 1) (+ row 2)) [c r]))))) + +// Return true if any new flashes were triggered, so handleFlashes can be called again +(function :Bool handleFlashes [:Array> grid] + (let [&mut newFlash false] + (doFor row (range grid.length) + (doFor col (range .length (first grid)) + (when (<= 10 (nth (nth grid row) col)) + (set newFlash true) + (setNth (nth grid row) col 0) + (doFor [c r] (neighboringPoints [col row]) + (when (and + (< c .length (first grid)) + (>= c 0) + (< r grid.length) + (>= r 0) + !(= 0 (nth (nth grid r) c))) + (setNth (nth grid r) c (+ 1 (nth (nth grid r) c)))))))) + newFlash)) + +(function _stepGrid [:Array> grid] + (let [newGrid + (for line grid (for octo line (+ octo 1)))] + (while (handleFlashes newGrid) 0) + newGrid)) +(var stepGrid (memoize _stepGrid)) + +(function _flashes [:Array> grid] + (apply + (for line grid (count line ->num ?(= num 0))))) +(var flashes (memoize _flashes)) + +(function flashesAfter [steps file] + (let [&mut grid (readGrid file) + &mut :Float f 0] + (doFor i (range steps) + (set grid (stepGrid grid)) + (+= f (flashes grid))) + f)) + +(function firstSimultaneousFlash [file] + (let [&mut grid (readGrid file) + &mut step 0] + (until (= (* grid.length .length (nth grid 0)) (flashes grid)) + (set grid (stepGrid grid)) + (+= step 1)) + step)) \ No newline at end of file diff --git a/projects/aoc/src/year2021/inputs/day11-example.txt b/projects/aoc/src/year2021/inputs/day11-example.txt new file mode 100644 index 00000000..a3819c90 --- /dev/null +++ b/projects/aoc/src/year2021/inputs/day11-example.txt @@ -0,0 +1,10 @@ +5483143223 +2745854711 +5264556173 +6141336146 +6357385478 +4167524645 +2176841721 +6882881134 +4846848554 +5283751526 \ No newline at end of file diff --git a/projects/aoc/src/year2021/inputs/day11.txt b/projects/aoc/src/year2021/inputs/day11.txt new file mode 100644 index 00000000..b5a77cd8 --- /dev/null +++ b/projects/aoc/src/year2021/inputs/day11.txt @@ -0,0 +1,10 @@ +2238518614 +4552388553 +2562121143 +2666685337 +7575518784 +3572534871 +8411718283 +7742668385 +1235133231 +2546165345