aboutsummaryrefslogtreecommitdiffstats
path: root/AoC2022/04/solver.lisp
blob: 0a4077f0d44a2e5edf8b10a641aaa26b6802c026 (plain)
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
(ql:quickload '(:fiveam :uiop))

(defun subinterval (a0 a1 b0 b1)
  "Test if [b0;b1] within [a0;a1]"
  (and (<= a0 b0 ) (>= a1 b1)))

(defun sections-contained (line)
  (destructuring-bind (a0 a1 b0 b1)
      (mapcar #'parse-integer
              (uiop:split-string line :separator '(#\, #\-)))
    (or (subinterval a0 a1 b0 b1)
        (subinterval b0 b1 a0 a1))))

(defun count-repeated ()
  (with-open-file (in "input")
    (loop for l = (read-line in nil nil)
          while l
          count (sections-contained l))))


(fiveam:test results
  (fiveam:is (= 515 (count-repeated)))
  (fiveam:is (= 2567 (badge))))


(fiveam:run-all-tests)