You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
55 lines
1.3 KiB
55 lines
1.3 KiB
import re
|
|
|
|
|
|
def decode(line):
|
|
groups = re.compile(r"(?P<op>\w\w\w) (?P<value>[+|-]\d+)").search(line)
|
|
return groups
|
|
|
|
|
|
def run(dict):
|
|
line = 0
|
|
acc = 0
|
|
while True:
|
|
(x, y) = dict[line]
|
|
if x == 'stop':
|
|
return acc
|
|
if x == 'nop':
|
|
dict[line] = ('stop', y)
|
|
line += 1
|
|
if x == 'acc':
|
|
dict[line] = ('stop', y)
|
|
acc += int(y)
|
|
line += 1
|
|
if x == 'jmp':
|
|
dict[line] = ('stop', y)
|
|
line = line + int(y)
|
|
if line > 632:
|
|
return acc
|
|
|
|
|
|
with open(r"C:\Users\Maya\Desktop\Uni\WS_2021\AoC\input08") as f:
|
|
lines = f.readlines()
|
|
|
|
memo = {}
|
|
|
|
for n, line in enumerate(lines):
|
|
memo[n] = decode(line).groups()
|
|
|
|
(x, y) = memo[405]
|
|
if memo[405] == ('nop', y):
|
|
memo[405] = ('jmp', y)
|
|
elif memo[405] == ('jmp', y):
|
|
memo[405] = ('nop', y)
|
|
|
|
print(run(memo))
|
|
|
|
for line in memo:
|
|
for n, l in enumerate(lines):
|
|
memo[n] = decode(l).groups()
|
|
(a, b) = memo[line]
|
|
if memo[line] == ('nop', b):
|
|
memo[line] = ('jmp', b)
|
|
elif memo[line] == ('jmp', b):
|
|
memo[line] = ('nop', b)
|
|
if run(memo) == 'here':
|
|
print(line)
|
|
|