|
|
|
@ -311,3 +311,100 @@
|
|
|
|
|
(time (progn (build-graph data)
|
|
|
|
|
(print (solve-7-1 data symbol))
|
|
|
|
|
(print (solve-7-2 symbol))))))
|
|
|
|
|
|
|
|
|
|
;;; day 8
|
|
|
|
|
;;; intcode again
|
|
|
|
|
|
|
|
|
|
(defun read-8 (file)
|
|
|
|
|
(let ((data (with-open-file (stream file)
|
|
|
|
|
(loop for line = (read-line stream nil)
|
|
|
|
|
while line
|
|
|
|
|
collect (let ((seq (split-sequence:split-sequence #\space line)))
|
|
|
|
|
(list (first seq) (parse-integer (second seq))))))))
|
|
|
|
|
(make-array (length data) :initial-contents data)))
|
|
|
|
|
|
|
|
|
|
(defun %solve-8 (data &optional alter)
|
|
|
|
|
(let* ((accumulator 0)
|
|
|
|
|
(position 0)
|
|
|
|
|
(ht (make-hash-table))
|
|
|
|
|
(lambdas (list (cons "nop" (lambda (n) (declare (ignore n)) (incf position)))
|
|
|
|
|
(cons "acc" (lambda (n) (incf position) (incf accumulator n)))
|
|
|
|
|
(cons "jmp" (lambda (n) (incf position n))))))
|
|
|
|
|
(labels ((rec (fiddle-ins)
|
|
|
|
|
(cond ((gethash position ht)
|
|
|
|
|
(unless alter accumulator))
|
|
|
|
|
((>= position (length data))
|
|
|
|
|
(values accumulator "EOF"))
|
|
|
|
|
(t (progn (setf (gethash position ht) (first (aref data position)))
|
|
|
|
|
(funcall (cdr (assoc
|
|
|
|
|
(or (if (eql position fiddle-ins)
|
|
|
|
|
(if (string= (first (aref data position))
|
|
|
|
|
"nop")
|
|
|
|
|
"jmp" "nop")
|
|
|
|
|
nil)
|
|
|
|
|
(first (aref data position)))
|
|
|
|
|
lambdas :test #'equal))
|
|
|
|
|
(second (aref data position)))
|
|
|
|
|
(rec fiddle-ins))))))
|
|
|
|
|
(let ((p1 (rec nil))
|
|
|
|
|
(index-list nil))
|
|
|
|
|
(if alter
|
|
|
|
|
(progn
|
|
|
|
|
(maphash (lambda (k v) (if (or (string= v "nop") (string= v "jmp"))
|
|
|
|
|
(push k index-list)))
|
|
|
|
|
ht)
|
|
|
|
|
(first (remove-if #'null (map 'list (lambda (n)
|
|
|
|
|
(setf accumulator 0
|
|
|
|
|
position 0
|
|
|
|
|
ht (make-hash-table))
|
|
|
|
|
(rec n))
|
|
|
|
|
index-list))))
|
|
|
|
|
p1)))))
|
|
|
|
|
|
|
|
|
|
(defun solve-8 (file)
|
|
|
|
|
(let ((data (read-8 file)))
|
|
|
|
|
(time (progn (print (%solve-8 data))
|
|
|
|
|
(print (%solve-8 data t))))))
|
|
|
|
|
|
|
|
|
|
;;; Day 9
|
|
|
|
|
|
|
|
|
|
(defun read-9 (file)
|
|
|
|
|
(apply #'vector
|
|
|
|
|
(with-open-file (stream file)
|
|
|
|
|
(loop for line = (read-line stream nil)
|
|
|
|
|
while line
|
|
|
|
|
collect (parse-integer line)))))
|
|
|
|
|
|
|
|
|
|
(defun step-xmas (seed-size data &optional (index 0))
|
|
|
|
|
(let* ((target (aref data (+ seed-size index)))
|
|
|
|
|
(window-list (coerce (subseq data index (+ index seed-size)) 'list))
|
|
|
|
|
(deficits (map 'list (lambda (n) (- target n)) window-list))
|
|
|
|
|
(pairs (intersection deficits window-list)))
|
|
|
|
|
;; (format t "~a, ~a, ~a ~%" target window-list deficits)
|
|
|
|
|
(if (< 1 (length pairs))
|
|
|
|
|
pairs target)))
|
|
|
|
|
|
|
|
|
|
(defun walk-xmas (seed-size data)
|
|
|
|
|
(loop for index from 0 to (- (length data) 1 seed-size)
|
|
|
|
|
collecting (step-xmas seed-size data index)))
|
|
|
|
|
|
|
|
|
|
(defun solve-9-1 (seed-size data)
|
|
|
|
|
(first (remove-if #'consp (walk-xmas seed-size data))))
|
|
|
|
|
|
|
|
|
|
(defun caterpillar (data target tail head)
|
|
|
|
|
(let ((range-sum (reduce #'+ (subseq data tail head))))
|
|
|
|
|
(cond ((> target range-sum)
|
|
|
|
|
(caterpillar data target tail (1+ head)))
|
|
|
|
|
((< target range-sum)
|
|
|
|
|
(caterpillar data target (1+ tail) head))
|
|
|
|
|
(t (subseq data tail head)))))
|
|
|
|
|
|
|
|
|
|
(defun solve-9-2 (data target)
|
|
|
|
|
(let ((range (sort (caterpillar data target 0 1) #'>)))
|
|
|
|
|
(+ (aref range 0) (aref range (1- (length range))))))
|
|
|
|
|
|
|
|
|
|
(defun solve-9 (file &optional (step-size 25))
|
|
|
|
|
(time (let* ((data (read-9 file))
|
|
|
|
|
(p1 (solve-9-1 step-size data)))
|
|
|
|
|
(print p1)
|
|
|
|
|
(print (solve-9-2 data p1)))))
|
|
|
|
|