-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path24.py
56 lines (53 loc) · 1.62 KB
/
24.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
from collections import defaultdict
f = open("23.in")
grid = defaultdict(list)
bounds = 0
for y, line in enumerate(f.readlines()):
for x, cell in enumerate(line):
if cell != "\n":
grid[cell].append((x,y))
bounds = y+1
regions = defaultdict(list)
def explore(t, x, y, f):
global grid
contig = [(x,y)]
f.append((x,y))
if (x+1, y) in grid[t] and (x+1, y) not in f:
contig.extend(explore(t, x+1, y, f))
if (x-1, y) in grid[t] and (x-1, y) not in f:
contig.extend(explore(t, x-1, y, f))
if (x, y+1) in grid[t] and (x, y+1) not in f:
contig.extend(explore(t, x, y+1, f))
if (x, y-1) in grid[t] and (x, y-1) not in f:
contig.extend(explore(t, x, y-1, f))
return contig
for t in grid:
found = []
while len(found) != len(grid[t]):
found = []
for f in regions[t]:
found.extend(f)
for pos in grid[t]:
if pos not in found:
regions[t].append(explore(t, pos[0], pos[1], [pos]))
break
total = 0
for t in regions:
for region in regions[t]:
sides = 0
tops = []
bottoms = []
for cell in region:
if (cell[0], cell[1]+1) not in region:
tops.append(cell)
if (cell[0], cell[1]-1) not in region:
bottoms.append(cell)
for top in tops:
if (top[0]+1,top[1]) not in tops:
sides += 1
for bottom in bottoms:
if (bottom[0]+1,bottom[1]) not in bottoms:
sides += 1
sides *= 2
total += sides * len(region)
print(total)