Skip to content

Instantly share code, notes, and snippets.

@vjeranc
Last active December 10, 2023 19:25
Show Gist options
  • Save vjeranc/cec27fb4d84b82c88e1bf225194b454d to your computer and use it in GitHub Desktop.
Save vjeranc/cec27fb4d84b82c88e1bf225194b454d to your computer and use it in GitHub Desktop.
G = open(0).read().splitlines()
N, M = len(G), len(G[0])
# dest needs to be valid and the dest direction change is predetermined
# (dx, dy, valid_dest, next MS index)
MS = [(1, 0, "|LJS", [0, 1, 3, 2]), (0, 1, "-J7S", [1, 2, 0, 3]),
(-1, 0, "|7FS", [2, 3, 1, 0]), (0, -1, "-FLS", [3, 0, 2, 1])]
sj, si = max((l.find('S'), i) for i, l in enumerate(G))
best = 0, 0
for d in range(3): # trying 3 directions is enough
x, y, l, area = si, sj, 1, 0
ps = x, y, x, y # area calculation
while True:
dx, dy, v, nxtds = MS[d]
nx, ny = x+dx, y+dy
if 0 <= nx < N and 0 <= ny < M and (nxti := v.find(G[nx][ny])) != -1:
x, y, l, d = nx, ny, l+1, nxtds[nxti]
x1, y1 = ps[:2]
area += x1*y-x*y1
ps = x, y, x1, y1
else:
l = 0 # unreachable
break
if nx==si and ny==sj: break
best = max(best, (l, area//2))
ans, A = best
print(ans//2) # Part 1
print(A-ans//2+1) # Part 2: Pick's theorem
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment