use lru cache

This commit is contained in:
2020-12-07 12:50:35 +01:00
parent a9c0e6da9e
commit 3db283a57d

View File

@@ -3,6 +3,7 @@ import time
import collections
import re
from aoc.partselector import part_one, part_two
import functools
def pw(line):
l = re.split(" bags?,?(?: contain)? ?", line.strip('.'))
@@ -12,16 +13,14 @@ def p1():
inp = get_input(pw)
pwd = dict()
for sample in inp:
key = sample[0]
data = []
for element in sample[1:]:
if 'no' in element:
continue
data.append(element.split(' ', 1))
pwd[key] = data
count = 0
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]))
@@ -36,6 +35,7 @@ def p2(r):
pwd = r
count = 0
@functools.lru_cache(maxsize=256)
def count_bags(y):
count = 1
for j in pwd[y]:
@@ -53,12 +53,12 @@ def p2(r):
if part_one():
start = time.time()
result1 = p1()
print(round(time.time() - start, 4), 's')
print(round(time.time() - start, 6), 's')
if part_two():
start = time.time()
p2(result1)
print(round(time.time() - start, 4), 's')
print(round(time.time() - start, 6), 's')