Login
7 branches 0 tags
Ben (Win10) Nicer editor b8d243a 3 years ago 566 Commits
nujel / tests / fast / day1.nuj
#!/usr/bin/env nujel
; https://adventofcode.com/2021/day/1

[def example-list '[199 200 208 210 200 207 240 269 260 263]]

[defn 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]

[defn 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]]]