-
Notifications
You must be signed in to change notification settings - Fork 0
/
strategies.py
67 lines (59 loc) · 2.13 KB
/
strategies.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
import random
def boxes(grid):
prefix_sums = [
[0] * (len(grid[0]) + 1)
for _ in range(len(grid) + 1)
]
# compute rectangle sums
for i in range(1, len(grid) + 1):
for j in range(1, len(grid[0]) + 1):
prefix_sums[i][j] = (
grid[i - 1][j - 1] +
prefix_sums[i - 1][j] +
prefix_sums[i][j - 1] -
prefix_sums[i - 1][j - 1]
)
# for all rectangles, find ones that sum to 10
for start_i in range(1, len(grid) + 1):
for start_j in range(1, len(grid[0]) + 1):
for end_i in range(start_i, len(grid) + 1):
for end_j in range(start_j, len(grid[0]) + 1):
box_sum = (
prefix_sums[end_i][end_j] -
prefix_sums[start_i - 1][end_j] -
prefix_sums[end_i][start_j - 1] +
prefix_sums[start_i - 1][start_j - 1]
)
if box_sum == 10:
yield (
(start_i - 1, start_j - 1),
(end_i - 1, end_j - 1)
)
def first(grid):
copy = [row[:] for row in grid]
solutions = list(boxes(copy))
output = []
while solutions:
(start_i, start_j), (end_i, end_j) = solutions[0]
for i in range(start_i, end_i + 1):
for j in range(start_j, end_j + 1):
copy[i][j] = 0
output.append(solutions[0])
solutions = list(boxes(copy))
return output
def best_random(grid, attempts):
best = []
for _ in range(attempts):
copy = [row[:] for row in grid]
solutions = list(boxes(copy))
output = []
while solutions:
solution = random.choice(solutions)
(start_i, start_j), (end_i, end_j) = solution
for i in range(start_i, end_i + 1):
for j in range(start_j, end_j + 1):
copy[i][j] = 0
output.append(solution)
solutions = list(boxes(copy))
best = max(best, output, key=len)
return best