diff options
Diffstat (limited to '2022/Day03/part-one.cl')
| -rw-r--r-- | 2022/Day03/part-one.cl | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/2022/Day03/part-one.cl b/2022/Day03/part-one.cl new file mode 100644 index 0000000..874f893 --- /dev/null +++ b/2022/Day03/part-one.cl @@ -0,0 +1,34 @@ +(defvar *prog-input*) + +(let ((ui (read-line))) + (if (equal ui "") + (setq *prog-input* *standard-input*) + (setq *prog-input* (open ui)))) + +(defun char-to-priority (c) + (if (and (char<= #\a c) (char<= c #\z)) + (- (char-int c) 96) ; (+1 (- (char-int c) (char-int #\a)) + (- (char-int c) 38))) ; (+ 27 (- (char-int c) (char-int #\A)) + +(let + ((inpt (read-line *prog-input* NIL)) (first-comp "") (second-comp "") (encountered '()) (priority-sum 0)) + + (loop until (or (string= inpt "end") (not inpt)) do + (setq encountered '()) + (setq first-comp (subseq inpt 0 (/ (length inpt) 2))) + (setq second-comp (subseq inpt (/ (length inpt) 2))) + + (loop for itype-first across first-comp do + (loop for itype-second across second-comp do + (if (and (char= itype-first itype-second) (not (member itype-first encountered))) + (progn (push itype-first encountered) + (setq priority-sum (+ priority-sum (char-to-priority itype-first))))))) + + (setq inpt (read-line *prog-input* NIL))) + + ;; (print (loop for itype in encountered sum (char-to-priority itype))) + (print priority-sum)) + +(if (not (eq *prog-input* *standard-input*)) + (close *prog-input*)) + |
