Skip to main content
s/Perl 6/Raku/, shorten solution, improve explanation
Source Link
raiph
  • 121
  • 5

Perl 6Raku, 5353 48 bytes

{my ($m,$d);$d;/[\[{$d++;$m=max$d++;$!=max $m$!,$d}|\]{$d--}|.]*/;$m;$!}
> {my ($m,$d);$d;/[\[{$d++;$m=max$d++;$!=max $m$!,$d}|\]{$d--}|.]*/;$m;$!}("[[[3]][2]]")
3
{ my ($m,$d);                 # start closure,outer declareIIFE variablesclosure;

  my $d;            # declare depth variable `$d`;

  /                 # start IIFE regex match;

    [                 # start regex(non-capturing) matchgroup;

   [                   # EITHER:

      \[                # match `[` in input; if successful then:

      {                 # start inner (non-capturingwithin regex) groupIIFE closure;

     \[ {$d++;$m=max $m $d++;             # increment depth variable; and then:

        $! = max $!, $d} |  # match(ab)use [`$!` andvariable to track MAX depth;

      }                 # end inner IIFE closure;

    |                 # OR:

      \]                # match `[` in input; if successful then:

      {$d--}            # decrement depth variable in IIFE closure;

    | # match ] and track depth; OR

     .     # OR:

      .                 # match one charactercharacter;

   ]*   ]*                # end capture group; match zero #or repeatmore grouptimes;

  /;                # end regex;

  $!      # end regex

  $m      # max depth as last value in closure;

}                 # returnend outer closure, returning last value (max depth);
}
("[[[3]][2]]")    # pass string "[[[3]][2]]" as outer closure argument.

Perl 6, 53 bytes

{my ($m,$d);/[\[{$d++;$m=max $m,$d}|\]{$d--}|.]*/;$m}
{my ($m,$d);/[\[{$d++;$m=max $m,$d}|\]{$d--}|.]*/;$m}("[[[3]][2]]")
3
{ my ($m,$d);                 # start closure, declare variables    

  /                           # start regex match

   [                          # start (non-capturing) group

     \[ {$d++;$m=max $m,$d} | # match [ and track depth; OR

     \] {$d--}              | # match ] and track depth; OR

     .                        # match one character

   ]*                         # repeat group

  /;                          # end regex

  $m                          # return max depth
}

Raku, 53 48 bytes

{my $d;/[\[{$d++;$!=max $!,$d}|\]{$d--}|.]*/;$!}
> {my $d;/[\[{$d++;$!=max $!,$d}|\]{$d--}|.]*/;$!}("[[[3]][2]]")
3
{                 # start outer IIFE closure;

  my $d;            # declare depth variable `$d`;

  /                 # start IIFE regex match;

    [                 # start (non-capturing) group;

                      # EITHER:

      \[                # match `[` in input; if successful then:

      {                 # start inner (within regex) IIFE closure;

        $d++;             # increment depth variable; and then:

        $! = max $!, $d   # (ab)use `$!` variable to track MAX depth;

      }                 # end inner IIFE closure;

    |                 # OR:

      \]                # match `[` in input; if successful then:

      {$d--}            # decrement depth variable in IIFE closure;

    |                 # OR:

      .                 # match one character;

    ]*                # end capture group; match zero or more times;

  /;                # end regex;

  $!                # max depth as last value in closure;

}                 # end outer closure, returning last value (max depth);

("[[[3]][2]]")    # pass string "[[[3]][2]]" as outer closure argument.
Commonmark migration
Source Link

#Perl 6, 53 bytes

Perl 6, 53 bytes

The closure:

{my ($m,$d);/[\[{$d++;$m=max $m,$d}|\]{$d--}|.]*/;$m}

Needs an argument, eg:

{my ($m,$d);/[\[{$d++;$m=max $m,$d}|\]{$d--}|.]*/;$m}("[[[3]][2]]")
3

Explanation:

{ my ($m,$d);                 # start closure, declare variables    

  /                           # start regex match

   [                          # start (non-capturing) group

     \[ {$d++;$m=max $m,$d} | # match [ and track depth; OR

     \] {$d--}              | # match ] and track depth; OR

     .                        # match one character

   ]*                         # repeat group

  /;                          # end regex

  $m                          # return max depth
}

#Perl 6, 53 bytes

The closure:

{my ($m,$d);/[\[{$d++;$m=max $m,$d}|\]{$d--}|.]*/;$m}

Needs an argument, eg:

{my ($m,$d);/[\[{$d++;$m=max $m,$d}|\]{$d--}|.]*/;$m}("[[[3]][2]]")
3

Explanation:

{ my ($m,$d);                 # start closure, declare variables    

  /                           # start regex match

   [                          # start (non-capturing) group

     \[ {$d++;$m=max $m,$d} | # match [ and track depth; OR

     \] {$d--}              | # match ] and track depth; OR

     .                        # match one character

   ]*                         # repeat group

  /;                          # end regex

  $m                          # return max depth
}

Perl 6, 53 bytes

The closure:

{my ($m,$d);/[\[{$d++;$m=max $m,$d}|\]{$d--}|.]*/;$m}

Needs an argument, eg:

{my ($m,$d);/[\[{$d++;$m=max $m,$d}|\]{$d--}|.]*/;$m}("[[[3]][2]]")
3

Explanation:

{ my ($m,$d);                 # start closure, declare variables    

  /                           # start regex match

   [                          # start (non-capturing) group

     \[ {$d++;$m=max $m,$d} | # match [ and track depth; OR

     \] {$d--}              | # match ] and track depth; OR

     .                        # match one character

   ]*                         # repeat group

  /;                          # end regex

  $m                          # return max depth
}
Source Link
raiph
  • 121
  • 5

#Perl 6, 53 bytes

The closure:

{my ($m,$d);/[\[{$d++;$m=max $m,$d}|\]{$d--}|.]*/;$m}

Needs an argument, eg:

{my ($m,$d);/[\[{$d++;$m=max $m,$d}|\]{$d--}|.]*/;$m}("[[[3]][2]]")
3

Explanation:

{ my ($m,$d);                 # start closure, declare variables    

  /                           # start regex match

   [                          # start (non-capturing) group

     \[ {$d++;$m=max $m,$d} | # match [ and track depth; OR

     \] {$d--}              | # match ] and track depth; OR

     .                        # match one character

   ]*                         # repeat group

  /;                          # end regex

  $m                          # return max depth
}