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))) sum (let ((ll (gethash (list layer ki kf) leg-lengths)))
ll))) 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) (defun fill-leg-table (n-robots)
(setf leg-lengths (make-hash-table :test #'equal)) (setf leg-lengths (make-hash-table :test #'equal))
@ -298,19 +303,15 @@
do (let* ( do (let* (
(dx (- xf xi)) (dx (- xf xi))
(dy (- yf yi)) (dy (- yf yi))
(hp (loop (hp (press-move dx #\> #\<))
for x from 0 below (abs dx) (vp (press-move dy #\v #\^))
collect (if (plusp dx) #\> #\<)))
(vp (loop
for y from 0 below (abs dy)
collect (if (minusp dy) #\^ #\v)))
(hf (coerce (append hp vp '(#\A)) 'string)) (hf (coerce (append hp vp '(#\A)) 'string))
(h-first (if (equal (cons xf yi) space-pos) (h-first (if (equal (cons xf yi) space-pos)
999 99999999999
(fewest-presses (1- layer) hf))) (fewest-presses (1- layer) hf)))
(vf (coerce (append vp hp '(#\A)) 'string)) (vf (coerce (append vp hp '(#\A)) 'string))
(v-first (if (equal (cons xi yf) space-pos) (v-first (if (equal (cons xi yf) space-pos)
999 99999999999
(fewest-presses (1- layer) vf)))) (fewest-presses (1- layer) vf))))
;;(format t "h ~A ~A ~A ~A~%" xi yf hf h-first) ;;(format t "h ~A ~A ~A ~A~%" xi yf hf h-first)
;;(format t "v ~A ~A ~A ~A~%" xf yi vf v-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) (defun complexity (code n)
(let ((l (fewest-presses n code)) (let ((l (fewest-presses n code))
(v (parse-integer (ppcre:scan-to-strings "\\d+" code)))) (v (parse-integer (ppcre:scan-to-strings "\\d+" code))))
@ -339,6 +347,15 @@
do (format t "~15A~%" (fewest-presses n code)) 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) (defun part1 (data)
(fill-leg-table 3) (fill-leg-table 3)