-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday8.py
executable file
·78 lines (62 loc) · 1.77 KB
/
day8.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
from copy import deepcopy
with open('test.txt', 'r') as f:
BROKEN_CODE = [command.split(' ') + [False] for command in f.readlines()]
class Command:
ptr = 0
accumulator = 0
instruction = None
def __init__(self, cmd: list):
self._cmd = cmd[0]
self._value = int(cmd[1])
def execute(self):
if Command.instruction[Command.ptr][2] is True:
raise ValueError(Command.accumulator)
Command.instruction[Command.ptr][2] = True
if self._cmd == 'nop':
Command.ptr += 1
elif self._cmd == 'acc':
Command.ptr += 1
Command.accumulator += self._value
elif self._cmd == 'jmp':
Command.ptr += self._value
@property
def next(self):
try:
next_cmd = Command.instruction[Command.ptr]
return Command(next_cmd)
except IndexError:
return None
def run(instruction: list) -> int:
Command.instruction = instruction
node = Command(Command.instruction[0])
while node.next:
node.execute()
if node.next:
node = node.next
else:
break
return node.accumulator
# Part 1:
print('=====Part 1============')
try:
attempt = deepcopy(BROKEN_CODE)
print(f'Execution result: {run(attempt)}')
except ValueError as e:
print(f'ValueError: {str(e)}')
# Part 2
print('=====Part 2============')
for i in range(len(BROKEN_CODE)):
attempt = deepcopy(BROKEN_CODE)
Command.ptr = 0
Command.accumulator = 0
if attempt[i][0] == 'nop':
attempt[i][0] = 'jmp'
elif attempt[i][0] == 'jmp':
attempt[i][0] = 'nop'
else:
continue
try:
print(f'Execution result: {run(attempt)}')
break
except ValueError:
continue