aboutsummaryrefslogtreecommitdiffstats
path: root/AoC2022/10/solver.lisp
diff options
context:
space:
mode:
authorOscar Najera <hi@oscarnajera.com>2022-12-10 20:08:24 +0100
committerOscar Najera <hi@oscarnajera.com>2022-12-10 20:08:24 +0100
commit95aa169c8aa57aea0e004486fc7ecba6552e51b1 (patch)
tree9dd6a03afe97b36c24e50a03284b330c596d9e4b /AoC2022/10/solver.lisp
parent3a98b4ad3e29ce66eb7c423865c44e1fecf1c9ff (diff)
downloadscratch-95aa169c8aa57aea0e004486fc7ecba6552e51b1.tar.gz
scratch-95aa169c8aa57aea0e004486fc7ecba6552e51b1.tar.bz2
scratch-95aa169c8aa57aea0e004486fc7ecba6552e51b1.zip
[AoC2022] LISP day 10
Diffstat (limited to 'AoC2022/10/solver.lisp')
-rw-r--r--AoC2022/10/solver.lisp51
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)