First part running with chained computers
3 files changed, 52 insertions(+), 26 deletions(-) M day7-input.txt M day7.lisp M intcode.lisp
M day7-input.txt => day7-input.txt +1 -0
@@ 1,1 1,2 @@ 3,8,1001,8,10,8,105,1,0,0,21,46,67,76,101,118,199,280,361,442,99999,3,9,1002,9,4,9,1001,9,2,9,102,3,9,9,101,3,9,9,102,2,9,9,4,9,99,3,9,1001,9,3,9,102,2,9,9,1001,9,2,9,1002,9,3,9,4,9,99,3,9,101,3,9,9,4,9,99,3,9,1001,9,2,9,1002,9,5,9,101,5,9,9,1002,9,4,9,101,5,9,9,4,9,99,3,9,102,2,9,9,1001,9,5,9,102,2,9,9,4,9,99,3,9,1002,9,2,9,4,9,3,9,1002,9,2,9,4,9,3,9,101,2,9,9,4,9,3,9,101,1,9,9,4,9,3,9,102,2,9,9,4,9,3,9,102,2,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,101,1,9,9,4,9,3,9,102,2,9,9,4,9,3,9,101,2,9,9,4,9,99,3,9,101,1,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,102,2,9,9,4,9,3,9,101,1,9,9,4,9,3,9,101,2,9,9,4,9,3,9,102,2,9,9,4,9,3,9,101,2,9,9,4,9,3,9,102,2,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,101,2,9,9,4,9,99,3,9,1001,9,1,9,4,9,3,9,1002,9,2,9,4,9,3,9,1002,9,2,9,4,9,3,9,101,1,9,9,4,9,3,9,102,2,9,9,4,9,3,9,1001,9,1,9,4,9,3,9,1002,9,2,9,4,9,3,9,1001,9,1,9,4,9,3,9,101,1,9,9,4,9,3,9,101,2,9,9,4,9,99,3,9,1002,9,2,9,4,9,3,9,1001,9,1,9,4,9,3,9,101,2,9,9,4,9,3,9,101,2,9,9,4,9,3,9,102,2,9,9,4,9,3,9,102,2,9,9,4,9,3,9,102,2,9,9,4,9,3,9,102,2,9,9,4,9,3,9,101,1,9,9,4,9,3,9,1001,9,2,9,4,9,99,3,9,102,2,9,9,4,9,3,9,102,2,9,9,4,9,3,9,101,2,9,9,4,9,3,9,101,1,9,9,4,9,3,9,101,2,9,9,4,9,3,9,1001,9,2,9,4,9,3,9,1001,9,2,9,4,9,3,9,101,2,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,101,2,9,9,4,9,99
M day7.lisp => day7.lisp +41 -21
@@ 1,24 1,23 @@ (in-package :intcode) (defparameter *i* (make-instance 'intcode)) (defun run-day7-part-one (intcode &rest args) (init-intcode "./day7-input.txt" intcode) (let ((result 0)) (dolist (arg args result) (with-intcode intcode (with-instruction-pointer (setf instruction-pointer 0) (let ((action (next-instruction))) (funcall action arg) ;; do first read instruction (do ((action (next-instruction) (next-instruction))) ((eql action 'halt) (setf result (get-mem 0 (1- instruction-pointer)))) (cond ((eql action 'input) (funcall action result)) ((eql action 'output) (funcall action nil)) (t (funcall action)))))))))) (defparameter *a* (make-instance 'intcode)) (defparameter *b* (make-instance 'intcode)) (defparameter *c* (make-instance 'intcode)) (defparameter *d* (make-instance 'intcode)) (defparameter *e* (make-instance 'intcode)) (defun run-day7-part-one (a b c d e &rest args) (let ((computers (list a b c d e))) (map nil (lambda (computer arg) (init-intcode "./day7-input.txt" computer arg)) computers args) (run-with-input a a 0) (run-with-input b a) (run-with-input c b) (run-with-input d c) (run-with-input e d))) (defun all-permutations (list) @@ (cond ((null list) nil) 27,10 26,31 @@ append (mapcar (lambda (l) (cons element l)) (all-permutations (remove element list))))))) (defun run-with-input (a b &optional (hardcode nil)) (let ((result 0)) (with-intcode a (with-instruction-pointer (let ((action (next-instruction))) (funcall action (setting a)) ;; do first read instruction (do ((action (next-instruction) (next-instruction))) ((eql action 'halt) (setf result (get-mem 0 (1- instruction-pointer)))) (cond ((eql action 'input) (cond ((out-buffer b) (input (pop (out-buffer b)))) (hardcode (input hardcode) (setf hardcode nil)) (t (return)))) ((eql action 'output) (output nil)) (t (funcall action))))))))) (defun day7-part-one () (let (result) (mapcar (lambda (x) (push (apply #'run-day7-part-one *i* x) result)) (mapcar (lambda (x) (push (apply #'run-day7-part-one *a* *b* *c* *d* *e* x) result)) (all-permutations '(0 1 2 3 4))) (car (sort result #'>))))
M intcode.lisp => intcode.lisp +10 -5
@@ 143,9 143,11 @@ (with-binary-expr (setf (set-mem z) (binary-result *)))) (defun input (in) (defun input (&optional in) (with-io (setf (set-mem x) (if in in (parse-integer (read-line)))))) ;; (setf (set-mem x) (if in in (parse-integer (read-line)))) (setf (set-mem x) in) )) (defun output (&optional (stream t)) @@ (with-io 181,7 183,7 @@ (with-intcode intcode (let* ((op-codes (read-op-codes filename)) (mem-size (length op-codes))) (with-internals (memory setting out-buffer instruction-pointer) (with-internals (memory halted setting out-buffer instruction-pointer) (setf instruction-pointer 0) (setf setting start) @@ (setf out-buffer nil) 192,7 194,10 @@ "RUN dynamically binds an instance of INTCODE. " (with-intcode intcode (with-internals (memory instruction-pointer) (setf instruction-pointer 0) (do* ((action (next-instruction) (next-instruction))) ((eql action 'halt) (aref memory 0)) (funcall action))))) (cond ((eql action 'input) (funcall action)) ((eql action 'output) (funcall action nil)) (t (funcall action)))))))