Last active
December 20, 2018 23:01
-
-
Save lynn/b3b601d67a3d6a35e6b46634c8c12398 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
sys.setrecursionlimit(20000) | |
stack = [[[]]] | |
for c in input().strip('^$'): | |
if c == '(': stack.append([[]]) # new fork | |
elif c == '|': stack[-1].append([]) # new tine | |
elif c == ')': fork = stack.pop(); stack[-1][-1].append(fork) | |
else: stack[-1][-1].append(c) | |
[[maze]] = stack | |
world = set() | |
def carve(maze, x=0, y=0): | |
global world | |
world |= {(x,y)} | |
if maze == []: return | |
if maze[0] == 'N': world |= {(x,y-1),(x,y-2)}; return carve(maze[1:], x, y-2) | |
if maze[0] == 'S': world |= {(x,y+1),(x,y+2)}; return carve(maze[1:], x, y+2) | |
if maze[0] == 'W': world |= {(x-1,y),(x-2,y)}; return carve(maze[1:], x-2, y) | |
if maze[0] == 'E': world |= {(x+1,y),(x+2,y)}; return carve(maze[1:], x+2, y) | |
tines, *rest = maze | |
if tines[-1] == []: # dead end | |
carve(tines[0], x, y) | |
carve(rest, x, y) | |
else: | |
assert rest == [] | |
for t in tines: carve(t, x, y) | |
carve(maze) | |
xmin = min(x for x,y in world) | |
xmax = max(x for x,y in world) | |
ymin = min(y for x,y in world) | |
ymax = max(y for x,y in world) | |
#for y in range(ymin-2, ymax+3): | |
# for x in range(xmin-2, xmax+3): | |
# print('\N{FULL BLOCK} '[(x,y) in world], end='') | |
# print() | |
frontier = {(0, 0)} | |
depth = 0 | |
seen = set() | |
distance = {} | |
while frontier: | |
for k in frontier: distance[k] = depth | |
seen |= frontier | |
frontier = {(x+dx, y+dy) for (x, y) in frontier for (dx,dy) in [(-1,0), (1,0), (0,-1), (0,1)] if (x+dx, y+dy) in world and (x+dx,y+dy) not in seen} | |
depth += 0.5 | |
depth -= 0.5 | |
print(depth) | |
print( len([(x,y) for (x,y) in world if (x%2,y%2) == (0,0) and distance[x,y] >= 1000])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment