aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--AoC2022/10/solver.el67
1 files changed, 49 insertions, 18 deletions
diff --git a/AoC2022/10/solver.el b/AoC2022/10/solver.el
index 5a8b6f8..d87ca0b 100644
--- a/AoC2022/10/solver.el
+++ b/AoC2022/10/solver.el
@@ -15,7 +15,6 @@
;;
;;; Code:
-(require 'f)
(require 'subr-x)
(require 'cl-lib)
(require 'ert)
@@ -24,26 +23,58 @@
(when (memq cycle '(20 60 100 140 180 220))
(* cycle register)))
-(let ((register 1)
- (cycle 1)
- measures)
- (dolist (instruction (split-string (f-read "input") "\n"))
- (cl-incf cycle)
- (let ((step1 (solver-probe cycle register)) ;; the noop default
- (step2 ;; addx takes 2 cycles
- (when (string-prefix-p "addx" instruction)
- (cl-incf cycle)
- (cl-incf register (string-to-number (cadr (split-string instruction))))
- (solver-probe cycle register))))
- (when-let ((probe (or step1 step2)))
- (push probe measures))))
- (list (reverse measures)
- (apply #'+ measures)
- cycle))
-
+(defsubst solver-draw-pixel (pixel register)
+ (when (= 0 pixel)
+ (insert "\n"))
+ (insert
+ (if (<= (1- register) pixel (1+ register))
+ "#" ".")))
+(defun solver-instructions (filename)
+ (with-temp-buffer
+ (insert-file-contents filename)
+ (split-string (buffer-string) "\n")))
+(defun solver (data-file)
+ (let ((register 1) (cycle 1) measures)
+ (with-temp-buffer
+ (dolist (instruction (solver-instructions data-file))
+ (solver-draw-pixel (mod (1- cycle) 40) register)
+ (cl-incf cycle)
+ (let ((step1 (solver-probe cycle register)) ;; the noop default
+ (step2 ;; addx takes 2 cycles
+ (when (string-prefix-p "addx" instruction)
+ (solver-draw-pixel (mod (1- cycle) 40) register)
+ (cl-incf cycle)
+ (cl-incf register (string-to-number (cadr (split-string instruction))))
+ (solver-probe cycle register))))
+ (when-let ((probe (or step1 step2)))
+ (push probe measures))))
+ (list
+ (apply #'+ measures)
+ (buffer-string)))))
+(ert-deftest test-solver ()
+ (should
+ (equal (solver "eg-in")
+ (list 13140 "
+##..##..##..##..##..##..##..##..##..##..
+###...###...###...###...###...###...###.
+####....####....####....####....####....
+#####.....#####.....#####.....#####.....
+######......######......######......####
+#######.......#######.......#######.....
+.")))
+ (should
+ (equal (solver "input")
+ (list 12540 "
+####.####..##..####.####.#....#..#.####.
+#....#....#..#....#.#....#....#..#.#....
+###..###..#......#..###..#....####.###..
+#....#....#.....#...#....#....#..#.#....
+#....#....#..#.#....#....#....#..#.#....
+#....####..##..####.####.####.#..#.####.
+."))))
(provide 'solver)
;;; solver.el ends here