-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpart2.py
More file actions
71 lines (60 loc) · 1.68 KB
/
Copy pathpart2.py
File metadata and controls
71 lines (60 loc) · 1.68 KB
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
import collections
import sys
fname = "e:/Advent of Code/Day-12/input.txt" if len(sys.argv) < 2 else sys.argv[1]
out = 0
grid = []
total = 0
for line in open(fname):
line = line.strip()
grid += [line]
total += len(line)
used = set()
def p(a, b):
return (a[0] + b[0], a[1] + b[1])
def invert(a):
return (a[1], a[0])
def neg(a):
return (-1 * a[0], -1 * a[1])
while len(used) < total:
for i in range(len(grid)):
for j in range(len(grid[i])):
if (i,j) not in used:
current_region = set()
borders = []
num_borders = 0
queue = [(i,j)]
while queue:
n = queue.pop()
current_region.add(n)
ii = n[0]
jj = n[1]
for d in [(0,1), (1,0), (-1,0), (0,-1)]:
iii = ii + d[0]
jjj = jj + d[1]
if iii < 0 or iii >= len(grid) or jjj < 0 or jjj >= len(grid[iii]) or grid[iii][jjj] != grid[i][j]:
borders += [((iii, jjj), d)]
elif (iii, jjj) not in queue and (iii, jjj) not in current_region:
queue += [(iii, jjj)]
while borders:
pt, d = borders.pop()
print(pt, d)
flipped = invert(d)
pt2 = pt
while True:
pt2 = p(pt2, flipped)
if (pt2,d) in borders:
borders.remove((pt2, d))
else:
break
pt2 = pt
while True:
pt2 = p(pt2, neg(flipped))
if (pt2,d) in borders:
borders.remove((pt2, d))
else:
break
num_borders += 1
out += len(current_region) * num_borders
for n in current_region:
used.add(n)
print(out)