solve AOC day 6 pt 1

This commit is contained in:
2021-12-06 13:04:43 -07:00
parent 880712f59c
commit 42e23f3b16
3 changed files with 43 additions and 1 deletions

View File

@@ -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)

View File

@@ -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<Int,Int> ageMap [:String file]
(let [:Array<Int> list (map (.split (first (Util.readLines file)) ",") Std.parseInt)
:Map<Int,Int> 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<Int,Int> stepAgeMap [:Map<Int,Int> theMap]
// Lanternfish are guaranteed to be between 0-8 years old
(let [:Map<Int,Int> 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)))

View File

@@ -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