advent-of-code-2023

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

commit a6412e56e85d2bd5343b60fc7d431351c52c2fce
parent df1d03bd1110063b94c895095dd5b9ea889c728a
Author: Lukas Henkel <lh@entf.net>
Date:   Wed,  6 Dec 2023 06:16:36 +0100

Day 6 task 1

Diffstat:
Ainput/6.txt | 2++
Msrc/day-5.lisp | 9---------
Asrc/day-6.lisp | 20++++++++++++++++++++
Msrc/utils.lisp | 12+++++++++++-
At/day-6.lisp | 10++++++++++
5 files changed, 43 insertions(+), 10 deletions(-)

diff --git a/input/6.txt b/input/6.txt @@ -0,0 +1,2 @@ +Time: 47 98 66 98 +Distance: 400 1213 1011 1540 diff --git a/src/day-5.lisp b/src/day-5.lisp @@ -3,15 +3,6 @@ (:export #:day-5)) (in-package #:aoc/day-5) -(defun read-number-list (string &key (start 0)) - (loop for i from start below (length string) - collect (multiple-value-bind (number end) - (parse-integer string - :start i - :junk-allowed t) - (setf i end) - number))) - (defun parse-seeds (input) (let ((line (prog1 (read-line input) diff --git a/src/day-6.lisp b/src/day-6.lisp @@ -0,0 +1,20 @@ +(defpackage #:aoc/day-6 + (:use #:cl #:aoc/utils) + (:export #:day-6)) +(in-package #:aoc/day-6) + +(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))))) diff --git a/src/utils.lisp b/src/utils.lisp @@ -16,7 +16,8 @@ #:point-x #:point-y #:point-neighbours - #:do-map-neighbours)) + #:do-map-neighbours + #:read-number-list)) (in-package #:aoc/utils) (defun normalize-type (type) @@ -160,3 +161,12 @@ (push bb? checks)) checks)) ,@body))))))) + +(defun read-number-list (string &key (start 0)) + (loop for i from start below (length string) + collect (multiple-value-bind (number end) + (parse-integer string + :start i + :junk-allowed t) + (setf i end) + number))) diff --git a/t/day-6.lisp b/t/day-6.lisp @@ -0,0 +1,10 @@ +(defpackage #:aoc-test/day-6 + (:use #:cl #:lisp-unit2)) +(in-package #:aoc-test/day-6) + +(define-test test-day-6 + () + (multiple-value-bind (task-1) + (aoc:run-day 6 "Time: 7 15 30 +Distance: 9 40 200") + (assert= 288 task-1)))