You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
1.9 KiB
1.9 KiB
solution to p17
Load program
(require 'dash)
(defun replace-regexp-from-top (a b)
(goto-char (point-min))
(replace-regexp a b))
(with-temp-buffer
(insert-file-contents "input")
(replace-regexp-from-top "Register \\(.\\): \\(.*\\)$" "(setq \\1 \\2)")
(replace-regexp-from-top "\\(.\\),\\(.\\)" "(\\1 \\2)")
(replace-regexp-from-top "," " ")
(replace-regexp-from-top "Program: \\(.*\\)$" "(setq program '(\\1))")
(eval-buffer))
Convert the opcodes into instructions
(let ((opcode-alist '( (0 . adv)
(1 . bxl)
(2 . bst)
(3 . jnz)
(4 . bxc)
(5 . out)
(6 . bdv)
(7 . cdv))))
(setq proggo (--map (cons (cdr (assoc (car it) opcode-alist)) (cdr it)) program)))
| bst | 4 |
| bxl | 5 |
| cdv | 5 |
| bxl | 6 |
| adv | 3 |
| bxc | 1 |
| out | 5 |
| jnz | 0 |
Now define the instructions and run, for part 1
(defun combo (c)
(let ((combo-alist `( (4 . ,A)
(5 . ,B)
(6 . ,C))))
(if (< c 4) c
(cdr (assoc c combo-alist)))))
(defun aux-adv (c)
(/ A (expt 2 (combo c))))
(defun adv (c)
(setq A (aux-adv c)))
(defun bdv (c)
(setq B (aux-adv c)))
(defun cdv (c)
(setq C (aux-adv c)))
(defun bxl (l)
(setq B (logxor l B)))
(defun bst (c)
(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)))))
(defun bxc (l)
(setq B (logxor B C)))
(defun jnz (l)
(unless (eq A 0)
(setq ip (- (/ l 2) 1))))
(defun exec (pr)
(setq so nil
ip 0
stack-trace nil)
(while (nth ip pr)
(push (list A B C ip (nth ip pr)) stack-trace)
(eval (nth ip pr))
(setq ip (1+ ip)))
so)
(exec proggo)