aboutsummaryrefslogtreecommitdiff
path: root/2022/Day03/part-two.cl
blob: fcde7ed0b111da8d24daf3aa9f0d93f71eeb4a88 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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*))