forked from tomkooij/AdventOfCode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
day15.py
61 lines (46 loc) · 1.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
# adventofcode.com
# day15
import numpy as np
INPUTFILE = 'input/input15'
TESTCASE = ['Butterscotch: capacity -1, durability -2, flavor 6, texture 3, calories 8',
'Cinnamon: capacity 2, durability 3, flavor -2, texture -1, calories 3']
def parse(lines):
ingredients = []
for idx, line in enumerate(lines):
name, capcity, c, durability, d, flavor, f, texture, t, calories, cal = line.replace(',','').split()
ingredients.append(map(int, (c,d,f,t)))
return ingredients
def score(n, ingredients):
p = np.dot(n, ingredients)
# negative values -> 0
p = map(lambda x: max(0,x), p)
# multiply all elements
return reduce(lambda x,y: x*y, p)
if __name__ == '__main__':
# Testcase
ingredients = parse(TESTCASE)
solution = []
n = [0,0]
for i in range(1,100):
n[0] = i
n[1] = 100 - n[0]
solution.append((score(n, ingredients), n[0], n[1]))
winner = max(solution)
print winner
assert winner[0] == 62842880, 'Testcase failure!'
# part A
with open(INPUTFILE, 'r') as f:
lines = f.readlines()
ingredients = parse(lines)
solution = []
n = [0, 0, 0, 0]
for i in range(0,101):
n[0] = i
for j in range(0,101-i):
n[1] = j
for k in range(0,101-(i+j)):
n[2] = k
n[3] = 100 - (i+j+k)
solution.append((score(n, ingredients), n[0], n[1], n[2], n[3]))
winner = max(solution)
print winner