day08
This commit is contained in:
95
day08/module.py
Normal file
95
day08/module.py
Normal file
@@ -0,0 +1,95 @@
|
|||||||
|
from aoc.input import get_input
|
||||||
|
import copy
|
||||||
|
import time
|
||||||
|
import collections
|
||||||
|
import re
|
||||||
|
from aoc.partselector import part_one, part_two
|
||||||
|
import functools
|
||||||
|
|
||||||
|
def pw(line):
|
||||||
|
return line.strip().split(' ')
|
||||||
|
|
||||||
|
def p1():
|
||||||
|
inp = get_input(pw)
|
||||||
|
|
||||||
|
visited = set()
|
||||||
|
current = 0
|
||||||
|
value = 0
|
||||||
|
while current not in visited:
|
||||||
|
visited.add(current)
|
||||||
|
if inp[current][0] == 'nop':
|
||||||
|
current += 1
|
||||||
|
continue
|
||||||
|
if inp[current][0] == 'jmp':
|
||||||
|
if inp[current][1][0] == '+':
|
||||||
|
current += int(inp[current][1][1:])
|
||||||
|
else:
|
||||||
|
current -= int(inp[current][1][1:])
|
||||||
|
continue
|
||||||
|
if inp[current][0] == 'acc':
|
||||||
|
if inp[current][1][0] == '+':
|
||||||
|
value += int(inp[current][1][1:])
|
||||||
|
else:
|
||||||
|
value -= int(inp[current][1][1:])
|
||||||
|
current += 1
|
||||||
|
continue
|
||||||
|
print(current)
|
||||||
|
print(value)
|
||||||
|
return inp
|
||||||
|
|
||||||
|
|
||||||
|
def p2(inp):
|
||||||
|
inp.append(['end', '0'])
|
||||||
|
orig = copy.deepcopy(inp)
|
||||||
|
for nopcount in range(0, len(inp)):
|
||||||
|
if inp[nopcount][0] not in ['nop', 'jmp']:
|
||||||
|
continue
|
||||||
|
visited = set()
|
||||||
|
current = 0
|
||||||
|
value = 0
|
||||||
|
inp = copy.deepcopy(orig)
|
||||||
|
print('-----')
|
||||||
|
print(inp[0])
|
||||||
|
while current not in visited:
|
||||||
|
print(current)
|
||||||
|
visited.add(current)
|
||||||
|
t = inp[current][0]
|
||||||
|
if current == nopcount:
|
||||||
|
if inp[current][0] == 'nop':
|
||||||
|
inp[current][0] = 'jmp'
|
||||||
|
else:
|
||||||
|
inp[current][0] = 'nop'
|
||||||
|
if (t != inp[current][0]):
|
||||||
|
print(t, inp[current][0], current, nopcount)
|
||||||
|
if inp[current][0] == 'nop':
|
||||||
|
current += 1
|
||||||
|
|
||||||
|
if inp[current][0] == 'jmp':
|
||||||
|
if inp[current][1][0] == '+':
|
||||||
|
current += int(inp[current][1][1:])
|
||||||
|
else:
|
||||||
|
current -= int(inp[current][1][1:])
|
||||||
|
if inp[current][0] == 'acc':
|
||||||
|
if inp[current][1][0] == '+':
|
||||||
|
value += int(inp[current][1][1:])
|
||||||
|
else:
|
||||||
|
value -= int(inp[current][1][1:])
|
||||||
|
current += 1
|
||||||
|
if inp[current][0] == 'end':
|
||||||
|
print('---', value)
|
||||||
|
return
|
||||||
|
print(value)
|
||||||
|
return 0
|
||||||
|
|
||||||
|
if part_one():
|
||||||
|
start = time.time()
|
||||||
|
result1 = p1()
|
||||||
|
print(round(1000*(time.time() - start), 2), 'ms')
|
||||||
|
|
||||||
|
|
||||||
|
if part_two():
|
||||||
|
start = time.time()
|
||||||
|
p2(result1)
|
||||||
|
print(round(1000*(time.time() - start), 2), 'ms')
|
||||||
|
|
||||||
|
|
||||||
Reference in New Issue
Block a user