Created
December 20, 2018 21:39
-
-
Save lynn/7a54e3cd0256f0e89d837ac4185e6539 to your computer and use it in GitHub Desktop.
Why doesn't this work ;-;
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) | |
def parse(regex): | |
stack = [[[]]] | |
for c in regex.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 | |
return maze | |
def furthest(maze): | |
if maze == []: return 0 | |
if isinstance(maze[0], str): return 1 + furthest(maze[1:]) | |
tines, *remainder = maze | |
if tines[-1] == []: # dead end | |
assert len(tines) == 2 | |
return max(furthest(tines[0]) // 2, furthest(remainder)) | |
else: | |
assert remainder == [] | |
return max(furthest(t) for t in tines) | |
while 1: print(furthest(parse(input()))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment