aboutsummaryrefslogtreecommitdiffstats
path: root/AoC2023/day19/eg-in
blob: e5b5d647aa4011e02a391a7a8a08fa32ee399cbd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
px{a<2006:qkq,m>2090:A,rfg}
pv{a>1716:R,A}
lnx{m>1548:A,A}
rfg{s<537:gd,x>2440:R,A}
qs{s>3448:A,lnx}
qkq{x<1416:A,crn}
crn{x>2662:A,R}
in{s<1351:px,qqz}
qqz{s>2770:qs,m<1801:hdj,R}
gd{a>3333:R,R}
hdj{m>838:A,pv}

{x=787,m=2655,a=1222,s=2876}
{x=1679,m=44,a=2067,s=496}
{x=2036,m=264,a=79,s=2244}
{x=2461,m=1339,a=466,s=291}
{x=2127,m=1623,a=2188,s=1013}
color: #89DDFF } /* Name.Variable.Global */ .highlight .vi { color: #89DDFF } /* Name.Variable.Instance */ .highlight .vm { color: #82AAFF } /* Name.Variable.Magic */ .highlight .il { color: #F78C6C } /* Literal.Number.Integer.Long */
(ql:quickload :fiveam)

(defun recover-item (bitfield-int)
  (loop for i from 0
        when (logbitp i bitfield-int) return i))

(defun priority (value)
  (if (< value 31) ;; It is a capital letter in range [1;26]
      (+ value 26)
    (- value 32)))

(defun line-priority (str)
  (let ((mid (/ (length str) 2))
        (left-pack 0) (right-pack 0))
    (loop with cross = 0
          for l across (subseq str 0 mid)
          for r across (subseq str mid)
          do (progn
               (setf left-pack (logior left-pack (ash 1 (- (char-code l) 64))))
               (setf right-pack (logior right-pack (ash 1 (- (char-code r) 64))))
               (setf cross (logand left-pack right-pack)))
          when (< 0 cross) return (priority (recover-item cross)))))


(defun prio ()
  (with-open-file (in "input")
    (loop :for l = (read-line in nil nil)
          :while l
          :sum (line-priority l))))

(defun pick-badge (str)
  (loop with stack = 0
        for i across str
        do (setf stack (logior stack (ash 1 (- (char-code i) 64))))
        finally (return stack)))

(defun badge ()
  (with-open-file (in "input")
    (loop :for b = (loop repeat 3 for l = (read-line in nil nil) while l collect (pick-badge l))
          :while b
          :sum (priority (recover-item (apply #'logand b))))))

;; (pick-badge "LdHVLDLDdHdtLMhcqCqGWcWg" )

(fiveam:test intermediate
             (fiveam:is (= 12 (recover-item 4096)))
             (fiveam:is (= 20 (line-priority
                               "gzCjffWZCtCfZZVdqVSqJdvJndSt"))))

(fiveam:test results
  (fiveam:is (= 8072 (prio)))
  (fiveam:is (= 2567 (badge))))