-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathservice.py
233 lines (174 loc) · 7.3 KB
/
service.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
#!/usr/local/bin/python
from copy import deepcopy
from random import randrange
import os
class WeirdMachine:
def __init__(self, arg1: int, arg2: int):
self.MAX_REGISTERS = 10
self.MAX_COMPUTERS = 10
self.MAX_VALUE = 1000
self.MAX_TICKS = 5000
self.MAX_SCRIPT_LENGTH = 100
self.computers = [
{
'REGISTERS': [arg1, arg2] + [0 for _ in range(self.MAX_REGISTERS - 2)],
'INSTRUCTIONS': [],
'IP': 0
}
]
self.running = False
self.result = None
self.ticks = 0
def parse_instruction(self, computer_idx: int) -> bool:
"""
Parse and run the instruction. Returns True if successful, False otherwise.
"""
computer = self.computers[computer_idx]
instruction = computer['INSTRUCTIONS'][computer['IP']]
computer['IP'] += 1
command, arguments = instruction.split(' ')[0], instruction.split(' ')[1:]
try:
arguments = [int(arg) for arg in arguments]
except:
return False
if command == "SET":
if len(arguments) != 2: return False
register, value = arguments[0], arguments[1]
if register >= self.MAX_REGISTERS or abs(value) > self.MAX_VALUE: return False
computer['REGISTERS'][register] = int(value)
return True
elif command == "ADD":
if len(arguments) != 2: return False
dest, src = arguments[0], arguments[1]
if src >= self.MAX_REGISTERS or dest >= self.MAX_REGISTERS: return False
computer['REGISTERS'][dest] += computer['REGISTERS'][src]
return True
elif command == "NEG":
if len(arguments) != 1: return False
register = arguments[0]
if register >= self.MAX_REGISTERS or len(self.computers) >= self.MAX_COMPUTERS: return False
computer_clone = deepcopy(computer)
computer_clone['REGISTERS'][register] = -computer_clone['REGISTERS'][register]
self.computers.append(computer_clone)
return True
elif command == "JIZ":
if len(arguments) != 2: return False
register, ip = arguments[0], arguments[1]
if register >= self.MAX_REGISTERS or ip >= len(computer['INSTRUCTIONS']): return False
if computer['REGISTERS'][register] == 0:
computer['IP'] = int(ip)
return True
elif command == "JNZ":
if len(arguments) != 2: return False
register, ip = arguments[0], arguments[1]
if register >= self.MAX_REGISTERS or ip >= len(computer['INSTRUCTIONS']): return False
if computer['REGISTERS'][register] != 0:
computer['IP'] = int(ip)
return True
elif command == "HALT":
if len(arguments) != 1: return False
register = arguments[0]
if register >= self.MAX_REGISTERS: return False
self.result = computer['REGISTERS'][register]
self.running = False
return True
def run_script(self, lines: list) -> int or None:
"""
Runs the script and returns the result.
"""
self.running = True
self.result = None
self.ticks = 0
for line in lines:
self.computers[0]['INSTRUCTIONS'].append(line)
while self.running:
# Run one instruction for each computer, per tick
for computer_idx in range(len(self.computers)):
if self.computers[computer_idx]['IP'] >= len(self.computers[computer_idx]['INSTRUCTIONS']):
print("Computer {} has no more instructions to run. Assuming implicit HALT, no results will be returned.".format(computer_idx))
self.running = False
break
elif not self.parse_instruction(computer_idx):
print("Invalid instruction or error occured: {}".format(self.computers[computer_idx]['INSTRUCTIONS'][self.computers[computer_idx]['IP'] - 1]))
self.running = False
break
# Return once the first computer halts
if not self.running:
break
self.ticks += 1
if self.ticks > self.MAX_TICKS:
print("Max ticks exceeded. Assuming implicit HALT, no results will be returned.")
self.running = False
return self.result
def get_script(self):
"""
Get the script.
"""
print('===== ENTER YOUR SCRIPT =====')
result = []
while len(result) <= self.MAX_SCRIPT_LENGTH:
line = input()
if line:
result.append(line)
else:
break
return result
def reset(self, arg1: int, arg2: int):
"""
Reset the machine with arg1 and arg2 as the inputs.
"""
self.computers = [
{
'REGISTERS': [arg1, arg2] + [0 for _ in range(self.MAX_REGISTERS - 2)],
'INSTRUCTIONS': [],
'IP': 0
}
]
self.running = False
self.result = None
self.ticks = 0
def test(self, range1: tuple, range2: tuple, script: list) -> bool:
"""
Runs test cases, based on randomly generated values from range1 and range2.
Returns True if the test case passes, False otherwise.
"""
print(f"Range: {range1} {range2}")
for _ in range(100):
arg1, arg2 = randrange(*range1), randrange(*range2)
self.reset(arg1, arg2)
result = self.run_script(script)
if result != arg1 * arg2:
print("Test case failed! Try harder")
return False
return True
def main(self):
"""
Main function.
"""
print("Sample test case: 5 * -5 = -25")
self.reset(5, -5)
script = self.get_script()
result = self.run_script(script)
print("========== RESULT ===========")
print("Result: {}".format(result))
print('============ END ============')
if result == -25:
print("Sample test case passed. Good job! Beginning to evaluate real test cases.")
print('========== TESTING ==========')
if all((
self.test((0, self.MAX_VALUE), (0, self.MAX_VALUE), script),
self.test((-self.MAX_VALUE, 0), (0, self.MAX_VALUE), script),
self.test((0, self.MAX_VALUE), (-self.MAX_VALUE, 0), script),
self.test((-self.MAX_VALUE, 0), (-self.MAX_VALUE, 0), script),
self.test((-self.MAX_VALUE, self.MAX_VALUE), (-self.MAX_VALUE, self.MAX_VALUE), script)
)):
print("All test cases passed. Good job!")
print("Here's your flag:", os.environ.get('FLAG'))
else:
print("Some test cases failed. Try harder.")
else:
print("Sample test case failed. Try harder!")
print('============ END ============')
if __name__ == '__main__':
weird_machine = WeirdMachine(0, 0)
weird_machine.main()