-
Notifications
You must be signed in to change notification settings - Fork 0
/
day07.py
65 lines (48 loc) · 1.47 KB
/
day07.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
from collections import defaultdict
import re
import time
import functools
from aoc import get_input
rules = get_input(day=7)
def clean(bagstring):
number, modifier, color, bag = re.fullmatch(r'(\d+) ([a-z]+) ([a-z]+) (bag)s?', bagstring).groups()
return int(number), ' '.join((modifier, color))
fits_in = defaultdict(set)
holds = defaultdict(set)
for rule in rules:
outer, inners = rule.split(' contain ')
outer = outer[:-5]
for inner in inners.split(','):
inner = inner.strip().removesuffix('.')
if inner != 'no other bags':
number, inner = clean(inner)
fits_in[inner].add((number, outer))
holds[outer].add((number, inner))
print(fits_in)
print(holds)
# 7-1
def can_contain(color):
if color not in fits_in:
return {}
else:
return set.union({c for n, c in fits_in[color]},
*[can_contain(c) for n, c in fits_in[color]])
print(len(can_contain('shiny gold')))
# 7-2
def n_required(color):
if color not in holds:
return 1
else:
return 1 + sum(n * n_required(c) for n, c in holds[color])
@functools.lru_cache(10000)
def n_required_mem(color):
if color not in holds:
return 1
else:
return 1 + sum(n * n_required_mem(c) for n, c in holds[color])
t0 = time.time()
print(n_required('shiny gold') - 1)
print(f'{time.time() - t0} seconds')
t0 = time.time()
print(n_required_mem('shiny gold') - 1)
print(f'{time.time() - t0} seconds')