Files
kiss-vscode/projects/aoc/src/year2021/day6.kiss
2022-06-18 20:35:57 +00:00

35 lines
1.5 KiB
Plaintext

// Store the simulation state in a map of age => count.
(function :Map<Int,Int64> ageMap [:String file]
(let [:Array<Int> list (map (.split (first (Util.readLines file)) ",") Std.parseInt)
:Map<Int,Int64> theMap (new Map)]
// special key -1 corresponds to total count
(dictSet theMap -1 (Int64Helper.fromFloat list.length))
(doFor age (range 9)
(dictSet theMap age (Int64Helper.fromFloat 0)))
(doFor age list
(let [count (dictGet theMap age)
one (Int64Helper.fromFloat 1)]
(dictSet theMap age #{count + one;}#)))
theMap))
(function :Map<Int,Int64> stepAgeMap [:Map<Int,Int64> theMap]
// Lanternfish are guaranteed to be between 0-8 years old
(let [:Map<Int,Int64> 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 :String countAfter [file days]
(let [&mut ageMap (ageMap file)]
(doFor i (range days)
(set ageMap (stepAgeMap ageMap)))
(Int64.toStr (dictGet ageMap -1))))