aboutsummaryrefslogtreecommitdiffstats
path: root/AoC2023/day05/solver.lisp
diff options
context:
space:
mode:
authorOscar Najera <hi@oscarnajera.com>2023-12-29 12:44:08 +0100
committerOscar Najera <hi@oscarnajera.com>2023-12-29 12:44:08 +0100
commit92b5b7b716b6f6906b1432a092571ae712e56ac7 (patch)
tree66b02ac7ed51ebd54802b79e1706714a71cb7470 /AoC2023/day05/solver.lisp
parentaa5641e8219672819c629954c478f484a4841ea6 (diff)
downloadscratch-92b5b7b716b6f6906b1432a092571ae712e56ac7.tar.gz
scratch-92b5b7b716b6f6906b1432a092571ae712e56ac7.tar.bz2
scratch-92b5b7b716b6f6906b1432a092571ae712e56ac7.zip
refactor
Diffstat (limited to 'AoC2023/day05/solver.lisp')
-rw-r--r--AoC2023/day05/solver.lisp50
1 files changed, 8 insertions, 42 deletions
diff --git a/AoC2023/day05/solver.lisp b/AoC2023/day05/solver.lisp
index a97f4f7..6eb9b44 100644
--- a/AoC2023/day05/solver.lisp
+++ b/AoC2023/day05/solver.lisp
@@ -1,38 +1,4 @@
-(ql:quickload '(fiveam str))
-
-(defparameter eg-input "seeds: 79 14 55 13
-
-seed-to-soil map:
-50 98 2
-52 50 48
-
-soil-to-fertilizer map:
-0 15 37
-37 52 2
-39 0 15
-
-fertilizer-to-water map:
-49 53 8
-0 11 42
-42 0 7
-57 7 4
-
-water-to-light map:
-88 18 7
-18 25 70
-
-light-to-temperature map:
-45 77 23
-81 45 19
-68 64 13
-
-temperature-to-humidity map:
-0 69 1
-1 0 69
-
-humidity-to-location map:
-60 56 37
-56 93 4")
+(ql:quickload '(fiveam str arrows))
(defun parse-rules (rule)
(destructuring-bind (dest source span) rule
@@ -74,12 +40,12 @@ humidity-to-location map:
((push (parse-rules (mapcar #'parse-integer (str:split-omit-nulls #\Space line)))
(gethash (car maps-stack) translators)))))
- (loop for rule-name being the hash-key of translators do
- (setf (gethash rule-name translators)
- (sort (gethash rule-name translators) #'< :key #'car)))
+ (maphash (lambda (name rule)
+ (setf (gethash name translators) (sort rule #'< :key #'car)))
+ translators)
(setf maps-stack (nreverse maps-stack))
- (transformation-in-order-p maps-stack)
- (values maps-stack translators seeds)))
+ (when (transformation-in-order-p maps-stack)
+ (values maps-stack translators seeds))))
(defun translate-range (rules ranges &optional translated-ranges)
@@ -167,11 +133,11 @@ humidity-to-location map:
(fiveam:is
(= 35
(solver1
- (uiop:split-string eg-input :separator '(#\Newline)))))
+ (uiop:read-file-lines "eg-in"))))
(fiveam:is
(= 46
(solver2
- (uiop:split-string eg-input :separator '(#\Newline)))))
+ (uiop:read-file-lines "eg-in"))))
(fiveam:is
(= 662197086
(solver1