Skip to content

Instantly share code, notes, and snippets.

@lynn
Created December 20, 2018 21:23
Show Gist options
  • Save lynn/f5548a6c8c879b248435f8029c94d169 to your computer and use it in GitHub Desktop.
Save lynn/f5548a6c8c879b248435f8029c94d169 to your computer and use it in GitHub Desktop.
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
def furthest(maze):
if maze == []: return 0
if isinstance(maze[0], str): return 1 + furthest(maze[1:])
tines, *rest = maze
if tines[-1] == []: # dead end
return max(max(furthest(t) // 2 for t in tines), furthest(rest))
else:
assert rest == []
return max(furthest(t) for t in tines)
print(furthest(maze))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment