-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday5.el
98 lines (86 loc) · 5.98 KB
/
day5.el
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
(defun program-to-list (program-string)
(mapcar 'string-to-number (split-string program-string ",")))
(defvar program-input "1,0,0,3,1,1,2,3,1,3,4,3,1,5,0,3,2,1,10,19,1,6,19,23,1,10,23,27,2,27,13,31,1,31,6,35,2,6,35,39,1,39,5,43,1,6,43,47,2,6,47,51,1,51,5,55,2,55,9,59,1,6,59,63,1,9,63,67,1,67,10,71,2,9,71,75,1,6,75,79,1,5,79,83,2,83,10,87,1,87,5,91,1,91,9,95,1,6,95,99,2,99,10,103,1,103,5,107,2,107,6,111,1,111,5,115,1,9,115,119,2,119,10,123,1,6,123,127,2,13,127,131,1,131,6,135,1,135,10,139,1,13,139,143,1,143,13,147,1,5,147,151,1,151,2,155,1,155,5,0,99,2,0,14,0")
(defconst program2 "3,225,1,225,6,6,1100,1,238,225,104,0,1001,152,55,224,1001,224,-68,224,4,224,1002,223,8,223,1001,224,4,224,1,224,223,223,1101,62,41,225,1101,83,71,225,102,59,147,224,101,-944,224,224,4,224,1002,223,8,223,101,3,224,224,1,224,223,223,2,40,139,224,1001,224,-3905,224,4,224,1002,223,8,223,101,7,224,224,1,223,224,223,1101,6,94,224,101,-100,224,224,4,224,1002,223,8,223,101,6,224,224,1,224,223,223,1102,75,30,225,1102,70,44,224,101,-3080,224,224,4,224,1002,223,8,223,1001,224,4,224,1,223,224,223,1101,55,20,225,1102,55,16,225,1102,13,94,225,1102,16,55,225,1102,13,13,225,1,109,143,224,101,-88,224,224,4,224,1002,223,8,223,1001,224,2,224,1,223,224,223,1002,136,57,224,101,-1140,224,224,4,224,1002,223,8,223,101,6,224,224,1,223,224,223,101,76,35,224,1001,224,-138,224,4,224,1002,223,8,223,101,5,224,224,1,223,224,223,4,223,99,0,0,0,677,0,0,0,0,0,0,0,0,0,0,0,1105,0,99999,1105,227,247,1105,1,99999,1005,227,99999,1005,0,256,1105,1,99999,1106,227,99999,1106,0,265,1105,1,99999,1006,0,99999,1006,227,274,1105,1,99999,1105,1,280,1105,1,99999,1,225,225,225,1101,294,0,0,105,1,0,1105,1,99999,1106,0,300,1105,1,99999,1,225,225,225,1101,314,0,0,106,0,0,1105,1,99999,1008,677,677,224,1002,223,2,223,1006,224,329,1001,223,1,223,8,677,226,224,102,2,223,223,1006,224,344,101,1,223,223,1107,226,226,224,1002,223,2,223,1006,224,359,1001,223,1,223,1108,677,226,224,102,2,223,223,1005,224,374,1001,223,1,223,1007,226,226,224,102,2,223,223,1006,224,389,1001,223,1,223,108,677,677,224,1002,223,2,223,1005,224,404,1001,223,1,223,1007,677,677,224,102,2,223,223,1005,224,419,1001,223,1,223,8,226,677,224,102,2,223,223,1005,224,434,101,1,223,223,1008,677,226,224,102,2,223,223,1006,224,449,1001,223,1,223,7,677,677,224,102,2,223,223,1006,224,464,1001,223,1,223,8,226,226,224,1002,223,2,223,1005,224,479,1001,223,1,223,7,226,677,224,102,2,223,223,1006,224,494,1001,223,1,223,7,677,226,224,1002,223,2,223,1005,224,509,101,1,223,223,107,677,677,224,102,2,223,223,1006,224,524,101,1,223,223,1007,677,226,224,102,2,223,223,1006,224,539,101,1,223,223,107,226,226,224,1002,223,2,223,1006,224,554,101,1,223,223,1008,226,226,224,102,2,223,223,1006,224,569,1001,223,1,223,1107,677,226,224,1002,223,2,223,1005,224,584,101,1,223,223,1107,226,677,224,102,2,223,223,1005,224,599,101,1,223,223,1108,226,677,224,102,2,223,223,1005,224,614,101,1,223,223,108,677,226,224,102,2,223,223,1005,224,629,101,1,223,223,107,226,677,224,102,2,223,223,1006,224,644,1001,223,1,223,1108,226,226,224,1002,223,2,223,1006,224,659,101,1,223,223,108,226,226,224,102,2,223,223,1005,224,674,101,1,223,223,4,223,99,226")
(defun decode-inst (inst-raw)
(let ((inst (mod inst-raw 100))
(op1 (mod (/ inst-raw 100) 10))
(op2 (mod (/ inst-raw 1000) 10))
(op3 (mod (/ inst-raw 10000) 10))
)
(list inst op1 op2 op3)))
(defun get-operand (val mode program)
(if (and (numberp val) (= mode 0))
(nth val program)
val))
(defun prompt-user ()
(interactive)
(read-number "Enter code: "))
(defun execute-program (program)
(defun op (program pc f arg1 arg2)
(let ((out-pos (nth (+ pc 3) program)))
(setf (nth out-pos program)
(apply f (list arg1 arg2)))
program))
(defun op-test (program pc f arg1 arg2)
(let ((out-pos (nth (+ pc 3) program)))
(setf (nth out-pos program)
(if (apply f (list arg1 arg2))
1
0))
program))
(defun op-input (program pc)
(let ((out-pos (nth (+ pc 1) program))
(val (prompt-user)))
(setf (nth out-pos program) val)
program))
(defun op-print (program pc arg1)
(print arg1)
program)
(defun ex (program pc)
(let* ((inst-parsed (decode-inst (nth pc program)))
(inst (car inst-parsed))
(arg1 (get-operand (nth (+ pc 1) program) (cadr inst-parsed) program))
(arg2 (get-operand (nth (+ pc 2) program) (caddr inst-parsed) program))
)
(cond ((= inst 1)
(ex (op program pc '+ arg1 arg2) (+ 4 pc)))
((= inst 2)
(ex (op program pc '* arg1 arg2) (+ 4 pc)))
((= inst 3)
(ex (op-input program pc) (+ 2 pc)))
((= inst 4)
(ex (op-print program pc arg1) (+ 2 pc)))
((= inst 5)
(ex program
(if (not (= 0 arg1))
arg2
(+ 3 pc))))
((= inst 6)
(ex program
(if (= 0 arg1)
arg2
(+ 3 pc))))
((= inst 7)
(ex (op-test program pc '< arg1 arg2) (+ 4 pc)))
((= inst 8)
(ex (op-test program pc '= arg1 arg2) (+ 4 pc)))
((= inst 99)
program)
(t (concat "Unknown instruction: " (number-to-string inst) " at pc " (number-to-string pc))))))
(ex (program-to-list program) 0))
(execute-program program2)
;; 1409363
;; => (314 225 1 225 6 6 1105 1 238 225 104 0 ...)
;; 9431221
(3 225 1 225 6 6 1101 1 238 225 104 0 ...)
(execute-program "104,1234,99")
(execute-program "1002,4,3,4,33,99")
(execute-program "1101,100,-1,4,0,99")
(execute-program "3,0,4,0,99")
;; part2
(execute-program "3,9,8,9,10,9,4,9,99,-1,8")
(execute-program "3,9,7,9,10,9,4,9,99,-1,8")
(execute-program "3,3,1108,-1,8,3,4,3,99")
(execute-program "3,12,6,12,15,1,13,14,13,4,13,99,-1,0,1,9")
(execute-program "3,21,1008,21,8,20,1005,20,22,107,8,21,20,1006,20,31,1106,0,36,98,0,0,1002,21,125,20,4,20,1105,1,46,104,999,1105,1,46,1101,1000,1,20,4,20,1105,1,46,98,99")