diff options
Diffstat (limited to 'AoC2022')
-rw-r--r-- | AoC2022/10/solver.lisp | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/AoC2022/10/solver.lisp b/AoC2022/10/solver.lisp new file mode 100644 index 0000000..a30aa56 --- /dev/null +++ b/AoC2022/10/solver.lisp @@ -0,0 +1,51 @@ +(ql:quickload '(:fiveam)) + +(defun probe (cycle register) + (when (member cycle '(20 60 100 140 180 220) :test #'eq) + (* cycle register))) + +(defun draw-pixel (cycle register) + (let ((pixel (mod (1- cycle) 40))) + (when (= 0 pixel) + (terpri)) + (princ (if (<= (1- register) pixel (1+ register)) + "#" "."))) + (1+ cycle)) + +(defun solver (data-file) + (let ((register 1) (cycle 1) + (*standard-output* (make-string-output-stream))) + (list + (with-open-file (in data-file) + (loop for inst = (read in nil nil) + while inst + when (progn (setf cycle (draw-pixel cycle register)) + (let ((step1 (probe cycle register)) + (step2 + (when (eq 'addx inst) + (setf cycle (draw-pixel cycle register)) + (incf register (read in nil 0)) + (probe cycle register)))) + (or step1 step2))) + sum it)) + (get-output-stream-string *standard-output*)))) + +(fiveam:test results + (fiveam:is (equal (solver "eg-in") + (list 13140 " +##..##..##..##..##..##..##..##..##..##.. +###...###...###...###...###...###...###. +####....####....####....####....####.... +#####.....#####.....#####.....#####..... +######......######......######......#### +#######.......#######.......#######....."))) + (fiveam:is (equal (solver "input") + (list 12540 " +####.####..##..####.####.#....#..#.####. +#....#....#..#....#.#....#....#..#.#.... +###..###..#......#..###..#....####.###.. +#....#....#.....#...#....#....#..#.#.... +#....#....#..#.#....#....#....#..#.#.... +#....####..##..####.####.####.#..#.####.")))) + +(fiveam:run-all-tests) |