aoc2022/day02/day02.lisp
2022-12-02 12:22:50 +01:00

61 lines
1.2 KiB
Common Lisp

(defun parse-rounds (input)
(cl-ppcre:split "\\n" (uiop:read-file-string input)))
(defun calc-points-one (round)
(let* ((round (uiop:split-string round :separator " "))
(p1 (intern (first round)))
(p2 (intern (second round))))
(cond ((eq 'A p1)
(cond ((eq 'X p2)
(+ 1 3))
((eq 'Y p2)
(+ 2 6))
((eq 'Z p2)
(+ 3 0))))
((eq 'B p1)
(cond ((eq 'X p2)
(+ 1 0))
((eq 'Y p2)
(+ 2 3))
((eq 'Z p2)
(+ 3 6))))
((eq 'C p1)
(cond ((eq 'X p2)
(+ 1 6))
((eq 'Y p2)
(+ 2 0))
((eq 'Z p2)
(+ 3 3)))))))
(defun part-one (input)
(reduce '+ (mapcar #'calc-points-one (parse-rounds input))))
(defun calc-points-two (round)
(let* ((round (uiop:split-string round :separator " "))
(p1 (intern (first round)))
(p2 (intern (second round))))
(cond ((eq 'A p1)
(cond ((eq 'X p2)
(+ 3 0))
((eq 'Y p2)
(+ 1 3))
((eq 'Z p2)
(+ 2 6))))
((eq 'B p1)
(cond ((eq 'X p2)
(+ 1 0))
((eq 'Y p2)
(+ 2 3))
((eq 'Z p2)
(+ 3 6))))
((eq 'C p1)
(cond ((eq 'X p2)
(+ 2 0))
((eq 'Y p2)
(+ 3 3))
((eq 'Z p2)
(+ 1 6)))))))
(defun part-two (input)
(reduce '+ (mapcar #'calc-points-two (parse-rounds input))))