From 92f0857f8f3644e31739f63628156c4b0384b3ac Mon Sep 17 00:00:00 2001 From: Oscar Najera Date: Mon, 5 Dec 2022 00:04:50 +0100 Subject: [AoC2022] Elisp + common lisp 04 --- AoC2022/04/solver.lisp | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) (limited to 'AoC2022/04/solver.lisp') diff --git a/AoC2022/04/solver.lisp b/AoC2022/04/solver.lisp index 0a4077f..bcf37d7 100644 --- a/AoC2022/04/solver.lisp +++ b/AoC2022/04/solver.lisp @@ -4,23 +4,26 @@ "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 subcontained (a0 a1 b0 b1) + (or (subinterval a0 a1 b0 b1) + (subinterval b0 b1 a0 a1))) -(defun count-repeated () +(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-contained l)))) - + count (sections-fulfills function l)))) (fiveam:test results - (fiveam:is (= 515 (count-repeated))) - (fiveam:is (= 2567 (badge)))) - + (fiveam:is (= 515 (count-according #'subcontained))) + (fiveam:is (= 883 (count-according #'overlap)))) (fiveam:run-all-tests) -- cgit v1.2.3