Skip to main content
8 of 17
-1 bytes - silly me, don't need a possessive quantifier when nothing would cause it to backtrack anyway
Deadcode
  • 11.9k
  • 1
  • 66
  • 52

Regex (Perl / PCRE), 36 35 bytes

x(x*),((x(?=((?(4)\4)\1)))*)\4?(x*)

Try it online!

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 Turning-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

Java, 41 40 bytes

x(x*),((x(?=(\4\1|(?!\5)\1)()))*)\4?(x*)

Try it online!

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 (.NET), 29 bytes

(x+),(?=(\1)*(x*))((?<-2>x)*)

Try it online!

This uses .NET's Balanced Groups feature. It returns the quotient and remainder in \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
Deadcode
  • 11.9k
  • 1
  • 66
  • 52