Compare commits
2 Commits
3db283a57d
...
dc12f63058
| Author | SHA1 | Date | |
|---|---|---|---|
| dc12f63058 | |||
| 42876de159 |
@@ -17,14 +17,14 @@ def p1():
|
||||
for element in sample[1:]:
|
||||
if 'no' in element:
|
||||
continue
|
||||
data.append(element.split(' ', 1))
|
||||
data.append(tuple(element.split(' ', 1)))
|
||||
pwd[sample[0]] = data
|
||||
|
||||
@functools.lru_cache(maxsize=256)
|
||||
def contains_gold(y):
|
||||
return 'shiny gold' == y or any(map(lambda x: contains_gold(x[1]), pwd[y]))
|
||||
|
||||
s = sum(map(lambda x: any(map(lambda x: contains_gold(x[1]), pwd[x])), pwd))
|
||||
s = sum(map(lambda x: any(map(lambda y: contains_gold(y[1]), pwd[x])), pwd))
|
||||
|
||||
|
||||
print(s)
|
||||
@@ -53,12 +53,12 @@ def p2(r):
|
||||
if part_one():
|
||||
start = time.time()
|
||||
result1 = p1()
|
||||
print(round(time.time() - start, 6), 's')
|
||||
print(round(1000*(time.time() - start), 2), 'ms')
|
||||
|
||||
|
||||
if part_two():
|
||||
start = time.time()
|
||||
p2(result1)
|
||||
print(round(time.time() - start, 6), 's')
|
||||
print(round(1000*(time.time() - start), 2), 'ms')
|
||||
|
||||
|
||||
|
||||
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