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.
		
		
		
		
		
			
		
			
				
					
					
						
							61 lines
						
					
					
						
							1.3 KiB
						
					
					
				
			
		
		
		
			
			
			
				
					
				
				
					
				
			
		
		
	
	
							61 lines
						
					
					
						
							1.3 KiB
						
					
					
				
								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"))
							 | 
						|
								
							 |