actually solve aoc day 6 pt 2
This commit is contained in:
@@ -3,6 +3,8 @@ package year2021;
|
|||||||
import kiss.Prelude;
|
import kiss.Prelude;
|
||||||
import haxe.Constraints;
|
import haxe.Constraints;
|
||||||
import haxe.ds.Option;
|
import haxe.ds.Option;
|
||||||
|
import haxe.Int64;
|
||||||
|
import haxe.Int64Helper;
|
||||||
#if day4
|
#if day4
|
||||||
import year2021.Day4;
|
import year2021.Day4;
|
||||||
#end
|
#end
|
||||||
|
@@ -20,7 +20,10 @@
|
|||||||
(dayTodo 5)
|
(dayTodo 5)
|
||||||
(day 6
|
(day 6
|
||||||
(load "day6.kiss")
|
(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 7)
|
||||||
(dayTodo 8)
|
(dayTodo 8)
|
||||||
(dayTodo 9)
|
(dayTodo 9)
|
||||||
|
@@ -1,28 +1,30 @@
|
|||||||
(defMacro dictInc [theMap key &builder b amount]
|
(defMacro dictInc [theMap key amount]
|
||||||
`(let [theMap ,theMap]
|
`(let [theMap ,theMap key ,key amount ,amount &mut count (dictGet theMap key)]
|
||||||
(unless (dictGet theMap ,key)
|
(unless count
|
||||||
(dictSet theMap ,key 0))
|
(dictSet theMap key (set count (Int64Helper.fromFloat 0))))
|
||||||
(+= (dictGet theMap ,key) ,amount)))
|
(dictSet theMap key #|count + amount|#)))
|
||||||
|
|
||||||
// Store the simulation state in a map of age => count.
|
// Store the simulation state in a map of age => count.
|
||||||
(function :Map<Int,Int> ageMap [:String file]
|
(function :Map<Int,Int64> ageMap [:String file]
|
||||||
(let [:Array<Int> list (map (.split (first (Util.readLines file)) ",") Std.parseInt)
|
(let [:Array<Int> list (map (.split (first (Util.readLines file)) ",") Std.parseInt)
|
||||||
:Map<Int,Int> theMap (new Map)]
|
:Map<Int,Int64> theMap (new Map)]
|
||||||
// special key -1 corresponds to total count
|
// special key -1 corresponds to total count
|
||||||
(dictSet theMap -1 list.length)
|
(dictSet theMap -1 (Int64Helper.fromFloat list.length))
|
||||||
(doFor age (range 9)
|
(doFor age (range 9)
|
||||||
(dictSet theMap age 0))
|
(dictSet theMap age (Int64Helper.fromFloat 0)))
|
||||||
(doFor age list
|
(doFor age list
|
||||||
(+= (dictGet theMap age) 1))
|
(let [count (dictGet theMap age)
|
||||||
|
one (Int64Helper.fromFloat 1)]
|
||||||
|
(dictSet theMap age #|count + one|#)))
|
||||||
theMap))
|
theMap))
|
||||||
|
|
||||||
(function :Map<Int,Int> stepAgeMap [:Map<Int,Int> theMap]
|
(function :Map<Int,Int64> stepAgeMap [:Map<Int,Int64> theMap]
|
||||||
// Lanternfish are guaranteed to be between 0-8 years old
|
// Lanternfish are guaranteed to be between 0-8 years old
|
||||||
(let [:Map<Int,Int> newMap (new Map)]
|
(let [:Map<Int,Int64> newMap (new Map)]
|
||||||
(dictSet newMap -1 (dictGet theMap -1))
|
(dictSet newMap -1 (dictGet theMap -1))
|
||||||
(doFor age (range 9)
|
(doFor age (range 9)
|
||||||
(let [count (dictGet theMap age)]
|
(let [count (dictGet theMap age)]
|
||||||
(case age
|
(case ~age
|
||||||
// Lanternfish giving birth:
|
// Lanternfish giving birth:
|
||||||
(0
|
(0
|
||||||
(dictInc newMap 8 count)
|
(dictInc newMap 8 count)
|
||||||
@@ -32,8 +34,8 @@
|
|||||||
(dictInc newMap (- age 1) count)))))
|
(dictInc newMap (- age 1) count)))))
|
||||||
newMap))
|
newMap))
|
||||||
|
|
||||||
(function countAfter [file days]
|
(function :String countAfter [file days]
|
||||||
(let [&mut ageMap (ageMap file)]
|
(let [&mut ageMap (ageMap file)]
|
||||||
(doFor i (range days)
|
(doFor i (range days)
|
||||||
(set ageMap (stepAgeMap ageMap)))
|
(set ageMap (stepAgeMap ageMap)))
|
||||||
(nth ageMap -1)))
|
(Int64.toStr (dictGet ageMap -1))))
|
Reference in New Issue
Block a user