aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorOscar Najera <hi@oscarnajera.com>2022-12-05 00:04:50 +0100
committerOscar Najera <hi@oscarnajera.com>2022-12-05 00:04:50 +0100
commit92f0857f8f3644e31739f63628156c4b0384b3ac (patch)
tree61b266d79fd2575e208718339e848f7c93adb293
parent976fafe4bfee6b66218b0ce0f0481624c5fe2f02 (diff)
downloadscratch-92f0857f8f3644e31739f63628156c4b0384b3ac.tar.gz
scratch-92f0857f8f3644e31739f63628156c4b0384b3ac.tar.bz2
scratch-92f0857f8f3644e31739f63628156c4b0384b3ac.zip
[AoC2022] Elisp + common lisp 04
-rw-r--r--AoC2022/04/makefile2
-rw-r--r--AoC2022/04/solver.el29
-rw-r--r--AoC2022/04/solver.lisp27
3 files changed, 45 insertions, 13 deletions
diff --git a/AoC2022/04/makefile b/AoC2022/04/makefile
index ee6a716..f4940ca 100644
--- a/AoC2022/04/makefile
+++ b/AoC2022/04/makefile
@@ -10,5 +10,5 @@
run:
sbcl --load ~/.sbclrc --script solver.lisp
- # emacs -batch -l ert -l solver.el -f ert-run-tests-batch-and-exit
+ emacs -batch -l ert -l solver.el -f ert-run-tests-batch-and-exit
# rustc solver.rs && ./solver
diff --git a/AoC2022/04/solver.el b/AoC2022/04/solver.el
new file mode 100644
index 0000000..854b247
--- /dev/null
+++ b/AoC2022/04/solver.el
@@ -0,0 +1,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))))
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)