solved day21 part 2.

This commit is contained in:
Peter Tillemans 2024-12-23 14:15:25 +01:00
parent 739ee1ba53
commit 93db30046a

View file

@ -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)