-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbacteria-colonies.py
96 lines (74 loc) · 2.74 KB
/
bacteria-colonies.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
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
def healthy(grid):
bestRadius = 0
bestR = 0
bestC = 0
for r, row in enumerate(grid):
for c, elem in enumerate(row):
radius = getHealthyRadius(grid, r, c)
if radius > bestRadius:
bestRadius = radius
bestR = r
bestC = c
return (bestR, bestC)
def getHealthyRadius(grid, r, c):
if not grid[r][c]:
return 0
radius = 1
while True:
hopefullyFullRing = getRingCells(grid, r, c, radius)
if not all(hopefullyFullRing):
break
radius += 1
hopefullyEmptyRing = getRingCells(grid, r, c, radius)
if any(hopefullyEmptyRing):
return 0
return radius
def getRingCells(grid, r, c, radius):
ringCoords = set()
for delR in range(radius + 1):
delC = radius - delR
ringCoords.add( (r + delR, c + delC) )
ringCoords.add( (r + delR, c - delC) )
ringCoords.add( (r - delR, c + delC) )
ringCoords.add( (r - delR, c - delC) )
ringCells = [grid[r2][c2] for r2, c2 in ringCoords
if inBounds(grid, r2, c2)]
return ringCells
def inBounds(grid, r, c):
return r >= 0 and c >= 0 and r < len(grid) and c < len(grid[r])
if __name__ == '__main__':
# These "asserts" using only for self-checking and not necessary for auto-testing
def check(result, answers):
return list(result) in answers
check(healthy(((0, 1, 0),
(1, 1, 1),
(0, 1, 0),)), [[1, 1]])
check(healthy(((0, 0, 1, 0, 0),
(0, 1, 1, 1, 0),
(0, 0, 1, 0, 0),
(0, 0, 0, 0, 0),
(0, 0, 1, 0, 0),)), [[1, 2]])
check(healthy(((0, 0, 1, 0, 0),
(0, 1, 1, 1, 0),
(0, 0, 1, 0, 0),
(0, 0, 1, 0, 0),
(0, 0, 1, 0, 0),)), [[0, 0]])
check(healthy(((0, 0, 0, 0, 0, 0, 1, 0),
(0, 0, 1, 0, 0, 1, 1, 1),
(0, 1, 1, 1, 0, 0, 1, 0),
(1, 1, 1, 1, 1, 0, 0, 0),
(0, 1, 1, 1, 0, 0, 1, 0),
(0, 0, 1, 0, 0, 1, 1, 1),
(0, 0, 0, 0, 0, 0, 1, 0),)), [[3, 2]])
check(healthy(((0, 0, 0, 0, 0, 0, 2, 0),
(0, 0, 0, 2, 2, 2, 2, 2),
(0, 0, 1, 0, 0, 0, 2, 0),
(0, 1, 1, 1, 0, 0, 2, 0),
(1, 1, 1, 1, 1, 0, 2, 0),
(0, 1, 1, 1, 0, 0, 2, 0),
(0, 0, 1, 0, 0, 0, 2, 0),
(0, 0, 0, 1, 0, 0, 2, 0),
(0, 0, 1, 1, 1, 0, 2, 0),
(0, 1, 1, 1, 1, 1, 0, 0),
(0, 0, 1, 1, 1, 0, 0, 0),
(0, 0, 0, 1, 0, 0, 0, 0),)), [[4, 2], [9, 3]])