generated from alvesvaren/AoC-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
07.py
35 lines (28 loc) · 1009 Bytes
/
07.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
from collections import defaultdict
import aoc
parents = defaultdict(set)
children = defaultdict(list)
data = aoc.get_input(7).splitlines()
for rule in data:
rule = rule.replace(".", "").replace(" bags", "").replace(" bag", "")
subj, other = rule.split(" contain ")
contains = [bag.split(" ", 1) for bag in other.split(", ")]
for count, color in contains:
try:
count = int(count)
except ValueError:
continue
children[subj].append((count, color))
parents[color].add(subj)
def recurse_gold(value="shiny gold", contains_gold=set()):
contains_gold.add(value)
for color in parents[value]:
recurse_gold(color, contains_gold)
return len(contains_gold)
def recurse_find_inner(value="shiny gold"):
total_count = 0
for count, color in children[value]:
total_count += count * recurse_find_inner(color) + count
return total_count
print("Part 1:", recurse_gold())
print("Part 2:", recurse_find_inner())