-
Notifications
You must be signed in to change notification settings - Fork 0
/
day7.py
58 lines (51 loc) · 1.54 KB
/
day7.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
#!/usr/bin/env python3
import sys
def part1(rules):
# create a private copy as we are going to modify rules
rules = rules.copy()
count = 0
search = [ 'shiny gold' ]
l = []
while True:
new_search = []
for color, contents in rules.items():
for s in search:
if s in contents:
new_search.append(color)
l.append(color)
count += 1
break
if len(new_search) == 0:
break
for color in new_search:
rules.pop(color, None)
search = new_search
return count
def count_contents(color, rules):
count = 0
for color, c in rules[color].items():
count += c * (1 + count_contents(color, rules))
return count
def part2(rules):
return count_contents('shiny gold', rules)
def main(arguments):
f = open('inputs/day7', 'r')
rules = {}
for rule in f.read().split('\n'):
if len(rule) == 0:
continue
bag, contents = rule.split('contain ')
bag = bag.split(' bags ')[0]
contents = contents.strip('.')
c = {}
if contents != 'no other bags':
for content in contents.split(', '):
num, color = content.split(' ', 1)
num = int(num)
color = color.split(' bag')[0]
c[color] = num
rules[bag] = c
print(f'Part 1: {part1(rules)}')
print(f'Part 2: {part2(rules)}')
if __name__ == '__main__':
sys.exit(main(sys.argv[1:]))