From 04131d1772e023f3564d3e17b2922d9b0ba373c9 Mon Sep 17 00:00:00 2001 From: Matteo Landi Date: Sun, 28 Jan 2024 14:44:27 +0100 Subject: [PATCH] Refactor 2023/15 - Use DEFINE-SOLUTION / DEFINE-TEST - Use (ENUMERATE :START), instead of bunch of 1+ --- src/2023/day15.lisp | 45 +++++++++++++++------------------------------ 1 file changed, 15 insertions(+), 30 deletions(-) diff --git a/src/2023/day15.lisp b/src/2023/day15.lisp index 02c995d..facbe3e 100644 --- a/src/2023/day15.lisp +++ b/src/2023/day15.lisp @@ -1,42 +1,17 @@ (defpackage :aoc/2023/15 #.cl-user::*aoc-use*) (in-package :aoc/2023/15) -(defun part1 (&optional (strings (uiop:read-file-lines #P"src/2023/day15.txt"))) - (bnd1 (s (first strings)) - (bnd1 (parts (split-sequence:split-sequence #\, s)) - (looping - (dolist (each parts) - (bnd1 (value 0) - (doseq (ch each) - (setf value (rem (* (+ (char-code ch) value) 17) 256))) - (sum! value))))))) -#+#:excluded (part1) - - (defun hash (s) (bnd1 (value 0) (doseq (ch s) (setf value (rem (* (+ (char-code ch) value) 17) 256))) value)) -#+#:excluded (hash "rn") -(defun part1 (&optional (strings (uiop:read-file-lines #P"src/2023/day15.txt")) - &aux (s (first strings))) - (looping - (dolist (part (split-sequence:split-sequence #\, s)) - (sum! (hash part))))) -#+#:excluded (part1) -; 510013 +(defun part1 (&optional (s (first (aoc::read-problem-input 2023 15)))) + (reduce #'+ (split-sequence:split-sequence #\, s) :key #'hash)) -(defun focusing-power (boxes) - (looping - (dorange (i 0 (length boxes)) - (bnd1 (box (aref boxes i)) - (doseq ((j (label . num)) (enumerate (reverse box))) - (sum! (* (1+ i) (1+ j) num))))))) -(defun part2 (&optional (strings (uiop:read-file-lines #P"src/2023/day15.txt")) - &aux (s (first strings))) +(defun part2 (&optional (s (first (aoc::read-problem-input 2023 15)))) (bnd1 (boxes (make-array 256 :initial-element nil)) (dolist (part (split-sequence:split-sequence #\, s)) (cl-ppcre:register-groups-bind (label op num) @@ -48,5 +23,15 @@ (setf (cdr it) (parse-integer num)) (push (cons label (parse-integer num)) (aref boxes i))))))) (focusing-power boxes))) -#+#:excluded (part2) -; 268497 + +(defun focusing-power (boxes) + (looping + (doseq ((i box) (enumerate boxes :start 1)) + (doseq ((j (label . num)) (enumerate (reverse box) :start 1)) + (sum! (* i j num)))))) + + +(define-solution (2023 15) (s first) + (values (part1 s) (part2 s))) + +(define-test (2023 15) (510013 268497))