From 3db283a57d33b44098068e739041e5f4c422a8e1 Mon Sep 17 00:00:00 2001 From: Matthias Bilger Date: Mon, 7 Dec 2020 12:50:35 +0100 Subject: [PATCH] use lru cache --- day07/module.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/day07/module.py b/day07/module.py index 46c00ab..242b370 100644 --- a/day07/module.py +++ b/day07/module.py @@ -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')