SnakeEx - 98 bytes with Javascript, 44 without
This looked like a good problem to try my language from the Fortnightly ChallengeFortnightly Challenge on:
m:({e<>PE}\-[|\-]*<T>\+|[|\-]*<T>)+`\+ e:\+ The best place to try this out is my online interpreter.
SnakeEx matches patterns in text by using "snakes" that move around the text matching regexes. The code reads kind of like a regex, except:
- The
<T>instruction. That is a direction command that branches the snake left and right from its current direction. {e<>PE}is like a subroutine call. It that spawns a snake with definitionemoving forward (<>) and with parametersP(piggyback - the spawning snake follows the new snake) andE(exclusive - don't match anything that's already been matched). This exclusive check is the only thing that stops the snake from looping infinitely.- The prefix
`at the end indicates that what follows should be matched only if it has already been matched, which we can use to force the loop to close.
Because SnakeEx is like regex and doesn't technically output the results as desired by itself, I guess we need to wrap it in some Javascript calling the interpreter:
function e(s){return snakeEx.run('m:({e<>PE}\\-[|\\-]*<T>\\+|[|\\-]*<T>)+`\\+\ne:\\+',s,1).length} Edit: fixed it up to work with blutorange's additional test cases