Regex (POSIX ERE / RE2 or better), 13 10 bytes
x(|
|.
.)x
Try it online! - POSIX ERE
Try it on regex101 - RE2
Try it online! - ECMAScript
Try it online! - .NET
The problem statement didn't say anywhere that there specifically had to be \$2\$ rows and \$N\$ columns rather than \$2\$ columns and \$N\$ rows, or that the input can't be taken in transposed form.
So here is a solution that works on virtually any regex engine. Like A username's answer, this matches iff the maze is not solvable. The wall character is x
, and empty tiles can be represented by any other character.
Regex (POSIX ERE / RE2 or better), 33 30 27 bytes
^((''
|^)(('.
)*|(.'
)*))*$
Try it online! - POSIX ERE
Try it on regex101 - RE2
Try it online! - ECMAScript
Try it online! - .NET
This is version that matches solvable mazes and doesn't match unsolvable mazes. It was interesting to implement in a regex flavor that has no negative lookahead. The way it works is pretty much like actually walking through the maze and solving it.
The empty tile character is '
, and walls can be represented by any other character. The input given to this regex must not begin with a newline, and must end with a single newline.
The version of POSIX ERE on TIO treats newlines incorrectly in this version, even though with REG_NEWLINE
not set, newline is supposed to be treated like any normal character. So in the ERE test harness, I made it use !
as a line separator instead of newline. But when tested locally on my machine, it works with actual newlines.
Regex (GNU ERE or better), 29 24 bytes
^((''
|^)(.?'.?
)?\3*)*$
Try it online! - GNU implementation of POSIX ERE, old version
Try it on regex101 - ECMAScript
Try it online! - ECMAScript
Try it online! - .NET
The addition of backreferences allows for a more compact regex. But there doesn't seem to be a clear-cut standard that includes backreferences without lookaheads. The closest appears to be GNU ERE, used by egrep and sed -E
, but there doesn't seem to be any library interface for it, and neither of those utilities support matching raw newlines.
The version of GNU libc on TIO enables backreferences in ERE, but with an up-to-date version of the libraries, they're only enabled in BRE, not ERE. The version on TIO has the newline bug, though, so same workaround for newlines is used in the TIO test harness.
The empty tile character is '
, and walls can be represented by any other character. Matches iff the maze is solvable. The input given to this regex must not begin with a newline, and must end with a single newline.
Regex (ECMAScript or better + /s
flag), 17 bytes
^(?!.*x(|
|.
.)x)
Try it on regex101 - ECMAScript 2018
Try it online! - ECMAScript 2018
Try it online! - .NET
With negative lookahead it is trivial to modify the 10 byte regex into one that matches iff the maze is solvable.
As in the regex it's based on, the wall character is x
, and empty tiles can be represented by any other character.
Note that the /s
flag was introduced in ECMAScript 2018 (JavaScript ES9).
Regex (Perl / Java / PCRE2 / .NET), 29 bytes
^(.(?=.*
(\2.|)))+x.*
\2.?.?x
Try it online! - Perl
Try it on regex101 - PCRE2
Try it online! - .NET
This takes the input in the form of \$2\$ rows of \$N\$ columns. The challenge just wouldn't be complete with that submitted only in .NET flavor and not PCRE as well. Matches iff the maze is not solvable. The wall character is x
, and empty tiles can be represented by any other character.
The .NET balanced group method is still shorter (at 27 bytes), as expected.
This solution takes advantage of the fact that the upper-left corner will never be blocked. It would need to be modified otherwise, because it looks for a wall in the top row that has a wall diagonally or orthogonally below it. It starts this search at the second column from the left.
3xn
or4xn
might be interesting as a separate challenge, which is more complex than2xn
but it is still guaranteed to have a non-backtracking path on any solvable maze. \$\endgroup\$