aboutsummaryrefslogtreecommitdiff
path: root/2022/Day03/part-one.cl
blob: 874f89310fcac2134f8f1e2c35d2822d92137608 (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
(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*))