aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorOscar Najera <hi@oscarnajera.com>2023-12-06 08:00:35 +0100
committerOscar Najera <hi@oscarnajera.com>2023-12-06 08:16:30 +0100
commit92347d8e922055b7828f24f3a464552840078969 (patch)
treee30b730c153262088989ac59e67050025a5583d2
parentfe9fa0dd75e9e5255094e62e2bfcf9fb2205a359 (diff)
downloadscratch-92347d8e922055b7828f24f3a464552840078969.tar.gz
scratch-92347d8e922055b7828f24f3a464552840078969.tar.bz2
scratch-92347d8e922055b7828f24f3a464552840078969.zip
day05 bug finally found and fixed
I had poor control of bounds. Right simple tests, it is not that hard, debugging by reading takes forever.
-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")))))