From 92347d8e922055b7828f24f3a464552840078969 Mon Sep 17 00:00:00 2001
From: Oscar Najera <hi@oscarnajera.com>
Date: Wed, 6 Dec 2023 08:00:35 +0100
Subject: 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.
---
 AoC2023/day05/solver.lisp | 55 ++++++++++++++++-------------------------------
 1 file changed, 18 insertions(+), 37 deletions(-)

(limited to 'AoC2023/day05')

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")))))
-- 
cgit v1.2.3