~theothornhill/advent

0fec3e9e72da49c7d965b23d04bff5676e6e42aa — Theodor Thornhill 4 years ago a23dbb4
Day 7 part one
2 files changed, 35 insertions(+), 0 deletions(-)

A day7-input.txt
A day7.lisp
A day7-input.txt => day7-input.txt +1 -0
@@ 0,0 1,1 @@
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

A day7.lisp => day7.lisp +34 -0
@@ 0,0 1,34 @@

(in-package :intcode)

(defun run (&rest args)
  (init-intcode "./day7-input.txt")
  (let ((result 0))
    (dolist (arg args result)
      (with-intcode (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)))))))))

(defun all-permutations (list)
  (cond ((null list) nil)
        ((null (cdr list)) (list list))
        (t (loop for element in list
             append (mapcar (lambda (l) (cons element l))
                            (all-permutations (remove element list)))))))

(defun day7-part-one ()
  (let (result)
    (mapcar (lambda (x) (push (apply #'run x) result))
            (all-permutations '(0 1 2 3 4)))
    (car (sort result #'>))))