advent-of-code-2023

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

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

Day 6 task 2

Diffstat:
Msrc/day-6.lisp | 42++++++++++++++++++++++++++++++++----------
Mt/day-6.lisp | 5+++--
2 files changed, 35 insertions(+), 12 deletions(-)

diff --git a/src/day-6.lisp b/src/day-6.lisp @@ -3,18 +3,40 @@ (: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)))) + +(defun join-numbers (numbers) + (parse-integer (apply 'concatenate 'string + (mapcar #'write-to-string numbers)))) + +(defun task-2 (time distance) + (let ((start (loop for speed from 1 below time + for remaining-time = (- time speed) + for travelled = (* speed remaining-time) + when (> travelled 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) + do (return speed)))) + (- end start -1))) + (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))))) - (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))))) + (values (task-1 times distances) + (task-2 (join-numbers times) + (join-numbers distances))))) diff --git a/t/day-6.lisp b/t/day-6.lisp @@ -4,7 +4,8 @@ (define-test test-day-6 () - (multiple-value-bind (task-1) + (multiple-value-bind (task-1 task-2) (aoc:run-day 6 "Time: 7 15 30 Distance: 9 40 200") - (assert= 288 task-1))) + (assert= 288 task-1) + (assert= 71503 task-2)))