-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathp126.py
More file actions
230 lines (175 loc) · 6.1 KB
/
p126.py
File metadata and controls
230 lines (175 loc) · 6.1 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
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
from math import ceil
from collections import defaultdict
from functions import factorial
'''
I'm not doing it right: Not counting properly the multi-layered layers.
Let's think in one lower dimension
On 1D, we need 2 blocks for layer N of a cuboid of dimensions x
####### k = 6 blocks
·#######· layer 1 = 2 blocks needed
On 2D, we need ? blocks for layer N of a cuboid of dimensions x*y
2*3 = 6 blocks
### x = 3, y = 2
###
··· layer 1
·###· needed 2*3 + 2*2 + 4*0 = 10 blocks
·###·
···
···
·###· layer 2
·#####· needed 2*3 + 2*2 + 4*1 = 14 blocks
·#####·
·###·
···
It seems that for layer n, we need (2*x + 2*y + 4*(l-1)) blocks to recover and so that it origins from a valid cuboid
So, for a number of n blocks, if it satisfies 2*x + 2*y + 4*(l - 1) == n, for some x, y, l, then we know it's a valid wrap
On 3D
x = 3, y = 2, z = 1
layer 1
needs 2 * 3*2 + 2 * 3*1 + 2 * 2*1 = 22 blocks
layer 2
needs 2 * 3*2 + 2 * 3*1 + 2 * 2*1 + 4 * 3 + 4 * 2 + 4 * 1 = 22 + 12 + 8 + 4 = 46 46-22 = 24
base + 4 (x+y+z)
layer 3
needs 2 * 3*2 + 2 * 3*1 + 2 * 2*1 + 2*4*3*1 + 2*4*2*1 + 2*4*1*1 = 22 + 24 + 16 + 8 = 54 + 16 = 70 (8 missing)? 78-46 = 32
layer 4
118-78 = 40
'''
def C(n):
set_sols = defaultdict(int)
result = 0
for x in range(1, n):
for y in range(1, x + 1):
x_times_y = x*y
if x_times_y > n:
break
for z in range(1, y + 1):
if x_times_y * z > n:
break
layer = 1
blocks = blocks_needed(x, y, z, layer)
while blocks < n:
layer = layer + 1
blocks = blocks_needed(x, y, z, layer)
if blocks == n:
# print(f'Valid solution for {n}: ({x}, {y}, {z}, {layer})')
result = result + 1
return result
# suppose n == 22
'''
algorithm: start from middle layer, and fill outwards, where layers can only become smaller or stay the same
take x*y, making sure that x*y + 2(x+y) <= n
for example: x = 2, y = 3
6 + 2*5 = 16 <= 22, so it's a valid check
for each starting position, while not cut:
if can add, must add
if exceeded number, break
if cannot add and not exceeded:
if valid, count solution
must hold:
2xy + k(2x + 2y) = n
2xy + 2k(x + y) = n
2 (xy + k(x+y)) = n
xy + k(x+y) = n/2
For n = 22
xy + k(x+y) = 11
x=2, y=3, k=1 is a solution
x=5, y=1, k=1 is a solution
distinct solution sets -> 2
C(22) = 2?
for each solution of the equation
result = result + inner_ways(x, y)
return result
'''
def blocks_needed(x, y, z, layer):
result = 2 * (x*y + x*z + y*z)
if layer >= 2:
result = result + 4*(x+y+z) * (layer - 1)
if layer >= 3:
result = result + 8*(layer-2)*(layer-1)//2
return result
assert blocks_needed(3, 2, 1, 1) == 22
assert blocks_needed(3, 2, 1, 2) == 46 # +24
assert blocks_needed(3, 2, 1, 3) == 78 # +24 + 8
assert blocks_needed(3, 2, 1, 4) == 118, blocks_needed(3, 2, 1, 4) # +24 + 8 + 8
last_res = C(22)
assert last_res == 2, last_res
last_res = C(46)
assert last_res == 4, last_res
last_res = C(78)
assert last_res == 5, last_res
last_res = C(118)
assert last_res == 8, last_res
last_res = C(154)
assert last_res == 10, last_res
#print(C(100000)) # 100000 can be done from 1109 cuboids
def generate(n):
sols = defaultdict(int)
for x in range(1, n):
for y in range(1, x + 1):
x_times_y = x*y
if x_times_y > n:
break
for z in range(1, y + 1):
if x_times_y * z > n:
break
fill_layers(n, x, y, z, sols)
return sols
def fill_layers(n, x, y, z, sols):
layer = 1
blocks = blocks_needed(x, y, z, layer)
while blocks < n:
sols[blocks] = sols[blocks] + 1
layer = layer + 1
blocks = blocks_needed(x, y, z, layer)
sols = generate(200)
expected_sols = 10
for k, v in sols.items():
if v == expected_sols:
print(f'Solution with {expected_sols} sols: {k}')
def fill_2d(y, z, l):
return 2*y + 2*z + 4*(l - 1)
assert fill_2d(3, 2, 2) == 14
def fill_3d(x, y, z, l):
result = x * fill_2d(y, z, l) # Fill the pure "inner blocks"
result = result + 2*z*y # Fill the outter most blocks
# Original code for staircase blocks:
# for layer in range(1, l):
# result = result + 2*fill_2d(y, z, layer) # Fill the staircase blocks in between
# Through some transformations we arrive to:
if l >= 2:
result = result + 4*(l - 1) * (y + z + l - 2)
return result
assert fill_3d(5, 1, 1, 1) == 22, fill_3d(5, 1, 1, 1)
assert fill_3d(3, 2, 1, 1) == 22
assert fill_3d(3, 2, 1, 2) == 46
assert fill_3d(3, 2, 1, 3) == 78
assert fill_3d(3, 2, 1, 4) == 118
"""
filling(3, 2, 1, layer=1) = 3 * fill_2d(2, 1, layer=1) + 2 * fill_side(1, 2, 1, layer=0)
layer 0 means it is the outter layer, i.e., the count of blocks we need
"""
def generate2(n):
sols = defaultdict(int)
for x in range(1, n):
for y in range(1, x + 1):
x_times_y = x*y
if x_times_y > n:
break
for z in range(1, y + 1):
if x_times_y * z > n:
break
fill_layers2(n, x, y, z, sols)
return sols
def fill_layers2(n, x, y, z, sols):
layer = 1
blocks = fill_3d(x, y, z, layer)
while blocks < n:
sols[blocks] = sols[blocks] + 1
layer = layer + 1
blocks = fill_3d(x, y, z, layer)
sols = generate2(200000)
expected_sols = 1000
for k, v in sols.items():
if v == expected_sols:
print(f'Solution with {expected_sols} sols: {k}') # Find min by hand from there