Regex (Perl / PCRE), 36 35 bytes
x(x*),((x(?=((?(4)\4)\1)))*)\4?(x*)
Takes its arguments in unary, as two strings of x
characters whose lengths represent the numbers. The divisor comes first, followed by a ,
delimiter, followed by the dividend. The quotient and remainder are returned in the capture groups \2
and \5
, respectively.
In contrast to the ECMAScript regex solution, this one doesn't have to do anything anywhere near as fancy or mathematically interesting. Just count the number of times \$divisor\$ fits into \$dividend\$ by splitting the divisor to keep two tandem running totals that are both subtracted from \$dividend\$, one that keeps subtracting \$divisor-1\$, and one that keeps subtracting \$1\$ and adding it to the total quotient. We must do a split like this, because regex refuses to repeat a zero-width group more than once (this, along with the limited space to work in, is exactly what prevents it from being Turing-complete).
I never wrote a division algorithm in any regex flavor besides ECMAScript before. So it's interesting to now know how they compare in golfed size.
x(x*), # \1 = divisor-1; tail = dividend
( # \2 = what will be the quotient
(
x # tail -= 1
(?=
( # \4 = running total
(?(4)\4) # recall the previous contents of \4, if any
\1 # \4 += divisor-1
)
)
)* # Loop the above as many times as possible (zero or more); if
# it loops zero times, \4 will be unset (we'll treat that as 0)
)
\4? # tail -= \4, or leave tail unchanged if \4 is unset
(x*) # \5 = remainder
Regex (Java), 41 40 bytes
x(x*),((x(?=(\4\1|(?!\5)\1)()))*)\4?(x*)
This is a port of the Perl/PCRE regex to a flavor that has no conditionals. Emulating a conditional costs 5 bytes here. The quotient and remainder are returned in the capture groups \2
and \6
, respectively.
x(x*), # \1 = divisor-1; tail = dividend
( # \2 = what will be the quotient
(
x # tail -= 1
(?=
( # \4 = running total
\4 # recall the previous contents of \4 (only if it is set)
\1 # \4 += divisor-1
|
(?!\5) # match this alternative only if \4 is unset
\1 # \4 = divisor-1
)
() # \5 = set to indicate that \4 is set
)
)* # Loop the above as many times as possible (zero or more); if
# it loops zero times, \4 will be unset (we'll treat that as 0)
)
\4? # tail -= \4, or leave tail unchanged if \4 is unset
(x*) # \6 = remainder
Regex (Pythonregex
/ Ruby), 43 bytes
x(x*),((x(?=((?(5)\5)\1))(?=(\4)))*)\4?(x*)
Try it online! - Python import regex
Try it online! - Ruby
This is a port of the Perl/PCRE regex to flavors that have no support for nested backreferences. Python's built-in re
module does not even support forward backreferences, so for Python this requires regex
.
Emulating a nested backreference by copying the group back and forth costs 8 bytes here. The quotient and remainder are returned in the capture groups \2
and \6
, respectively.
x(x*), # \1 = divisor-1; tail = dividend
( # \2 = what will be the quotient
(
x # tail -= 1
(?=
( # \4 = running total
(?(5)\5) # recall the previous contents of \4 (as copied into \5) if any
\1 # \4 += divisor-1
)
)
(?=(\4)) # \5 = \4, to make up for Python's lack of nested backreferences
)* # Loop the above as many times as possible (zero or more); if
# it loops zero times, \4 will be unset (we'll treat that as 0)
)
\4? # tail -= \4, or leave tail unchanged if \4 is unset
(x*) # \6 = remainder
Regex (.NET), 29 bytes
(x+),(?=(\1)*(x*))((?<-2>x)*)
This uses .NET's Balanced Groups feature. It returns the quotient and remainder in the lengths of \4
and \3
, respectively.
(x+), # \1 = divisor; assert \1 > 0; tail = dividend
(?=
(\1)* # push \2 onto the stack for each time \1 fits into dividend
(x*) # \3 = remainder
)
((?<-2>x)*) # \4 = quotient: pop all \2 from stack, doing \4 += 1 for each
Regex (.NET), 14 bytes
(x+),(\1)*(x*)
This uses .NET's Balanced Groups feature. It returns the quotient in the capture count of \2
, and remainder in the length of \3
.