diff --git a/projects/aoc/src/year2021/Solutions2021.hx b/projects/aoc/src/year2021/Solutions2021.hx index fb2c4d17..896b9681 100644 --- a/projects/aoc/src/year2021/Solutions2021.hx +++ b/projects/aoc/src/year2021/Solutions2021.hx @@ -3,6 +3,8 @@ package year2021; import kiss.Prelude; import haxe.Constraints; import haxe.ds.Option; +import haxe.Int64; +import haxe.Int64Helper; #if day4 import year2021.Day4; #end diff --git a/projects/aoc/src/year2021/Solutions2021.kiss b/projects/aoc/src/year2021/Solutions2021.kiss index 14817f83..36e36fe3 100644 --- a/projects/aoc/src/year2021/Solutions2021.kiss +++ b/projects/aoc/src/year2021/Solutions2021.kiss @@ -20,7 +20,10 @@ (dayTodo 5) (day 6 (load "day6.kiss") - (print (countAfter "src/year2021/inputs/day6.txt" 80))) + (assert (= "5934" (countAfter "src/year2021/inputs/day6-example.txt" 80))) + (assert (= "26984457539" (countAfter "src/year2021/inputs/day6-example.txt" 256))) + (assert (= "346063" (countAfter "src/year2021/inputs/day6.txt" 80))) + (assert (= "1572358335990" (countAfter "src/year2021/inputs/day6.txt" 256)))) (dayTodo 7) (dayTodo 8) (dayTodo 9) diff --git a/projects/aoc/src/year2021/day6.kiss b/projects/aoc/src/year2021/day6.kiss index 3b15ff93..9d165ff1 100644 --- a/projects/aoc/src/year2021/day6.kiss +++ b/projects/aoc/src/year2021/day6.kiss @@ -1,28 +1,30 @@ -(defMacro dictInc [theMap key &builder b amount] - `(let [theMap ,theMap] - (unless (dictGet theMap ,key) - (dictSet theMap ,key 0)) - (+= (dictGet theMap ,key) ,amount))) +(defMacro dictInc [theMap key amount] + `(let [theMap ,theMap key ,key amount ,amount &mut count (dictGet theMap key)] + (unless count + (dictSet theMap key (set count (Int64Helper.fromFloat 0)))) + (dictSet theMap key #|count + amount|#))) // Store the simulation state in a map of age => count. -(function :Map ageMap [:String file] +(function :Map ageMap [:String file] (let [:Array list (map (.split (first (Util.readLines file)) ",") Std.parseInt) - :Map theMap (new Map)] + :Map theMap (new Map)] // special key -1 corresponds to total count - (dictSet theMap -1 list.length) + (dictSet theMap -1 (Int64Helper.fromFloat list.length)) (doFor age (range 9) - (dictSet theMap age 0)) + (dictSet theMap age (Int64Helper.fromFloat 0))) (doFor age list - (+= (dictGet theMap age) 1)) + (let [count (dictGet theMap age) + one (Int64Helper.fromFloat 1)] + (dictSet theMap age #|count + one|#))) theMap)) -(function :Map stepAgeMap [:Map theMap] +(function :Map stepAgeMap [:Map theMap] // Lanternfish are guaranteed to be between 0-8 years old - (let [:Map newMap (new Map)] + (let [:Map newMap (new Map)] (dictSet newMap -1 (dictGet theMap -1)) (doFor age (range 9) (let [count (dictGet theMap age)] - (case age + (case ~age // Lanternfish giving birth: (0 (dictInc newMap 8 count) @@ -32,8 +34,8 @@ (dictInc newMap (- age 1) count))))) newMap)) -(function countAfter [file days] +(function :String 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 + (Int64.toStr (dictGet ageMap -1)))) \ No newline at end of file