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
|
import sys
import re
from copy import deepcopy
from math import gcd
from collections import defaultdict, Counter, deque
D = open(sys.argv[1]).read().strip()
L = D.split("\n")
G = [[c for c in row] for row in L]
R = len(G)
C = len(G[0])
DR = [-1, 0, 1, 0]
DC = [0, 1, 0, -1]
def step(r, c, d):
return (r + DR[d], c + DC[d], d)
def score(sr, sc, sd):
POS = [(sr, sc, sd)]
SEEN = set()
SEEN2 = set()
while True:
NP = []
if not POS:
break
for r, c, d in POS:
if 0 <= r < R and 0 <= c < C:
SEEN.add((r, c))
if (r, c, d) in SEEN2:
continue
SEEN2.add((r, c, d))
ch = G[r][c]
if ch == ".":
NP.append(step(r, c, d))
elif ch == "/":
NP.append(step(r, c, {0: 1, 1: 0, 2: 3, 3: 2}[d]))
elif ch == "\\":
NP.append(step(r, c, {0: 3, 1: 2, 2: 1, 3: 0}[d]))
elif ch == "|":
if d in [0, 2]:
NP.append(step(r, c, d))
else:
NP.append(step(r, c, 0))
NP.append(step(r, c, 2))
elif ch == "-":
if d in [1, 3]:
NP.append(step(r, c, d))
else:
NP.append(step(r, c, 1))
NP.append(step(r, c, 3))
else:
assert False
POS = NP
return len(SEEN)
print(score(0, 0, 1))
ans = 0
for r in range(R):
ans = max(ans, score(r, 0, 1))
ans = max(ans, score(r, C - 1, 3))
for c in range(C):
ans = max(ans, score(0, c, 2))
ans = max(ans, score(R - 1, c, 0))
print(ans)
|