From 93db30046a4e4c12c89cf2ab3bc6e8a5861cbe48 Mon Sep 17 00:00:00 2001 From: Peter Tillemans Date: Mon, 23 Dec 2024 14:15:25 +0100 Subject: [PATCH] solved day21 part 2. --- src/2024/day21.lisp | 33 +++++++++++++++++++++++++-------- 1 file changed, 25 insertions(+), 8 deletions(-) diff --git a/src/2024/day21.lisp b/src/2024/day21.lisp index 926b0f8..62098b8 100644 --- a/src/2024/day21.lisp +++ b/src/2024/day21.lisp @@ -262,6 +262,11 @@ sum (let ((ll (gethash (list layer ki kf) leg-lengths))) ll))) +(defun press-move (d pc mc) + (loop + for i from 1 to (abs d) + collect (if (plusp d) pc mc))) + (defun fill-leg-table (n-robots) (setf leg-lengths (make-hash-table :test #'equal)) @@ -298,19 +303,15 @@ do (let* ( (dx (- xf xi)) (dy (- yf yi)) - (hp (loop - for x from 0 below (abs dx) - collect (if (plusp dx) #\> #\<))) - (vp (loop - for y from 0 below (abs dy) - collect (if (minusp dy) #\^ #\v))) + (hp (press-move dx #\> #\<)) + (vp (press-move dy #\v #\^)) (hf (coerce (append hp vp '(#\A)) 'string)) (h-first (if (equal (cons xf yi) space-pos) - 999 + 99999999999 (fewest-presses (1- layer) hf))) (vf (coerce (append vp hp '(#\A)) 'string)) (v-first (if (equal (cons xi yf) space-pos) - 999 + 99999999999 (fewest-presses (1- layer) vf)))) ;;(format t "h ~A ~A ~A ~A~%" xi yf hf h-first) ;;(format t "v ~A ~A ~A ~A~%" xf yi vf v-first) @@ -322,6 +323,13 @@ )) ) + +(defun print-leg-lengths (stream) + (loop + for k being the hash-keys in leg-lengths + for v being the hash-values in leg-lengths + do (format stream "~2,'0D,~A,~A,~A~%" (first k) (second k) (third k) v))) + (defun complexity (code n) (let ((l (fewest-presses n code)) (v (parse-integer (ppcre:scan-to-strings "\\d+" code)))) @@ -339,6 +347,15 @@ do (format t "~15A~%" (fewest-presses n code)) ))) +(defun write-leg-lengths-to-file (filename) + (with-open-file (str filename + :direction :output + :if-exists :supersede + :if-does-not-exist :create) + (print-leg-lengths str))) + + + (defun part1 (data) (fill-leg-table 3)