-
Notifications
You must be signed in to change notification settings - Fork 0
/
d8-code.py
43 lines (36 loc) · 1.09 KB
/
d8-code.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
import pyperclip
lines = []
with open('day8-input.txt') as file:
for line in file:
line = line.rstrip().split(' ')
lines.append([line[0], int(line[1])])
def move(lines, part_1=False):
seen = set()
accumulator = 0
idx = 0
while True:
if idx >= len(lines):
return accumulator
move, arg = lines[idx]
if idx in seen:
return accumulator if part_1 else False
seen.add(idx)
if move == 'nop':
idx += 1
elif move == 'acc':
accumulator += arg
idx += 1
elif move == "jmp":
idx += arg
def flip(val):
return 'jmp' if val == 'nop' else 'nop'
def change_piece(lines):
for idx, turn in enumerate(lines):
if turn[0] == 'nop' or turn[0] == 'jmp':
prev = turn[0]
lines[idx][0] = flip(turn[0])
if accumulator:= move(lines):
return accumulator
lines[idx][0] = prev
print("Part 1:", move(lines, True))
print("Part 2:", change_piece(lines))