Repository navigation
Expand file tree
/
Copy pathcomputer.py
More file actions
126 lines (107 loc) · 3.34 KB
/
Copy pathcomputer.py
File metadata and controls
126 lines (107 loc) · 3.34 KB
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
class Computer:
def __init__(self, num_registers, start_ip, instructions):
self.registers = [0] * num_registers
self.ip = start_ip
self.process_instructions(instructions)
def process_instructions(self, instructions):
self.instructions=[]
for instruction in instructions:
instruction = instruction.split()
for i in range(1, 4):
instruction[i] = int(instruction[i])
self.instructions.append(instruction)
def run(self):
while self.registers[0] < len(self.instructions):
self.do_instruction(self.registers[0])
self.registers[0] += 1
def do_instruction(self, ip):
command = self.instructions[ip]
cmd = getattr(self, command[0])
res = cmd(command, self.registers)
# print('ip={ip} {reg} {cmd} {after}'.format(
# ip=ip,
# reg=self.registers,
# cmd=command,
# after=res
# ))
self.registers = res
@staticmethod
def addr(command, reg):
res = list(reg)
res[command[3]] = res[command[1]] + res[command[2]]
return res
@staticmethod
def addi(command, reg):
res = list(reg)
res[command[3]] = res[command[1]] + command[2]
return res
@staticmethod
def mulr(command, reg):
res = list(reg)
res[command[3]] = res[command[1]] * res[command[2]]
return res
@staticmethod
def muli(command, reg):
res = list(reg)
res[command[3]] = res[command[1]] * command[2]
return res
@staticmethod
def banr(command, reg):
res = list(reg)
res[command[3]] = res[command[1]] & res[command[2]]
return res
@staticmethod
def bani(command, reg):
res = list(reg)
res[command[3]] = res[command[1]] & command[2]
return res
@staticmethod
def borr(command, reg):
res = list(reg)
res[command[3]] = res[command[1]] | res[command[2]]
return res
@staticmethod
def bori(command, reg):
res = list(reg)
res[command[3]] = res[command[1]] | command[2]
return res
@staticmethod
def setr(command, reg):
res = list(reg)
res[command[3]] = res[command[1]]
return res
@staticmethod
def seti(command, reg):
res = list(reg)
res[command[3]] = command[1]
return res
@staticmethod
def gtir(command, reg):
res = list(reg)
res[command[3]] = 1 if command[1] > res[command[2]] else 0
return res
@staticmethod
def gtri(command, reg):
res = list(reg)
res[command[3]] = 1 if res[command[1]] > command[2] else 0
return res
@staticmethod
def gtrr(command, reg):
res = list(reg)
res[command[3]] = 1 if res[command[1]] > res[command[2]] else 0
return res
@staticmethod
def eqir(command, reg):
res = list(reg)
res[command[3]] = 1 if command[1] == res[command[2]] else 0
return res
@staticmethod
def eqri(command, reg):
res = list(reg)
res[command[3]] = 1 if res[command[1]] == command[2] else 0
return res
@staticmethod
def eqrr(command, reg):
res = list(reg)
res[command[3]] = 1 if res[command[1]] == res[command[2]] else 0
return res