Advent of Code 2020, now in the most terse and awful python possible
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

48 lines
826 B

4 years ago
#!/usr/bin/env python3
from sys import argv
i = [x.strip() for x in open(argv[1])]
b = {}
r = {}
for l in i:
c, l = l.split(" bags contain ")
if c not in b:
b[c] = {}
if l.startswith("no other bags"):
continue
l = l.split(", ")
for i in l:
i = i.split(" ")
n = int(i[0])
cc = " ".join(i[1:3])
b[c][cc] = n
if cc not in r:
r[cc] = set()
r[cc].add(c)
o = set()
def add(x):
global o
o.add(x)
if x in r:
for c in r[x]:
4 years ago
if c not in o:
add(c)
4 years ago
add("shiny gold")
print(len(o)-1)
4 years ago
ch = {}
4 years ago
def add(x):
o = 1
for c in b[x]:
4 years ago
if c in ch:
o += ch[c]*b[x][c]
else:
o += add(c)*b[x][c]
ch[x] = o
4 years ago
return o
o = add("shiny gold")
print(o-1)