Solve AOC day 11
This commit is contained in:
@@ -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")))
|
||||
|
52
projects/aoc/src/year2021/day11.kiss
Normal file
52
projects/aoc/src/year2021/day11.kiss
Normal file
@@ -0,0 +1,52 @@
|
||||
(function readGrid [file]
|
||||
(for line (Util.readLines file) (map (line.split "") Std.parseInt)))
|
||||
|
||||
(function :Array<Array<Int>> neighboringPoints [:Array<Int> 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<Array<Int>> 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<Array<Int>> grid]
|
||||
(let [newGrid
|
||||
(for line grid (for octo line (+ octo 1)))]
|
||||
(while (handleFlashes newGrid) 0)
|
||||
newGrid))
|
||||
(var stepGrid (memoize _stepGrid))
|
||||
|
||||
(function _flashes [:Array<Array<Int>> 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))
|
10
projects/aoc/src/year2021/inputs/day11-example.txt
Normal file
10
projects/aoc/src/year2021/inputs/day11-example.txt
Normal file
@@ -0,0 +1,10 @@
|
||||
5483143223
|
||||
2745854711
|
||||
5264556173
|
||||
6141336146
|
||||
6357385478
|
||||
4167524645
|
||||
2176841721
|
||||
6882881134
|
||||
4846848554
|
||||
5283751526
|
10
projects/aoc/src/year2021/inputs/day11.txt
Normal file
10
projects/aoc/src/year2021/inputs/day11.txt
Normal file
@@ -0,0 +1,10 @@
|
||||
2238518614
|
||||
4552388553
|
||||
2562121143
|
||||
2666685337
|
||||
7575518784
|
||||
3572534871
|
||||
8411718283
|
||||
7742668385
|
||||
1235133231
|
||||
2546165345
|
Reference in New Issue
Block a user