Regex 🐇
(ECMAScriptRME / Perl / PCRE / Raku:P5
), 14 bytes
^(xxx?|x{5})*$
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.)
Try it on replit.com (RegexMathEngine)
Try it online! - Perl
Try it online! - PCRE1
Try it online! - PCRE2
Try it online! - Raku
Same method as used for Fibonacci function or sequence, directly enumerating the ordered partitions.
^ # tail = input number
(
xxx? # tail-=2 or tail-=3
| # or
x{5} # tail-=5
)* # Loop the above as many times as possible, minimum zero
$ # Assert tail == 0
Regex 🐇
(Raku), 14 bytes
^(xxx?|x**5)*$
Try it online!
Raku's plain regex alternation operator is ||
, and |
does something special – it chooses the alternative making the longest match. But it'll still try every alternative when backtracking, so this makes no difference in the number of matches.
Perl, 38 bytes (full program)
1x<>~~/^(...?|.{5})*$(??{++$i})/;say$i
Try it online!
Interestingly, I noticed after making this post that Ton Hospel already used the same basic method (and it predates my Perl Fibonacci answer by 3 years), but did it with Perl -p
in 37 bytes (which can be reduced to 34 bytes), by exploiting Perl actually using literal {
}
to create the -p
loop. I previously assumed Perl emulated a loop when launched with -p
, not that it literally string-concatenated the program into a loop.
Using the $\
trick (setting the print
record separator), there's an alternative 38 byte flagless full program:
1x<>~~/^(...?|.{5})*$(??{++$\})/;print
Try it online!
Sadly it's only literally a print
record separator and doesn't apply to say
.
Raku, 27 bytesSBCS (anonymous function)
{+m:ex/^(...?|.**5)*$/}o¹x*
Try it online!
sum
in them was not a good idea to try to solve that inquiry... \$\endgroup\$11
from12
to16
. Of course feel free to fix this if I misunderstood your intention \$\endgroup\$