From 95aa169c8aa57aea0e004486fc7ecba6552e51b1 Mon Sep 17 00:00:00 2001
From: Oscar Najera <hi@oscarnajera.com>
Date: Sat, 10 Dec 2022 20:08:24 +0100
Subject: [AoC2022] LISP day 10

---
 AoC2022/10/solver.lisp | 51 ++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 51 insertions(+)
 create mode 100644 AoC2022/10/solver.lisp

(limited to 'AoC2022/10')

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)
-- 
cgit v1.2.3