aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--AoC2023/day05/solver.lisp55
1 files changed, 18 insertions, 37 deletions
diff --git a/AoC2023/day05/solver.lisp b/AoC2023/day05/solver.lisp
index 47eb491..a97f4f7 100644
--- a/AoC2023/day05/solver.lisp
+++ b/AoC2023/day05/solver.lisp
@@ -1,4 +1,4 @@
-(ql:quickload '(fiveam str arrows))
+(ql:quickload '(fiveam str))
(defparameter eg-input "seeds: 79 14 55 13
@@ -119,16 +119,27 @@ humidity-to-location map:
(cons (list rule-end element-end)
(cdr ranges))
(cons (list (+ rule-offset element-start)
- (+ rule-offset element-end))
+ (+ rule-offset rule-end))
translated-ranges)))
((assert nil nil "Not all cases processed")))))))
+(fiveam:test range-translation
+ (let ((single-rule '((5 10 3))))
+ (fiveam:is (equal '((16 18)) (translate-range single-rule '((16 18))))
+ "Right sided element")
+ (fiveam:is (equal '((1 4)) (translate-range single-rule '((1 4))))
+ "Left sided element")
+ (fiveam:is (equal '((9 11)) (translate-range single-rule '((6 8))))
+ "Fully contained")
+ (fiveam:is (equal '((3 5) (8 10)) (translate-range single-rule '((3 7))))
+ "Left overlap")
+ (fiveam:is (equal '((10 13) (12 13)) (translate-range single-rule '((9 13))))
+ "Right overlap")))
(defun translator (translate-chain translator-rules)
(lambda (value)
(reduce
(lambda (acc fn)
- (print (list fn acc))
(translate (gethash fn translator-rules) acc))
translate-chain
:initial-value value)))
@@ -145,17 +156,12 @@ humidity-to-location map:
(defun solver2 (lines)
(multiple-value-bind (maps-stack translator seeds) (parser lines)
- (print (list 'seeds (seed-to-ranges seeds)))
- (arrows:-<>
+ (caar
(reduce
(lambda (acc fn)
- (let ((res
- (translate-range (gethash fn translator) acc)))
- (print (list fn res))
- res))
+ (translate-range (gethash fn translator) acc))
maps-stack
- :initial-value (seed-to-ranges seeds))
- (reduce #'min <> :key #'car))))
+ :initial-value (seed-to-ranges seeds)))))
(fiveam:test solutions
(fiveam:is
@@ -173,29 +179,4 @@ humidity-to-location map:
(fiveam:is
(= 52510809
(solver2
- (uiop:read-file-lines "input"))))
- )
-
-;; (multiple-value-bind (maps-stack translator seeds) (parser
-;; ;; (uiop:split-string eg-input :separator '(#\Newline))
-;; (uiop:read-file-lines "input")
-;; )
-;; (arrows:-<>
-;; (reduce
-;; (lambda (acc fn)
-;; (let ((res
-;; (translate-range (gethash fn translator) acc)))
-;; (print (list fn res))
-;; res))
-;; maps-stack
-;; :initial-value (mapcar
-;; (lambda (s) (list s (1+ s)))
-;; seeds))
-;; ;; (reduce #'min <> :key #'car)
-;; ))
-
-(multiple-value-bind (maps-stack translator seeds) (parser
- ;; (uiop:split-string eg-input :separator '(#\Newline))
- (uiop:read-file-lines "input")
- )
- (sort (mapcar (translator maps-stack translator) seeds) #'<))
+ (uiop:read-file-lines "input")))))