// 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 (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 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 :String countAfter [file days] (let [&mut ageMap (ageMap file)] (doFor i (range days) (set ageMap (stepAgeMap ageMap))) (Int64.toStr (dictGet ageMap -1))))