-
Notifications
You must be signed in to change notification settings - Fork 0
/
17.py
58 lines (33 loc) · 1007 Bytes
/
17.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
from lib import *
input = read_input(2015, 17)
containers = list(map(int, input.splitlines()))
dp = {}
def solve1(i, x):
if i == len(containers):
return int(x == 0)
if (i, x) not in dp:
out = solve1(i + 1, x)
if containers[i] <= x:
out += solve1(i + 1, x - containers[i])
dp[(i, x)] = out
return dp[(i, x)]
print(solve1(0, 150))
containers = list(map(int, input.splitlines()))
dp = {}
def solve2(i, x):
if i == len(containers):
return None if x else (0, 1)
if (i, x) not in dp:
out = solve2(i + 1, x)
if containers[i] <= x:
y = solve2(i + 1, x - containers[i])
if y is not None:
l, c = y
l += 1
if out is None or l < out[0]:
out = l, c
elif out is not None and out[0] == l:
out = l, out[1] + c
dp[(i, x)] = out
return dp[(i, x)]
print(solve2(0, 150)[1])