-
Notifications
You must be signed in to change notification settings - Fork 2
/
calculate-islands.py
68 lines (49 loc) · 1.95 KB
/
calculate-islands.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
'''
author: Jacob Egner
date: 2015-06-25, or maybe a few days earlier
island: scientific expedition
puzzle prompt:
http://www.checkio.org/mission/calculate-islands/
puzzle prompt source repo:
https://github.com/Bryukh-Checkio-Tasks/checkio-task-calculate-islands
my checkio solution repo:
https://github.com/jmegner/CheckioPuzzles
this puzzle is very similar to the radiation-search puzzle from electronic
station island
'''
import itertools
def checkio(landGrid):
touchGrid = [[False] * len(landGrid[0]) for row in landGrid]
landSizes = []
for r, row in enumerate(landGrid):
for c, isLand in enumerate(row):
if isLand and not touchGrid[r][c]:
landSizes.append(touchAndGetSize(landGrid, touchGrid, r, c))
return sorted(landSizes)
def touchAndGetSize(landGrid, touchGrid, r, c):
if (r < 0 or c < 0 or r >= len(landGrid) or c >= len(landGrid[r])
or touchGrid[r][c] or not landGrid[r][c]):
return 0
touchGrid[r][c] = True
landSize = 1
for r2, c2 in itertools.product(range(r - 1, r + 2), range(c - 1, c + 2)):
landSize += touchAndGetSize(landGrid, touchGrid, r2, c2)
return landSize
#These "asserts" using only for self-checking and not necessary for auto-testing
if __name__ == '__main__':
assert checkio([[0, 0, 0, 0, 0],
[0, 0, 1, 1, 0],
[0, 0, 0, 1, 0],
[0, 1, 0, 0, 0],
[0, 0, 0, 0, 0]]) == [1, 3], "1st example"
assert checkio([[0, 0, 0, 0, 0],
[0, 0, 1, 1, 0],
[0, 0, 0, 1, 0],
[0, 1, 1, 0, 0]]) == [5], "2nd example"
assert checkio([[0, 0, 0, 0, 0, 0],
[1, 0, 0, 1, 1, 1],
[1, 0, 0, 0, 0, 0],
[0, 0, 1, 1, 1, 0],
[0, 0, 0, 0, 0, 0],
[0, 1, 1, 1, 1, 0],
[0, 0, 0, 0, 0, 0]]) == [2, 3, 3, 4], "3rd example"