Login
7 branches 0 tags
Ben (X13/Arch) Simplified things a little 0643405 9 days ago 1260 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))
                    (inc! 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 (not= result 7) (throw (list :wrong-result "Wrong result" result)))

(def result (count-increases (read (file/read "tests/fast/day1.dat"))))
(when (not= result 1711) (throw (list :wrong-result "Wrong result" result)))

(def result (count-increases (build-sums (read (file/read "tests/fast/day1.dat")))))
(when (not= result 1743) (throw (list :wrong-result "Wrong result" result)))

(return :success)