aboutsummaryrefslogtreecommitdiff
path: root/2019/interpreter.lisp
blob: 0ce6057ec469793306f4de7b73b34b46bb48bbd5 (plain) (blame)
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
(defpackage #:intcode
  (:use :cl)
  (:export :run :parse))

(in-package #:intcode)

;;; Interpreter

(defstruct opcode
  instr
  param-count
  param-modes
  last-outputs-p)

(defconstant +instr-add+   1)
(defconstant +instr-mul+   2)
(defconstant +instr-set+   3)
(defconstant +instr-out+   4)
(defconstant +instr-jt+    5)
(defconstant +instr-jn+    6)
(defconstant +instr-le+    7)
(defconstant +instr-eq+    8)
(defconstant +instr-quit+ 99)

(defparameter *handlers* (make-array 100))

;; Interpreter State
(defparameter *ip* 0)
(defparameter *inputs* nil)
(defparameter *outputs* nil)
(defparameter *ram* nil)

(defun run (ram &optional inputs)
  (setq *ip* 0 *inputs* inputs *outputs* nil *ram* ram)
  (loop
    (let* ((opcode (decode-next-opcode))
           (handler (aref *handlers* (opcode-instr opcode))))
      (when (eq (apply handler (fetch-arguments opcode)) 'quit)
        (return-from run (nreverse *outputs*))))))

(defun decode-next-opcode ()
  (let* ((opcode
           (aref *ram* *ip*))
         (instr
           (mod opcode 100))
         (param-count
           (ecase instr
             (#.+instr-add+  3)
             (#.+instr-mul+  3)
             (#.+instr-set+  1)
             (#.+instr-out+  1)
             (#.+instr-jt+   2)
             (#.+instr-jn+   2)
             (#.+instr-le+   3)
             (#.+instr-eq+   3)
             (#.+instr-quit+ 0)))
         (param-modes
           (make-array 3 :initial-contents
                       (list (mod (floor opcode   100) 10)
                             (mod (floor opcode  1000) 10)
                             (mod (floor opcode 10000) 10))))
         (last-outputs-p
           (member instr '(#.+instr-add+ #.+instr-mul+ #.+instr-set+
                           #.+instr-le+ #.+instr-eq+))))
    (make-opcode :instr instr
                 :param-count param-count
                 :param-modes param-modes
                 :last-outputs-p last-outputs-p)))

(defun fetch-arguments (opcode)
  (when (opcode-last-outputs-p opcode)
    (setf (aref (opcode-param-modes opcode)
                (1- (opcode-param-count opcode)))
          1))
  (loop for i from 1 to (opcode-param-count opcode)
        collect (ecase (aref (opcode-param-modes opcode) (1- i))
                  (0 (aref *ram* (aref *ram* (+ *ip* i))))
                  (1             (aref *ram* (+ *ip* i))))))

;;; Instructions

(defmacro definstruction (name (&rest params) &body forms)
  (let ((instr (intern (format nil "+INSTR-~:@(~a~)+" name))))
    `(setf (aref *handlers* ,instr)
           (lambda ,params
             (case (progn ,@forms)
               ('jumpedp nil)
               ('quit 'quit)
               (otherwise (incf *ip* ,(1+ (length params)))))))))

(definstruction add (x y dst)
  (setf (aref *ram* dst) (+ x y)))

(definstruction mul (x y dst)
  (setf (aref *ram* dst) (* x y)))

(definstruction set (dst)
  (setf (aref *ram* dst) (car *inputs*))
  (setq *inputs* (cdr *inputs*)))

(definstruction out (x)
  (push x *outputs*))

(definstruction jt (x addr)
  (unless (zerop x)
    (setq *ip* addr)
    'jumpedp))

(definstruction jn (x addr)
  (when (zerop x)
    (setq *ip* addr)
    'jumpedp))

(definstruction le (x y dst)
  (setf (aref *ram* dst) (bool->int (< x y))))

(definstruction eq (x y dst)
  (setf (aref *ram* dst) (bool->int (= x y))))

(definstruction quit ()
  'quit)

;;; Input Parsing

(defun parse (filename)
  (with-open-file (stream filename)
    (let ((contents (make-string (file-length stream))))
      (read-sequence contents stream)
      (extract-numbers-from-string contents))))

(defun extract-numbers-from-string (string)
  (loop for comma-pos = (position-if #'commap string)
        collect (parse-integer (subseq string 0 comma-pos)) into numbers
        unless comma-pos
          return (coerce numbers 'vector)
        do (setq string (subseq string (1+ comma-pos)))))

;;; Helper Functions

(defun commap (char)
  (char= char #\,))

(defun bool->int (bool)
  (if bool 1 0))

(defun try-aref (array &rest subscripts)
  (loop for i in subscripts
        for j in (array-dimensions array)
        unless (< -1 i j)
          return nil
        finally (return (apply #'aref array subscripts))))