-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbaggage.py
48 lines (37 loc) · 1.34 KB
/
baggage.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
class Rules:
def __init__(self):
self.data = {}
def __setitem__(self, i, val):
self.data[i] = val
def __getitem__(self, i):
res = {ele[1] for ele in self.data[i]}
for _, col in self.data[i]:
res |= self[col]
return res
def __str__(self):
return repr(self)
def __repr__(self):
return repr(self.data)
def __iter__(self):
return iter(self.data)
def __contains__(self, val):
return val in self.data
def count(self, i):
return sum([ele[0]*(1 + self.count(ele[1])) for ele in self.data[i]])
with open('/users/sysadmin/Documents/Prajwal/Programming/Competitions/Advent of Code/input.txt') as fin:
finished = False
rules = Rules()
while not finished:
line = fin.readline().strip('.\n')
if not line:
finished = True
else:
bag, contents_str = tuple(line.split(' bags contain '))
contents = set()
if contents_str != 'no other bags':
contents_list = contents_str.split(', ')
for i in range(len(contents_list)):
num, colors = tuple(' '.join(contents_list[i].split()[:-1]).split(' ', 1))
contents.add((int(num), colors))
rules[bag] = contents
print(rules.count('shiny gold'))