11import re , json
22
3- split_data = True
4- completed = False
5- raw_data = None # Not To be touched
6-
7- chart = {}
8-
93def process_data (data ):
104 regx = r'(\w+): capacity (-?\d+), durability (-?\d+), flavor (-?\d+), texture (-?\d+), calories (-?\d+)'
11- for line in data :
5+ for line in data . split ( ' \n ' ) :
126 name , capacity , durability , flavor , texture , calories = re .fullmatch (regx , line ).groups ()
137 chart [name ] = {'capacity' : int (capacity ), 'durability' : int (durability ), 'flavor' :int (flavor ), 'texture' :int (texture ), 'calories' :int (calories )}
148
9+ split_data = process_data
10+ completed = True
11+ raw_data = None # Not To be touched
12+
13+ chart = {}
14+
15+
1516def get_score (* args ):
1617 configs = [* chart .values ()]
17- capacity_score = sum (
18- # chart.values()[index] will give us the configuration for the corresponding ingredient
19- # args[index] will gives us how many times is the ingredient used
20- configs [index ]['capacity' ] * args [index ]
21- for index in range (len (args ))
22- )
23- durability_score = sum (configs [index ]['durability' ] * args [index ] for index in range (len (args )))
24- flavor_score = sum (configs [index ]['flavor' ] * args [index ] for index in range (len (args )))
25- texture_score = sum (configs [index ]['texture' ] * args [index ] for index in range (len (args )))
26-
27- final_score = capacity_score * durability_score * flavor_score * texture_score
18+ final_score = 1
19+ for key in ['capacity' , 'durability' , 'flavor' , 'texture' ]:
20+ final_score *= max (0 , sum (ingredient [key ] * count for ingredient , count in zip (configs , args )))
2821 return final_score
2922
3023def part1 (data ):
3124 """The Code is supposed to run here"""
32- process_data (data )
3325 highest_score = 0
3426 # Using this method is slow but still it's possible to solve the question like so
3527 for fros in range (1 , 100 ):
@@ -43,4 +35,17 @@ def part1(data):
4335
4436def part2 (data ):
4537 """The Code is supposed to run here"""
46- process_data (data )
38+ highest_score = 0
39+ # Using this method is slow but still it's possible to solve the question like so
40+ for fros in range (1 , 100 ):
41+ for cand in range (100 - fros , 1 , - 1 ):
42+ for butter in range (100 - fros - cand , 1 , - 1 ):
43+ sugar = 100 - fros - cand - butter
44+
45+ # Calculing the calories
46+ calories = sum (ingredient ['calories' ] * count for ingredient , count in zip (chart .values (), [fros , cand , butter , sugar ]))
47+ if calories != 500 : continue
48+
49+ # The Highest Score will be now the best one between this two
50+ highest_score = max (highest_score , get_score (fros , cand , butter , sugar ))
51+ return highest_score
0 commit comments