aboutsummaryrefslogtreecommitdiffstats
path: root/AoC2022/04/solver.el
blob: 854b247ea8b417685441a1dba485424418c305fa (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
(require 'subr-x)

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

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

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

(defun solver-sections-fulfills (func line)
  (apply func (mapcar #'string-to-number
                      (split-string line "[,-]"))))

(defun solver-count-according (func)
  (with-temp-buffer
    (insert-file-contents "input")
    (cl-loop for l = (buffer-substring-no-properties (line-beginning-position) (line-end-position))
             while (not (string-blank-p l))
             count (solver-sections-fulfills func l)
             do (forward-line))))

(ert-deftest test-problems ()
  (should (= 515 (solver-count-according #'solver-subcontained)))
  (should (= 883 (solver-count-according #'solver-overlap))))