Files
aoc2020/day07/module.py
2020-12-07 12:39:00 +01:00

65 lines
1.3 KiB
Python

from aoc.input import get_input
import time
import collections
import re
from aoc.partselector import part_one, part_two
def pw(line):
l = re.split(" bags?,?(?: contain)? ?", line.strip('.'))
return list(filter(len, l))
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
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))
print(s)
return pwd
def p2(r):
pwd = r
count = 0
def count_bags(y):
count = 1
for j in pwd[y]:
tmpcount = count_bags(j[1])
count += tmpcount * int(j[0])
return count
ret = 0
for t in pwd['shiny gold']:
ret += count_bags(t[1]) * int(t[0])
print(ret)
return ret
if part_one():
start = time.time()
result1 = p1()
print(round(time.time() - start, 4), 's')
if part_two():
start = time.time()
p2(result1)
print(round(time.time() - start, 4), 's')