rename stuff
This commit is contained in:
1 parent
7c3d439f6e
commit
97ddc36b62
325 files changed
+85
No files matched your search
@@ -0,0 +1,61 @@
|
||||
import re
|
||||
|
||||
|
||||
# find colours and the colours it can contain
|
||||
def parse(l):
|
||||
group = re.compile(r"(?P<colour>.*?) bags contain").search(l)
|
||||
group2 = re.compile(r"(?P<number>\d+) (?P<colour>.*?) bag").findall(l)
|
||||
return group.group(1), group2
|
||||
|
||||
|
||||
# count what a bag of given colour can contain
|
||||
def containments(colour):
|
||||
if colour not in dic:
|
||||
memo[colour] = 0
|
||||
|
||||
if colour in memo:
|
||||
return memo[colour]
|
||||
|
||||
total = 0
|
||||
for (x, y) in dic[colour]:
|
||||
total += int(x) * containments(y)
|
||||
memo[colour] = total
|
||||
return total
|
||||
|
||||
|
||||
# count number of bags, that can be contained
|
||||
def number_bags(colour):
|
||||
if colour not in dic:
|
||||
memo2[colour] = 0
|
||||
|
||||
if colour in memo2:
|
||||
return memo2[colour]
|
||||
|
||||
total = 0
|
||||
for (x,y) in dic[colour]:
|
||||
total += int(x) + int(x) * number_bags(y)
|
||||
memo2[colour] = total
|
||||
return total
|
||||
|
||||
|
||||
with open(r"C:\Users\Maya\Desktop\Uni\WS_2021\AoC\input07") as f:
|
||||
lines = f.readlines()
|
||||
|
||||
# make dictionary with the values
|
||||
dic = {}
|
||||
for line in lines:
|
||||
key, value = parse(line)
|
||||
dic[key] = value
|
||||
|
||||
# for challenge 1
|
||||
memo = {"shiny gold": 1}
|
||||
|
||||
count = 0
|
||||
for key in dic.keys():
|
||||
if containments(key) > 0:
|
||||
count += 1
|
||||
print(count-1)
|
||||
|
||||
# print for challenge 2
|
||||
memo2 = {}
|
||||
print(number_bags("shiny gold"))
|
||||
Reference in new issue
Block a user