diff options
author | Oscar Najera <hi@oscarnajera.com> | 2022-12-10 19:23:14 +0100 |
---|---|---|
committer | Oscar Najera <hi@oscarnajera.com> | 2022-12-10 19:23:14 +0100 |
commit | 3a98b4ad3e29ce66eb7c423865c44e1fecf1c9ff (patch) | |
tree | e9701f8839920a544ae2825791f8734c660c46cb /AoC2022 | |
parent | 5e1f67166cc7607b2b45396a210462ec62b0433e (diff) | |
download | scratch-3a98b4ad3e29ce66eb7c423865c44e1fecf1c9ff.tar.gz scratch-3a98b4ad3e29ce66eb7c423865c44e1fecf1c9ff.tar.bz2 scratch-3a98b4ad3e29ce66eb7c423865c44e1fecf1c9ff.zip |
[AoC2022] ELISP 10 with test
Diffstat (limited to 'AoC2022')
-rw-r--r-- | AoC2022/10/solver.el | 67 |
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 |