-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathP17679.py
64 lines (56 loc) · 2.09 KB
/
P17679.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
from time import perf_counter_ns as ns
def remove_blocks(b, h: int, w: int) -> int:
block = ((0, 0), (0, 1), (1, 0), (1, 1))
blocks = []
for r in range(h - 1):
for c in range(w - 1):
v = [b[r + dr][c + dc] for dr, dc in block]
if v[0] and v.count(v[0]) == 4:
blocks += [(r + dr, c + dc) for dr, dc in block]
num_removed = len(unique_blocks := set(blocks))
for r, c in unique_blocks:
b[r][c] = 0
return num_removed
def drop(b, h: int, w: int) -> None:
for r in range(1, h):
for c in range(w):
if b[r][c]:
continue
for k in range(r, 0, -1):
if not b[k - 1][c]:
break
b[k][c], b[k - 1][c] = b[k - 1][c], b[k][c]
def solution(h: int, w: int, b) -> int:
b = [[c for c in row] for row in b]
ans = 0
while True:
if not (num_removed := remove_blocks(b, h, w)):
return ans
ans += num_removed
drop(b, h, w)
if __name__ == '__main__':
ITERATIONS = 1_000
print(f'Running the basic tests {ITERATIONS:,} times...')
tests = (
(4, 5, ["CCBDE", "AAADE", "AAABF", "CCBBF"], 14),
(6, 6, ["TTTANT", "RRFACC", "RRRFCC", "TRRRAA", "TTMMMF", "TMMTTJ"],
15)
)
for m, n, board, expected in tests:
print(f'solution({m}, {n}, {board}) returned', end=' ')
if (result := solution(m, n, board)) == expected:
print(f'the expected result {expected}', end=' ')
fastest = float('inf')
slowest = total = 0
for _ in range(ITERATIONS):
start = ns()
solution(m, n, board)
end = ns()
time = end - start
fastest, slowest = min(time, fastest), max(time, slowest)
total += time
print(f'in an average of {total / ITERATIONS / 1e3:,.2f}μs '
f'(min: {fastest / 1e3:,.2f}μs, '
f'max: {slowest / 1e3:,.2f}μs)')
else:
print(f'a wrong result {result} (expected: {expected})')