-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcode_reader.py
47 lines (39 loc) · 1.32 KB
/
code_reader.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
def run(operation):
global res, op_num
if operation[0] == 'acc':
res += operation[1]
elif operation[0] == 'jmp':
op_num += operation[1] - 1
op_num += 1
def switch(instruction):
return 'jmp' if instruction == 'nop' else 'nop'
with open('/users/sysadmin/Documents/Prajwal/Programming/Competitions/Advent of Code/input.txt') as fin:
finished = False
operations = []
while not finished:
line = fin.readline().strip()
if not line:
finished = True
else:
operations.append((line[:3], int(line[4:])))
finished = False
i = 0
while i in range(len(operations)) and not finished:
if operations[i][0] != 'acc':
operations[i] = switch(operations[i][0]), operations[i][1]
res = 0
op_num = 0
seen_op_nums = set()
run_finished = False
while not run_finished:
run(operations[op_num])
if op_num in seen_op_nums:
run_finished = True
elif op_num >= len(operations):
run_finished = True
finished = True
else:
seen_op_nums.add(op_num)
operations[i] = switch(operations[i][0]), operations[i][1]
i += 1
print(res)