23 lines
704 B
Fennel
23 lines
704 B
Fennel
(io.input "input")
|
|
|
|
(local n 3)
|
|
|
|
(var max [0 0 0]) ; greatest is position 1, then 2, last 3
|
|
(var calories-sum 0)
|
|
|
|
(each [line (io.lines)]
|
|
(let [num (tonumber line)]
|
|
(if (not num)
|
|
(let [ s calories-sum
|
|
; count to how many "max" the new sum is greater than
|
|
greater-than
|
|
(accumulate [sum 0 _ m (ipairs max)] (+ sum (if (> s m) 1 0)))]
|
|
(set calories-sum 0)
|
|
(when (> greater-than 0)
|
|
(table.insert max (- (+ n 1) greater-than) s) ; insert new max
|
|
(table.remove max))) ; remove last element
|
|
(set calories-sum (+ calories-sum num)))))
|
|
|
|
(print "part 1" (. max 1))
|
|
(print "part 2" (accumulate [sum 0 _ m (ipairs max)] (+ sum m)))
|