diff options
author | Oscar Najera <hi@oscarnajera.com> | 2023-12-06 08:57:43 +0100 |
---|---|---|
committer | Oscar Najera <hi@oscarnajera.com> | 2023-12-06 08:57:43 +0100 |
commit | 914efa4071448ae8005784c45b9dc2b028e19d47 (patch) | |
tree | 3f596d9fcaf1515f284fd19e73db284c4c65d60b /AoC2023/day06 | |
parent | 92347d8e922055b7828f24f3a464552840078969 (diff) | |
download | scratch-914efa4071448ae8005784c45b9dc2b028e19d47.tar.gz scratch-914efa4071448ae8005784c45b9dc2b028e19d47.tar.bz2 scratch-914efa4071448ae8005784c45b9dc2b028e19d47.zip |
[AoC2023] day06 lisp part 1
Diffstat (limited to 'AoC2023/day06')
-rw-r--r-- | AoC2023/day06/input | 2 | ||||
-rw-r--r-- | AoC2023/day06/solver.lisp | 40 |
2 files changed, 42 insertions, 0 deletions
diff --git a/AoC2023/day06/input b/AoC2023/day06/input new file mode 100644 index 0000000..973ba45 --- /dev/null +++ b/AoC2023/day06/input @@ -0,0 +1,2 @@ +Time: 47 70 75 66 +Distance: 282 1079 1147 1062 diff --git a/AoC2023/day06/solver.lisp b/AoC2023/day06/solver.lisp new file mode 100644 index 0000000..460b554 --- /dev/null +++ b/AoC2023/day06/solver.lisp @@ -0,0 +1,40 @@ +(ql:quickload '(fiveam str arrows)) + +(defparameter eg-input "Time: 7 15 30 +Distance: 9 40 200") + +(defun bounds (time r &optional lower) + (multiple-value-bind (point reminder) + (if lower + (ceiling (/ (- time (sqrt (- (* time time) (* 4 r)))) 2)) + (floor (/ (+ time (sqrt (- (* time time) (* 4 r)))) 2))) + (if (zerop reminder) + (funcall (if lower #'1+ #'1-) point) + point))) + +(defun charge-time-solutions (pair) + (destructuring-bind (race-time . record-distance) pair + ;; quadratic equation: -charge_time**2 + race_time*charge_time - record_distance + (let ((lower (bounds race-time record-distance 'lower)) + (upper (bounds race-time record-distance))) + (1+ (- upper lower))))) + +(defun solver1 (lines) + (arrows:->> + lines + (mapcar + (lambda (line) + (mapcar #'parse-integer (cdr (str:split-omit-nulls #\Space line))))) + (apply #'mapcar #'cons) + (mapcar #'charge-time-solutions) + (apply #'*))) + +(fiveam:test solutions + (fiveam:is + (= 288 + (solver1 + (uiop:split-string eg-input :separator '(#\Newline))))) + (fiveam:is + (= 281600 + (solver1 + (uiop:read-file-lines "input"))))) |