-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday8.py
More file actions
68 lines (62 loc) · 2.46 KB
/
day8.py
File metadata and controls
68 lines (62 loc) · 2.46 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
import re
with open("./input8.txt", encoding="utf-8") as file:
instructions = [l.rstrip("\n") for l in file]
def part1():
accumulator = 0
instructions_history = [0]
cursor = 0
instruction = instructions[0].split(' ')
execute_instruction_part1(instruction[0], int(instruction[1]), cursor, accumulator, instructions_history)
def execute_instruction_part1(operation, value, cursor, accumulator, instructions_history):
instructions_history.append(cursor)
if operation == 'nop':
cursor += 1
elif operation == 'acc':
cursor += 1
accumulator += value
else:
cursor += value
if cursor not in instructions_history:
instruction = instructions[cursor].split(' ')
execute_instruction_part1(instruction[0], int(instruction[1]), cursor, accumulator, instructions_history)
else:
print(accumulator)
def execute_instruction_part2(operation, value, cursor, accumulator, instructions_history, instructions_changed, instruction_changed_performed):
instructions_history.append(cursor)
if operation == 'nop':
if instruction_changed_performed or cursor in instructions_changed:
cursor += 1
else:
instruction_changed_performed = True
instructions_changed.append(cursor)
cursor += value
elif operation == 'acc':
cursor += 1
accumulator += value
else:
if instruction_changed_performed or cursor in instructions_changed:
cursor += value
else:
instruction_changed_performed = True
instructions_changed.append(cursor)
cursor += 1
if cursor == len(instructions):
print(accumulator)
return True
elif cursor not in instructions_history:
instruction = instructions[cursor].split()
return execute_instruction_part2(instruction[0], int(instruction[1]), cursor, accumulator, instructions_history, instructions_changed, instruction_changed_performed)
else:
return False
def part2():
instructions_changed = []
result_found = False
while not result_found:
accumulator = 0
instructions_history = []
cursor = 0
instruction_changed_performed = False
instruction = instructions[0].split()
result_found = execute_instruction_part2(instruction[0], int(instruction[1]), cursor, accumulator, instructions_history, instructions_changed, instruction_changed_performed)
part1()
part2()