advent-of-code-2023

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

day-6.lisp (1434B)


      1 (defpackage #:aoc/day-6
      2   (:use #:cl #:aoc/utils)
      3   (:export #:day-6))
      4 (in-package #:aoc/day-6)
      5 
      6 (declaim (ftype (function (fixnum fixnum fixnum) t) beats-distance-p)
      7          (inline beats-distance-p))
      8 (defun beats-distance-p (speed time distance)
      9   (let* ((remaining-time (- time speed))
     10          (travelled (the fixnum (* speed remaining-time))))
     11     (> travelled distance)))
     12 
     13 (declaim (ftype (function (fixnum fixnum) fixnum) possible-ways-to-beat))
     14 (defun possible-ways-to-beat (time distance)
     15   (let ((start (loop for speed from 1 below time
     16                      when (beats-distance-p speed time distance)
     17                        do (return speed))))
     18     (- time (* start 2) -1)))
     19 
     20 (defun join-numbers (numbers)
     21   (parse-integer (apply 'concatenate 'string
     22                         (mapcar #'write-to-string numbers))))
     23 
     24 (defun day-6 (input)
     25   (let* ((line (read-line input))
     26          (times (read-number-list line :start (1+ (position #\: line))))
     27          (line (read-line input))
     28          (distances (read-number-list line :start (1+ (position #\: line)))))
     29     (values (loop with task-1 fixnum = 1
     30                   for time in times
     31                   for distance in distances
     32                   do (setf task-1 (* task-1 (possible-ways-to-beat time distance)))
     33                   finally (return task-1))
     34             (possible-ways-to-beat (join-numbers times)
     35                                    (join-numbers distances)))))