commitadbeaddc3cfb996a88ca94ee3ef197a98be8d173parenta77310d87bc6eec68bd446da4469f4fc44790cd2Author:Lukas Henkel <lh@entf.net>Date:Tue, 5 Dec 2023 07:17:13 +0100 Day 5 task 2 This took a long time to run. Needs to be optimized.Diffstat:

M | src/day-5.lisp | | | 30 | ++++++++++++++++++++---------- |

M | t/day-5.lisp | | | 5 | +++-- |

2 files changed, 23 insertions(+), 12 deletions(-)diff --git a/src/day-5.lisp b/src/day-5.lisp@@ -25,19 +25,29 @@ while (and line (string/= line "")) collect (read-number-list line)))) +(declaim (ftype (function (fixnum list) fixnum) map-number)) (defun map-number (number map) - (or (loop for (dest source length) in map - for diff = (- number source) + (or (loop for (dest source length) (fixnum fixnum fixnum) in map + for diff fixnum = (- number source) when (and (>= diff 0) (< diff length)) - do (return (+ dest diff))) + do (return (the fixnum (+ dest diff)))) number)) +(defun map-seed (seed maps) + (loop with n = seed + for map in maps + do (setf n (map-number n map)) + finally (return n))) + (defun day-5 (input) - (loop with seeds = (parse-seeds input) - with maps = (parse-maps input) - for seed in seeds - minimize (loop with n = seed - for map in maps - do (setf n (map-number n map)) - finally (return n)))) + (let ((seeds (parse-seeds input)) + (maps (parse-maps input))) + (values + (loop for seed in seeds + minimize (map-seed seed maps) fixnum) + (loop for (seed-start seed-length) on seeds by #'cddr + minimize (loop for seed from seed-start + repeat seed-length + minimize (map-seed seed maps) fixnum) fixnum + do (format t "Seed range ~A ~A done!~%" seed-start seed-length)))))diff --git a/t/day-5.lisp b/t/day-5.lisp@@ -4,7 +4,7 @@ (define-test test-day-5 () - (multiple-value-bind (task-1) + (multiple-value-bind (task-1 task-2) (aoc:run-day 5 "seeds: 79 14 55 13 seed-to-soil map: @@ -38,4 +38,5 @@ temperature-to-humidity map: humidity-to-location map: 60 56 37 56 93 4") - (assert= 35 task-1))) + (assert= 35 task-1) + (assert= 46 task-2)))