diff options
author | Aryadev Chavali <aryadev@aryadevchavali.com> | 2024-10-30 15:16:23 +0000 |
---|---|---|
committer | Aryadev Chavali <aryadev@aryadevchavali.com> | 2024-10-30 22:33:27 +0000 |
commit | d357964e2fed2096badd684ac08f6a8a566e143c (patch) | |
tree | e048b6bde5380dcbcd7537c75962bf3c4458a2f4 | |
parent | 07a5f754da457631dc22e612a01e5d2cb26137d3 (diff) | |
download | advent-of-code-d357964e2fed2096badd684ac08f6a8a566e143c.tar.gz advent-of-code-d357964e2fed2096badd684ac08f6a8a566e143c.tar.bz2 advent-of-code-d357964e2fed2096badd684ac08f6a8a566e143c.zip |
FINALLY figured out puzzle 10 2022
Part 2 was a real struggle to get right, though part 1 was way easier.
-rw-r--r-- | 2022/puzzle-10.lisp | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/2022/puzzle-10.lisp b/2022/puzzle-10.lisp new file mode 100644 index 0000000..9bb1364 --- /dev/null +++ b/2022/puzzle-10.lisp @@ -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))) |