Skip to content

Commit 984704d

Browse files
committed
Completed day 15 of year 2015
1 parent 4f12aa3 commit 984704d

2 files changed

Lines changed: 26 additions & 21 deletions

File tree

‎2015/15.py‎

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,27 @@
11
import re, json
22

3-
split_data = True
4-
completed = False
5-
raw_data = None # Not To be touched
6-
7-
chart = {}
8-
93
def 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+
1516
def 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

3023
def 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

4436
def 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

‎main.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
import config
1717
except ImportError:
1818
# Generate the config file
19-
print("You didn't have the config filem, so i created one! Please edit it and enter your session cookie in the file!")
19+
print("You didn't have the config file, so i created one! Please edit it and enter your session cookie in the file!")
2020
# Read the template
2121
with open('config-template.py','r') as file:
2222
lines = file.readlines()

0 commit comments

Comments
 (0)