-
Notifications
You must be signed in to change notification settings - Fork 0
/
computer.py
174 lines (165 loc) · 6.37 KB
/
computer.py
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
from dataclasses import dataclass, field
from typing import List
class Computer:
def __init__(self, instruction_set, inputs=[], position = 0, input_position = 0):
self.instruction_set = instruction_set
self.position = position
self.inputs = inputs
self.input_position = input_position
self.output = []
self.stopped = False
def process_all(self):
while not self.stopped:
self.process_one()
def process_one(self):
instruction = self.get_instruction()
addr_values = self.evaluate_arg(instruction.args)
print("Running:", instruction.key_word)
if instruction.key_word == "add":
addr_a = addr_values[0]
addr_b = addr_values[1]
self.instruction_set[instruction.output] = addr_a + addr_b
self.position += 4
elif instruction.key_word == "multiply":
addr_a = addr_values[0]
addr_b = addr_values[1]
self.instruction_set[instruction.output] = addr_a * addr_b
self.position += 4
elif instruction.key_word == "input":
while True:
try:
next_input = self.inputs[self.input_position]
print("requested input got:", next_input)
break
except ValueError:
print("Must be a number")
self.instruction_set[instruction.output] = next_input
self.input_position += 1
self.position += 2
elif instruction.key_word == "output":
addr_a = addr_values[0]
self.output.append(addr_a)
self.position += 2
elif instruction.key_word == "jump_true":
addr_a = addr_values[0]
addr_b = addr_values[1]
if addr_a != 0:
self.position = addr_b
else:
self.position += 3
elif instruction.key_word == "jump_false":
addr_a = addr_values[0]
addr_b = addr_values[1]
if addr_a == 0:
self.position = addr_b
else:
self.position += 3
elif instruction.key_word == "less":
addr_a = addr_values[0]
addr_b = addr_values[1]
if addr_a < addr_b:
self.instruction_set[instruction.output] = 1
else:
self.instruction_set[instruction.output] = 0
self.position += 4
elif instruction.key_word == "equal":
addr_a = addr_values[0]
addr_b = addr_values[1]
if addr_a == addr_b:
self.instruction_set[instruction.output] = 1
else:
self.instruction_set[instruction.output] = 0
self.position += 4
elif instruction.key_word == "STOP":
self.stopped = True
if not self.output:
self.output.append(self.instruction_set[0])
else:
raise Exception(f"Instruction not implemented {instruction.key_word}")
def evaluate_arg(self, args):
addrs = []
for arg in args:
if arg.mode == "value":
addrs.append(arg.value)
elif arg.mode == "address":
addrs.append(self.instruction_set[arg.value])
else:
raise Exception(f"Mode not implemented {arg.mode}")
return addrs
def get_instruction(self):
full_instruction = self.instruction_set[self.position]
code = full_instruction % 100
mode_names = ["address", "value"]
modes = [
full_instruction // 100 % 10,
full_instruction // 1000 % 10
]
if code == 1:
key_word = "add"
args = [
Arg(mode_names[modes[0]], self.instruction_set[self.position+1]),
Arg(mode_names[modes[1]], self.instruction_set[self.position+2])
]
output = self.instruction_set[self.position+3]
return Instruction(key_word, args, output)
elif code == 2:
key_word = "multiply"
args = [
Arg(mode_names[modes[0]], self.instruction_set[self.position+1]),
Arg(mode_names[modes[1]], self.instruction_set[self.position+2])
]
output = self.instruction_set[self.position+3]
return Instruction(key_word, args, output)
elif code == 3:
return Instruction(
key_word = "input",
output = self.instruction_set[self.position+1]
)
elif code == 4:
return Instruction(
key_word = "output",
args = [Arg(mode_names[modes[0]], self.instruction_set[self.position+1])]
)
elif code == 5:
key_word = "jump_true"
args = [
Arg(mode_names[modes[0]], self.instruction_set[self.position+1]),
Arg(mode_names[modes[1]], self.instruction_set[self.position+2])
]
return Instruction(key_word, args)
elif code == 6:
key_word = "jump_false"
args = [
Arg(mode_names[modes[0]], self.instruction_set[self.position+1]),
Arg(mode_names[modes[1]], self.instruction_set[self.position+2])
]
return Instruction(key_word, args)
elif code == 7:
key_word = "less"
args = [
Arg(mode_names[modes[0]], self.instruction_set[self.position+1]),
Arg(mode_names[modes[1]], self.instruction_set[self.position+2])
]
output = self.instruction_set[self.position+3]
return Instruction(key_word, args, output)
elif code == 8:
key_word = "equal"
args = [
Arg(mode_names[modes[0]], self.instruction_set[self.position+1]),
Arg(mode_names[modes[1]], self.instruction_set[self.position+2])
]
output = self.instruction_set[self.position+3]
return Instruction(key_word, args, output)
elif code == 99:
return Instruction(key_word = "STOP")
else:
raise Exception(f"Instruction not implemented {code}")
@dataclass
class Arg:
mode: str
value: int
@dataclass
class Instruction:
key_word: str
args: List[Arg] = field(default_factory=list)
output: int = None