-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path24.py
67 lines (50 loc) · 1.26 KB
/
24.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
from lib import *
input = read_input(2015, 24)
packages = list(map(int, input.splitlines()))
gs = sum(packages) // 3
dp = {}
def test(i, x, pkg):
if x == 0:
return [[]]
if i == len(pkg):
return []
if (i, x) not in dp:
out = [[0] + y for y in test(i + 1, x, pkg)]
if packages[i] <= x:
out += [[1] + y for y in test(i + 1, x - pkg[i], pkg)]
dp[(i, x)] = out
return dp[(i, x)]
arr = []
for x in test(0, gs, packages):
f = [z for y, z in zip(x, packages) if y]
if not test(0, gs, f):
continue
p = 1
for y in f:
p *= y
arr.append((len(f), p))
print(min(arr)[1])
packages = list(map(int, input.splitlines()))
gs = sum(packages) // 4
dp = {}
def test(i, x, pkg):
if x == 0:
return [[]]
if i == len(pkg):
return []
if (i, x) not in dp:
out = [[0] + y for y in test(i + 1, x, pkg)]
if packages[i] <= x:
out += [[1] + y for y in test(i + 1, x - pkg[i], pkg)]
dp[(i, x)] = out
return dp[(i, x)]
arr = []
for x in test(0, gs, packages):
f = [z for y, z in zip(x, packages) if y]
if not test(0, gs, f):
continue
p = 1
for y in f:
p *= y
arr.append((len(f), p))
print(min(arr)[1])