-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday15p1.py
More file actions
executable file
·47 lines (35 loc) · 1.05 KB
/
day15p1.py
File metadata and controls
executable file
·47 lines (35 loc) · 1.05 KB
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
#!/usr/bin/env python
import re
def comb_sum(total):
for x in xrange(0, total + 1):
for y in xrange(0, total + 1 - x):
for z in xrange(0, total + 1 - x - y):
yield (x, y, z, total - x - y - z)
with open('input/day15.txt') as fh:
data = fh.read().rstrip('\n').split('\n')
pat = re.compile(
'(\w+): capacity (-?\d+), durability (-?\d+), flavor (-?\d+), '
'texture (-?\d+), calories (-?\d+)'
)
ingredients = list()
max_product = 0
for line in data:
m = re.search(pat, line)
if not m:
raise RuntimeError('Bad line!')
ingredients.append(
(int(m.group(2)), int(m.group(3)), int(m.group(4)), int(m.group(5)))
)
for ratio in comb_sum(100):
product = 1
for idx in xrange(4):
prop_list = [x[idx] for x in ingredients]
total = sum(x * y for x, y in zip(ratio, prop_list))
if total <= 0:
product = 0
break # No good
else:
product *= total
if product > max_product:
max_product = product
print max_product