-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday8.py
51 lines (40 loc) · 1.54 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
input_filename = __file__.split('.')[0] + ".input"
with open(input_filename) as f:
raw = f.read().strip().split('\n')
instructions = [x for x in raw]
def _run_program(program):
already_executed = [False] * len(program)
program_counter = 0
accumulator = 0
end_of_program = len(program)
while program_counter < end_of_program:
if program_counter < 0:
raise ValueError("Cannot jump below zero. Fix your code")
if already_executed[program_counter]:
# print(f"Line {program_counter} has run before. Accumulator is {accumulator}.")
return False, accumulator
already_executed[program_counter] = True
opcode, offset = program[program_counter].split()
if opcode == 'acc':
accumulator += int(offset)
if opcode == 'jmp':
program_counter += int(offset)
else:
program_counter += 1
# print("Program completed successfully")
return True, accumulator
# _run_program(instructions)
for line_no, line in enumerate(instructions):
if line.startswith('acc'):
continue
if line.startswith('nop'):
replaced_instruction = line.replace('nop', 'jmp')
else:
replaced_instruction = line.replace('jmp', 'nop')
temp_program = [x for x in instructions]
temp_program[line_no] = replaced_instruction
success, accumulator = _run_program(temp_program)
if not success:
continue
print(f"Sir, we found it! Line {line_no} was the problem")
print(f"Accumulator was {accumulator}")