aoc18/day19.lisp -rw-r--r-- 927 bytes View raw
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
(in-package :aoc18)

(defun read-day19-input (path)
  (mapcar #'(lambda (l) (read (make-string-input-stream (format nil "(~a)" (cl-ppcre:regex-replace "#" l ""))))) (read-lines path)))

(defun solve-day19-part1 (input)
  (let ((state (copy-seq #(0 0 0 0 0 0)))
	(ip-reg (cadar input))
	(ip 0)
	(instr (coerce (cdr input) 'vector)))
    (loop while (and (>= ip 0) (< ip (length instr))) do
	 (setf (aref state ip-reg) ip)
	 (let* ((ins (aref instr ip))
		(op (car ins))
		(regs (cdr ins)))
	   (apply op state regs))
	 (setf ip (1+ (aref state ip-reg))))
    (aref state 0)))

(test day19-part1
  (is (= 2304 (solve-day19-part1 (read-day19-input "day19.input")))))

(defun solve-day19-part2 (val)
  (declare (optimize (speed 3) (safety 1))
	   (type fixnum val))
  (+ val (loop for x fixnum from 1 to (/ val 2)
	    when (= 0 (mod val x)) summing x fixnum)))

(test day19-part2
  (is (= 28137600 (solve-day19-part2 10551330))))