diff options
| author | Syndamia <kamen@syndamia.com> | 2022-12-03 09:49:17 +0200 |
|---|---|---|
| committer | Syndamia <kamen@syndamia.com> | 2022-12-03 09:49:17 +0200 |
| commit | 39d5d4f3957971979cf65ad3fa505fb93274c2ee (patch) | |
| tree | c134731ab25ef70e55fa3cb42908b7d2e66bd4dd /2022/Day03/part-two.cl | |
| parent | 15aab5d23ae97f4dd498bf3460be1ef22e31f3b5 (diff) | |
| download | advent-of-code-39d5d4f3957971979cf65ad3fa505fb93274c2ee.tar advent-of-code-39d5d4f3957971979cf65ad3fa505fb93274c2ee.tar.gz advent-of-code-39d5d4f3957971979cf65ad3fa505fb93274c2ee.zip | |
[2022/D03] Solved both tasks from today
Diffstat (limited to '2022/Day03/part-two.cl')
| -rw-r--r-- | 2022/Day03/part-two.cl | 39 |
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*)) |
