adventofcode2022

My solutions for Advent of Code 2022
Log | Files | Refs

day02.lisp (1185B)


      1 (defpackage #:adventofcode2022/day02
      2   (:use #:cl #:adventofcode2022))
      3 (in-package #:adventofcode2022/day02)
      4 
      5 (defun task1 (inputs)
      6   (let ((score 0))
      7     (dolist (input inputs)
      8       (if (= (car input) (cdr input))
      9           (incf score 3)
     10           (trivia:match input
     11             ((or (cons 1 2)
     12                  (cons 2 3)
     13                  (cons 3 1))
     14              (incf score 6))))
     15       (incf score (cdr input)))
     16     score))
     17 
     18 (defun task2 (inputs)
     19   (let ((score 0))
     20     (dolist (input inputs)
     21       (cond
     22         ((= (cdr input) 2)
     23          (incf score 3))
     24         ((= (cdr input) 3)
     25          (incf score 6)))
     26       (if (= (cdr input) 2)
     27           (incf score (car input))
     28           (trivia:match input
     29             ((or (cons 1 1)
     30                  (cons 2 3))
     31              (incf score 3))
     32             ((or (cons 1 3)
     33                  (cons 3 1))
     34              (incf score 2))
     35             ((or (cons 2 1)
     36                  (cons 3 3))
     37              (incf score 1)))))
     38     score))
     39 
     40 (define-day 2
     41     (:translate-input (lambda (line)
     42                         (cons (- (char-code (aref line 0)) 64)
     43                               (- (char-code (aref line 2)) 87))))
     44   #'task1
     45   #'task2)