Ready for part 2

main
Jacopo De Simoi 11 months ago
parent 24e8453b8c
commit 6f568723ea
  1. 2
      p17/input-test
  2. 31
      p17/p17.org

@ -2,4 +2,4 @@ Register A: 729
Register B: 0
Register C: 0
Program: 0,1,5,4,3,0x
Program: 0,1,5,4,3,0

@ -1,7 +1,7 @@
#+title: solution to p17
Load program
#+begin_src emacs-lisp
#+begin_src emacs-lisp :results none
(require 'dash)
(defun replace-regexp-from-top (a b)
@ -9,7 +9,7 @@ Load program
(replace-regexp a b))
(with-temp-buffer
(insert-file-contents "input")
(insert-file-contents "input-test")
(replace-regexp-from-top "Register \\(.\\): \\(.*\\)$" "(setq \\1 \\2)")
(replace-regexp-from-top "\\(.\\),\\(.\\)" "(\\1 \\2)")
@ -19,8 +19,6 @@ Load program
(eval-buffer))
#+end_src
#+RESULTS:
Convert the opcodes into instructions
#+begin_src emacs-lisp
(let ((opcode-alist '( (0 . adv)
@ -35,13 +33,8 @@ Convert the opcodes into instructions
#+end_src
#+RESULTS:
| bst | 4 |
| bxl | 5 |
| cdv | 5 |
| bxl | 6 |
| adv | 3 |
| bxc | 1 |
| out | 5 |
| adv | 1 |
| out | 4 |
| jnz | 0 |
@ -55,7 +48,7 @@ Now define the instructions and run, for part 1
(cdr (assoc c combo-alist)))))
(defun aux-adv (c)
(/ A (expt 2 (combo c))))
(ash A (* -1 (combo c))))
(defun adv (c)
(setq A (aux-adv c)))
@ -73,13 +66,11 @@ Now define the instructions and run, for part 1
(setq B (mod (combo c) 8)))
(defun out (c)
(let ((out-c (mod (combo c) 8)))
(if so (setq so (concat so (format ",%d" out-c)))
(setq so (format "%d" out-c)))))
(push (mod (combo c) 8) so))
(defun bxc (l)
(setq B (logxor B C)))
(setq B (logxor B C))
)
(defun jnz (l)
(unless (eq A 0)
(setq ip (- (/ l 2) 1))))
@ -93,7 +84,11 @@ Now define the instructions and run, for part 1
(push (list A B C ip (nth ip pr)) stack-trace)
(eval (nth ip pr))
(setq ip (1+ ip)))
so)
(substring (apply #'concat (-map (-partial #'format "%d,") (reverse so))) 0 -1))
(exec proggo)
#+end_src
#+RESULTS:
: 4,6,3,5,6,3,5,2,1,0

Loading…
Cancel
Save