day03.lisp (1519B)
1 (defpackage #:adventofcode2022/day03 2 (:use #:cl #:adventofcode2022)) 3 (in-package #:adventofcode2022/day03) 4 5 (defun calculate-priority (result) 6 (if (char>= result #\a) 7 (- (char-code result) 96) 8 (- (char-code result) 38))) 9 10 (defun task1 (inputs) 11 (loop for input in inputs 12 sum (loop with compartment-size = (/ (length input) 2) 13 with compartment1 = (subseq input 0 compartment-size) 14 with compartment2 = (subseq input compartment-size) 15 with use-item2 = nil 16 for item1 across compartment1 17 for item2 across compartment2 18 until (or 19 (find item1 compartment2) 20 (and 21 (find item2 compartment1) 22 (setf use-item2 t))) 23 finally (return 24 (calculate-priority (if use-item2 item2 item1)))))) 25 26 (defun task2 (inputs) 27 (loop with head = inputs 28 sum (loop for item = #\A then (cond ((char= item #\Z) #\a) 29 ((char= item #\z) nil) 30 (t (code-char (1+ (char-code item))))) 31 while item 32 until (loop for i from 0 to 2 33 always (find item (elt head i))) 34 finally (return (calculate-priority item))) 35 do (setf head (cdddr head)) 36 while head)) 37 38 (define-day 3 39 () 40 #'task1 41 #'task2)