aboutsummaryrefslogtreecommitdiffstats
path: root/AoC2022/04/solver.lisp
blob: bcf37d7c90bca103522c2e095e21e89e556914ef (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
27
28
29
(ql:quickload '(:fiveam :uiop))

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

(defun subcontained (a0 a1 b0 b1)
  (or (subinterval a0 a1 b0 b1)
      (subinterval b0 b1 a0 a1)))

(defun overlap (a0 a1 b0 b1)
  "Test if [b0;b1] overlaps [a0;a1]"
  (and (<= a0 b1) (<= b0 a1)))

(defun sections-fulfills (function line)
  (apply function (mapcar #'parse-integer
                          (uiop:split-string line :separator '(#\, #\-)))))

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

(fiveam:test results
  (fiveam:is (= 515 (count-according #'subcontained)))
  (fiveam:is (= 883 (count-according #'overlap))))

(fiveam:run-all-tests)