Regex 🐇
(ECMAScript+(?^*)
RME), 13 bytes
^(x(?^*x+))*$
Takes its input in unary, as a string of x
characters whose length represents the number. Returns its output as the number of ways the regex can match. (The rabbit emoji indicates this output method. It can yield outputs bigger than the input, and is really good at multiplying.)
Try it on replit.com
This version uses molecular lookinto, a new feature in RegexMathEngine. The basic form (?^*
...)
matches the expression inside it against the entire input string. (With a parameter, it evaluates it against the contents of a backreference, e.g. (?^5*
...)
to look into \5
.)
On every iteration of the outer loop, there are \$n\$ possible matching states of the inner loop, and at the end of \$n\$ iterations, the match is complete. In a complete match, each iteration can independently be at any one of \$n\$ states, so there are \$n^n\$ total possible matches.
Changing the *
at the end to +
would make it give a result of \$0\$ for \$n=0\$.
^ # tail = N = input number
(
x # tail -= 1
(?^* # Non-atomic lookinto - starts with tail = N
x+ # Create a choice between N possibilities
)
)*$ # Loop as many times as possible, minimum 0. This will loop N times,
# because it subtracts 1 from tail on each iteration. The minimum of
# 0 iterations gives a correct result of 1 for N==0.
The constant-power \$n^k\$ version of this can be seen in Quartic Summation, for which there is also a method that doesn't require non-atomic lookaround. It might be impossible to implement \$n^n\$ without non-atomic lookaround, due to it being impossible to create \$m\$ number of choices (with the value \$m\$ being accessed via a backreference) without the guarantee of having \$O(m)\$ avaiable space.
The constant-base \$k^n\$ version of this would be e.g. ^((.+)*)+
or ^(x(||))*$
for \$3^n\$.
Regex 🐇
(PCRE2 v10.35+), 26 bytes
^(x((?<*(?*^x+|(?2)).)))*$
Attempt This Online!
This solution uses non-atomic recursion, lookahead, and lookbehind to emulate non-atomic variable-length lookbehind. The algorithm used is the same as that of the 13 byte version.
^ # tail = N = input number
(
x # tail -= 1
( # Define subroutine (?2)
(?<* # Non-atomic lookbehind
(?* # Non-atomic lookahead
^ # If we've reached tail==0, then:
x+ # create a choice between N possibilities.
|
(?2) # Otherwise, recursively call (?2).
)
. # tail += 1 (Go back 1 character at a time)
)
)
)*$ # Loop as many times as possible, minimum 0. This
# will loop N times, because it subtracts 1 from
# tail on each iteration. The minimum of 0 iterations
# gives a correct result of 1 for N==0.
The equivalent in a hypothetical engine with real (not emulated) non-atomic variable-length lookbehind would be ^(x(?<*^x+.*))*$
(16 bytes).
Regex 🐇
(PCRE2 v10.35+ without lookbehind), 59 bytes
^(?=(x*)\1(x?))(((?*x+(?!\1)()?|\2+$))x(?(?=\2\1)(?4)))*\1$
Attempt This Online!
This demonstrates this to be possible without the use of lookbehind or recursion. It works by emulating operation on \$n\$ within the space of \$n/2\$, using one of those halves as a counter for the outer loop, and the other half for the inner loop.
(I wrote this version on July 29, and am finally posting it now.)
^ # Anchor to start; tail = N = input number
(?=(x*)\1(x?)) # \1 = floor(N / 2); \2 = N % 2
(
( # Define subroutine (?4):
(?*
x+(?!\1) # Add \1 to possibility count
()? # Double the possibility count of the above
|
\2+$ # If \2==1, add 1 to possibility count
)
)
x # tail -= 1
(?(?=\2\1) # If tail ≥ \2 + \1:
(?4) # Call subroutine (?4), to multiply its possibility
# count with the above
)
)* # For every possible iteration count, from 0 to the
# maximum, add the above to the possibility count
\1$ # Assert tail == \1
Regex 🐇
(ECMAScript+(?*)
RME / PCRE2 v10.35+), 81 bytes
^(?=(x*)\1(x?))((?*x+(?!\1)(|)|\2+$)x((?=\2\1)(?*x+(?!\1)(|)|\2+$)|(?!\2\1)))*\1$
Try it on replit.com - RegexMathEngine
Attempt This Online! - PCRE2 v10.40+
This is a port of the 59 byte version. Basic ECMAScript lacks not only lookbehind, but subroutine calls and conditionals as well. The only feature added to basic ECMAScript is (?*
...)
, molecular lookahead (a.k.a. non-atomic lookahead). Note that (|)
must be used instead of ()?
due to ECMAScript's no-empty-optional rule.
(I wrote this version on July 29, and am finally posting it now.)
^ # Anchor to start; tail = N = input number
(?=(x*)\1(x?)) # \1 = floor(N / 2); \2 = N % 2
(
(?* # Define "subroutine":
x+(?!\1) # Add \1 to possibility count
(|) # Double the possibility count of the above
|
\2+$ # If \2==1, add 1 to possibility count
)
x # tail -= 1
# Multiply the above possibility count with the following:
( # Emulated conditional, upon whether tail ≥ \2 + \1
(?=\2\1) # If yes:
# Call "subroutine" (duplicates it, since this regex flavor lacks
# subroutine calls) to multiply with the above possibility count
(?*
x+(?!\1)
(|)
|
\2+$
)
|
(?!\2\1) # If no: Use possibility multiplier of 1
)
)* # For every possible iteration count, from 0 to the
# maximum, add the above to the possibility count
\1$ # Assert tail == \1
0
and that the expected output be specified (0
or1
or either). Finally, having to handle negative integers would be a nice addition to the challenge. \$\endgroup\$1
for0^0
. However,Foundation
+ Swift returns 0 \$\endgroup\$0
and instead specified that0<x
in the lead-in. I also removed the restriction that code shouldn't throw errors; that should go without saying. Feel free to roll back if necessary. \$\endgroup\$