application/octet-stream
•
1.86 KB
•
51 lines
#!/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)