application/octet-stream
•
1.03 KB
•
29 lines
#!/usr/bin/env nujel
; https://adventofcode.com/2021/day/1
[def example-list '[199 200 208 210 200 207 240 269 260 263]]
[defun count-increases [l]
"Compares every two items in L and increments a counter if the value increases"
[def acc 0]
[while [and l [cdr l]]
[when [> [cadr l] [car l]] [++ acc]]
[cdr! l]]
acc]
[defun build-sums [l]
"Build sums out of 3 values with a sliding window"
[def ret #nil]
[while [and l [cdr l] [cddr l]]
[set! ret [cons [+ [car l] [cadr l] [caddr l]] ret]]
[cdr! l]]
[nreverse ret]]
[def result [count-increases example-list]]
[when [!= result 7] [throw [list :wrong-result "Wrong result" result]]]
[def result [count-increases [read [file/read "tests/fast/day1.dat"]]]]
[when [!= result 1711] [throw [list :wrong-result "Wrong result" result]]]
[def result [count-increases [build-sums [read [file/read "tests/fast/day1.dat"]]]]]
[when [!= result 1743] [throw [list :wrong-result "Wrong result" result]]]