diff --git a/projects/aoc/src/year2021/Solutions2021.kiss b/projects/aoc/src/year2021/Solutions2021.kiss index 2b60da11..14817f83 100644 --- a/projects/aoc/src/year2021/Solutions2021.kiss +++ b/projects/aoc/src/year2021/Solutions2021.kiss @@ -18,7 +18,9 @@ (assert (= 4512 (winningScore "src/year2021/inputs/day4-example.txt"))) (assert (= 46920 (winningScore "src/year2021/inputs/day4.txt")))) (dayTodo 5) - (dayTodo 6) + (day 6 + (load "day6.kiss") + (print (countAfter "src/year2021/inputs/day6.txt" 80))) (dayTodo 7) (dayTodo 8) (dayTodo 9) diff --git a/projects/aoc/src/year2021/day6.kiss b/projects/aoc/src/year2021/day6.kiss new file mode 100644 index 00000000..3b15ff93 --- /dev/null +++ b/projects/aoc/src/year2021/day6.kiss @@ -0,0 +1,39 @@ +(defMacro dictInc [theMap key &builder b amount] + `(let [theMap ,theMap] + (unless (dictGet theMap ,key) + (dictSet theMap ,key 0)) + (+= (dictGet theMap ,key) ,amount))) + +// Store the simulation state in a map of age => count. +(function :Map ageMap [:String file] + (let [:Array list (map (.split (first (Util.readLines file)) ",") Std.parseInt) + :Map theMap (new Map)] + // special key -1 corresponds to total count + (dictSet theMap -1 list.length) + (doFor age (range 9) + (dictSet theMap age 0)) + (doFor age list + (+= (dictGet theMap age) 1)) + theMap)) + +(function :Map stepAgeMap [:Map theMap] + // Lanternfish are guaranteed to be between 0-8 years old + (let [:Map newMap (new Map)] + (dictSet newMap -1 (dictGet theMap -1)) + (doFor age (range 9) + (let [count (dictGet theMap age)] + (case age + // Lanternfish giving birth: + (0 + (dictInc newMap 8 count) + (dictInc newMap -1 count) + (dictInc newMap 6 count)) + (otherwise + (dictInc newMap (- age 1) count))))) + newMap)) + +(function countAfter [file days] + (let [&mut ageMap (ageMap file)] + (doFor i (range days) + (set ageMap (stepAgeMap ageMap))) + (nth ageMap -1))) \ No newline at end of file diff --git a/projects/aoc/src/year2021/inputs/day6.txt b/projects/aoc/src/year2021/inputs/day6.txt new file mode 100644 index 00000000..b800e1c3 --- /dev/null +++ b/projects/aoc/src/year2021/inputs/day6.txt @@ -0,0 +1 @@ +1,5,5,1,5,1,5,3,1,3,2,4,3,4,1,1,3,5,4,4,2,1,2,1,2,1,2,1,5,2,1,5,1,2,2,1,5,5,5,1,1,1,5,1,3,4,5,1,2,2,5,5,3,4,5,4,4,1,4,5,3,4,4,5,2,4,2,2,1,3,4,3,2,3,4,1,4,4,4,5,1,3,4,2,5,4,5,3,1,4,1,1,1,2,4,2,1,5,1,4,5,3,3,4,1,1,4,3,4,1,1,1,5,4,3,5,2,4,1,1,2,3,2,4,4,3,3,5,3,1,4,5,5,4,3,3,5,1,5,3,5,2,5,1,5,5,2,3,3,1,1,2,2,4,3,1,5,1,1,3,1,4,1,2,3,5,5,1,2,3,4,3,4,1,1,5,5,3,3,4,5,1,1,4,1,4,1,3,5,5,1,4,3,1,3,5,5,5,5,5,2,2,1,2,4,1,5,3,3,5,4,5,4,1,5,1,5,1,2,5,4,5,5,3,2,2,2,5,4,4,3,3,1,4,1,2,3,1,5,4,5,3,4,1,1,2,2,1,2,5,1,1,1,5,4,5,2,1,4,4,1,1,3,3,1,3,2,1,5,2,3,4,5,3,5,4,3,1,3,5,5,5,5,2,1,1,4,2,5,1,5,1,3,4,3,5,5,1,4,3