-
Notifications
You must be signed in to change notification settings - Fork 0
/
2313.py
55 lines (53 loc) · 1.34 KB
/
2313.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
F = 0
r1, r2 = 0, 0
A = []
def dbg(T):
for t in T:print(t)
with open('13.' + str(F)) as file:
temp = []
for bloc in file:
bloc=bloc.strip()
if not bloc:
A.append(temp)
temp = []
else:
temp.append([_ for _ in bloc])
if temp:
A.append(temp)
# dbg(A)
for bloc in A:
p2 = True
def horver(bloc, p2=False): # yield either row index or col index
idx = 0
for i in range(1, len(bloc)):
diff = 0
u, d = i - 1, i
busted = False
while u > -1 and d < len(bloc):
for U, D in zip(bloc[u], bloc[d]):
if U != D:
diff += 1
if diff > 1:
busted = True
break
if busted:
break
u -= 1
d += 1
if not p2 and diff == 0 or p2 and diff == 1:
idx = i
break
return idx
tb = [_ for _ in zip(*bloc)] # transpose
lhs = horver(bloc)
rhs = horver(tb)
print('p1:', lhs, rhs)
r1 += lhs * 100 + rhs
lhs = horver(bloc,p2)
rhs = horver(tb,p2)
r2 += lhs * 100 + rhs
print('p2:', lhs, rhs)
print("Part 1:", r1)
assert(r1==37975)
print("Part 2:", r2)
assert(r2==32497)