-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay15.py
77 lines (70 loc) · 2.48 KB
/
Day15.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
from PuzzleInput import ReadInput
pz_input = ReadInput(15).data
def part_a():
ingredients = []
max_score = 0
for item in pz_input:
if item == "":
continue
pieces = item.split(" ")
ingredients.append([int(pieces[2][:-1]), int(pieces[4][:-1]), int(pieces[6][:-1]), int(pieces[8][:-1]), int(pieces[10])])
amount = [0] * 4
while amount[0] < 100:
for i in range(len(ingredients)-1, -1, -1):
amount[i] += 1
if amount[i] <= 100:
break
else:
amount[i] = 0
if sum(amount) != 100:
continue
capacity = 0
durability = 0
flavor = 0
texture = 0
for i in range(len(ingredients)):
capacity += ingredients[i][0] * amount[i]
durability += ingredients[i][1] * amount[i]
flavor += ingredients[i][2] * amount[i]
texture += ingredients[i][3] * amount[i]
curr_score = capacity * durability * flavor * texture if capacity > 0 and durability > 0 and flavor > 0 and texture > 0 else 0
if curr_score > max_score:
max_score = curr_score
return max_score
def part_b():
ingredients = []
max_score = 0
for item in pz_input:
if item == "":
continue
pieces = item.split(" ")
ingredients.append(
[int(pieces[2][:-1]), int(pieces[4][:-1]), int(pieces[6][:-1]), int(pieces[8][:-1]), int(pieces[10])])
amount = [0] * 4
while amount[0] < 100:
for i in range(len(ingredients) - 1, -1, -1):
amount[i] += 1
if amount[i] <= 100:
break
else:
amount[i] = 0
if sum(amount) != 100:
continue
capacity = 0
durability = 0
flavor = 0
texture = 0
calories = 0
for i in range(len(ingredients)):
capacity += ingredients[i][0] * amount[i]
durability += ingredients[i][1] * amount[i]
flavor += ingredients[i][2] * amount[i]
texture += ingredients[i][3] * amount[i]
calories += ingredients[i][4] * amount[i]
curr_score = capacity * durability * flavor * texture if capacity > 0 and durability > 0 and flavor > 0 and texture > 0 else 0
if curr_score > max_score and calories == 500:
max_score = curr_score
return max_score
if __name__ == "__main__":
print("Part A", part_a())
print("Part B", part_b())