aboutsummaryrefslogtreecommitdiff
path: root/2022/Day03/part-two.cl
diff options
context:
space:
mode:
Diffstat (limited to '2022/Day03/part-two.cl')
-rw-r--r--2022/Day03/part-two.cl39
1 files changed, 39 insertions, 0 deletions
diff --git a/2022/Day03/part-two.cl b/2022/Day03/part-two.cl
new file mode 100644
index 0000000..fcde7ed
--- /dev/null
+++ b/2022/Day03/part-two.cl
@@ -0,0 +1,39 @@
+(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))
+
+(defun str-to-uniq-list (str)
+ (let ((out '()))
+ (loop for c across str do
+ (if (not (member c out))
+ (push c out)))
+ out))
+
+(let
+ ((inpt (read-line *prog-input* NIL)) (line-cnt 0) (encountered '()) (badge-prior 0))
+
+ (loop until (or (string= inpt "end") (not inpt)) do
+ (if (= line-cnt 0)
+ (setq encountered (str-to-uniq-list inpt))
+ (loop for c in encountered do
+ (if (not (find c inpt))
+ (setq encountered (remove c encountered)))))
+
+ (if (= line-cnt 2)
+ (setq badge-prior (+ badge-prior (char-to-priority (first encountered)))))
+
+ (setq line-cnt (mod (1+ line-cnt) 3))
+ (setq inpt (read-line *prog-input* NIL)))
+
+ (print badge-prior))
+
+(if (not (eq *prog-input* *standard-input*))
+ (close *prog-input*))