FINALLY figured out puzzle 10 2022

Part 2 was a real struggle to get right, though part 1 was way easier.
This commit is contained in:
2024-10-30 15:16:23 +00:00
parent 07a5f754da
commit d357964e2f

67
2022/puzzle-10.lisp Normal file
View File

@@ -0,0 +1,67 @@
(load "lib")
(defparameter input (uiop:read-file-string "10-input"))
(defparameter lines (get-lines input))
(defun parse-line (line)
(destructuring-bind (op . operand)
(--> (string-to-clist line)
(split-by-completely it #\Space)
(mapcar #'clist-to-string it))
(if (string= op "noop")
'(noop . nil)
`(add . ,(parse-integer (car operand))))))
(defparameter important-cycles (list 20 60 100 140 180 220))
(defun check-sum (sum cycles x)
(if (member cycles important-cycles)
(+ sum (* cycles x))
sum))
(let ((sum 0))
(loop with x = 1
for cycles from 1
for line in lines
for (op . operand) = (parse-line line)
if (eq op 'noop)
do (setq sum (check-sum sum cycles x))
else
do (setq sum (check-sum sum cycles x)
cycles (+ cycles 1)
sum (check-sum sum cycles x)
x (+ x operand)))
(format t "Round 1: ~a~%" sum))
(let ((lit-pixels nil))
(defun update-pixels (x cycles)
(let ((cycles (mod (- cycles 1) 40)))
(push
(if (or (= cycles (+ x 1))
(= cycles x)
(= cycles (- x 1)))
"#"
".")
lit-pixels)))
(loop with x = 1
for cycles from 1
for line in lines
for (op . operand) = (parse-line line)
if (eq op 'noop)
do (update-pixels x cycles)
else
do (update-pixels x cycles)
and do (setq cycles (+ cycles 1))
and do (update-pixels x cycles)
and do (setq x (+ x operand)))
(setq lit-pixels (reverse lit-pixels))
(format t "Round 2: ")
(loop for pixel in lit-pixels
for cycle from 0
if (= (mod cycle 40) 0)
do (format t "~%")
do
(format t "~a" pixel)))