aboutsummaryrefslogtreecommitdiffstats
path: root/AoC2023/day05
diff options
context:
space:
mode:
Diffstat (limited to 'AoC2023/day05')
-rw-r--r--AoC2023/day05/eg-in33
-rw-r--r--AoC2023/day05/solver.lisp50
2 files changed, 41 insertions, 42 deletions
diff --git a/AoC2023/day05/eg-in b/AoC2023/day05/eg-in
new file mode 100644
index 0000000..f756727
--- /dev/null
+++ b/AoC2023/day05/eg-in
@@ -0,0 +1,33 @@
+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
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