Login
7 branches 0 tags
Ben (X13/Arch) Simplified things a little 0643405 9 days ago 1260 Commits
nujel / tests / fast / day10.nuj
#!/usr/bin/env nujel

(defn char-to-score (cc)
      (case cc
            (#\)     3)
            (#\]    57)
            (#\}  1197)
            (#\> 25137)
            (otherwise 0)))

(defn p2-score (cc)
      (case cc
            (#\) 1)
            (#\] 2)
            (#\} 3)
            (#\> 4)
            (otherwise 0)))

(defn calc-part2-score (stack α)
      (if-not stack α
              (calc-part2-score (cdr stack)
                                (+ (* 5 α)
                                   (p2-score (car stack))))))

(defn find-first-syntax-error (line)
      (def stack #nil)
      (try (fn (ε) (if (= (car ε) :return)
                       (cons :part1 (char-to-score (cadr ε)))
                       (throw ε)))
           (dotimes (i (:length line))
                    (def cc (ref line i))
                    (case cc
                          ((#\() (set! stack (cons #\) stack)))
                          ((#\{) (set! stack (cons #\} stack)))
                          ((#\[) (set! stack (cons #\] stack)))
                          ((#\<) (set! stack (cons #\> stack)))
                          ((#\) #\} #\] #\>) (if (= cc (car stack))
                                                 (set! stack (cdr stack))
                                                 (throw (list :return cc i))))))
           (calc-part2-score stack 0)))

(def lines (split (file/read "tests/fast/day10.dat") "\n"))
(def res (map lines find-first-syntax-error))
(def p1-res (reduce res (fn (α β) (if (= :part1 (car β)) (+ α (cdr β)) α)) 0))
(def incomplete-lines (list/sort (filter res (fn (α) (not= :part1 (car α))))))
(def p2-res (ref incomplete-lines (div/int (:length incomplete-lines) 2)))
(when (not= p1-res 315693)
  (throw (list :wrong-result "Wrong result" p1-res)))
(when (not= p2-res 1870887234)
  (throw (list :wrong-result "Wrong result" p2-res)))
(return :success)