Created
December 20, 2018 21:23
-
-
Save lynn/f5548a6c8c879b248435f8029c94d169 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 | |
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