advent-of-code-2023

My solutions to AoC 2023
git clone git://git.entf.net/advent-of-code-2023
Log | Files | Refs

commit d2f25f66d712a2d3cfdfa79a23a4df06dc2d8922
parent 562cace75e1d775536106561ec459bbdd51e5388
Author: Lukas Henkel <lh@entf.net>
Date:   Wed,  6 Dec 2023 06:31:09 +0100

Cleanup

Diffstat:
Msrc/day-6.lisp | 46+++++++++++++++++++++-------------------------
1 file changed, 21 insertions(+), 25 deletions(-)

diff --git a/src/day-6.lisp b/src/day-6.lisp @@ -3,40 +3,36 @@ (:export #:day-6)) (in-package #:aoc/day-6) -(defun task-1 (times distances) - (loop with task-1 = nil - for time in times - for distance in distances - do (push (loop for speed from 1 below time - for remaining-time = (- time speed) - for travelled = (* speed remaining-time) - when (> travelled distance) - sum 1 fixnum) - task-1) - finally (return (apply #'* task-1)))) +(declaim (ftype (function (fixnum fixnum fixnum) t) beats-distance-p) + (inline beats-distance-p)) +(defun beats-distance-p (speed time distance) + (let* ((remaining-time (- time speed)) + (travelled (the fixnum (* speed remaining-time)))) + (> travelled distance))) -(defun join-numbers (numbers) - (parse-integer (apply 'concatenate 'string - (mapcar #'write-to-string numbers)))) - -(defun task-2 (time distance) +(declaim (ftype (function (fixnum fixnum) fixnum) possible-ways-to-beat)) +(defun possible-ways-to-beat (time distance) (let ((start (loop for speed from 1 below time - for remaining-time = (- time speed) - for travelled = (* speed remaining-time) - when (> travelled distance) + when (beats-distance-p speed time distance) do (return speed))) (end (loop for speed from (1- time) downto 1 - for remaining-time = (- time speed) - for travelled = (* speed remaining-time) - when (> travelled distance) + when (beats-distance-p speed time distance) do (return speed)))) (- end start -1))) +(defun join-numbers (numbers) + (parse-integer (apply 'concatenate 'string + (mapcar #'write-to-string numbers)))) + (defun day-6 (input) (let* ((line (read-line input)) (times (read-number-list line :start (1+ (position #\: line)))) (line (read-line input)) (distances (read-number-list line :start (1+ (position #\: line))))) - (values (task-1 times distances) - (task-2 (join-numbers times) - (join-numbers distances))))) + (values (loop with task-1 fixnum = 1 + for time in times + for distance in distances + do (setf task-1 (* task-1 (possible-ways-to-beat time distance))) + finally (return task-1)) + (possible-ways-to-beat (join-numbers times) + (join-numbers distances)))))