advent-of-code-2023

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

commit 4cb0b879d1a96dd9a5afd45be2a80fcf631faeb4
parent 26569d70e0e3c2a53f2a975ca9c6d741617d16d5
Author: Lukas Henkel <lh@entf.net>
Date:   Sat,  2 Dec 2023 06:57:44 +0100

Handwritten parser not using str:split

Diffstat:
Msrc/day-2.lisp | 34+++++++++++++++++++++++-----------
1 file changed, 23 insertions(+), 11 deletions(-)

diff --git a/src/day-2.lisp b/src/day-2.lisp @@ -3,18 +3,30 @@ (:export #:day-2)) (in-package #:aoc/day-2) +(defun parse-sets (line start) + (loop with sets = nil + with current-set = nil + for pos from start below (length line) + do (multiple-value-bind (n end) + (parse-integer line :start pos :junk-allowed t) + (setf pos end) + (setf end (position-if (lambda (char) + (or (char= #\, char) + (char= #\; char))) + line + :start pos)) + (setf (getf current-set (make-keyword (upcase (subseq line (1+ pos) end)))) n + pos (or end (1- (length line))))) + when (char= (aref line pos) #\;) + do (push current-set sets) + and do (setf current-set nil) + finally (return (cons current-set sets)))) + (defun parse-game-line (line) - (let* ((game-cubes (str:split ": " line)) - (game (parse-integer (subseq (first game-cubes) 5))) - (sets (mapcar (lambda (set) - (let ((cubes (str:split ", " set))) - (mapcan (lambda (cube-color) - (let ((cube-color (str:split " " cube-color))) - (list (make-keyword (string-upcase (second cube-color))) - (parse-integer (first cube-color))))) - cubes))) - (str:split "; " (second game-cubes))))) - (list game sets))) + (let* ((start-game-id 5) + (end-game-id (position #\: line))) + (list (parse-integer line :start start-game-id :end end-game-id) + (parse-sets line (1+ end-game-id))))) (defun game-possible-p (sets) (loop for set in sets