Created
August 30, 2020 11:16
-
-
Save TheAthlete/6f0f68eeeeecc6d984fc8bdb50deb45e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
use strict; | |
use warnings; | |
use feature 'say'; | |
use DDP; | |
my @strings = qw/RLRRLLRLRL RLLLLRRRLR LLLLRRRR RLRRRLLRLL LRLLLRRLRR/; | |
for my $s (@strings) { | |
pos $s = 0; | |
my $count = 0; | |
my ($r, $l) = ('', ''); | |
my ($l_count, $r_count) = (0, 0); | |
while (pos $s < length $s) { | |
if ($s =~ /\G(R+)/gc) { | |
if ($r) { | |
$r .= $1; | |
++$r_count; | |
} else { | |
$r = $1; | |
} | |
if ($l) { | |
if (length $l == length $r) { | |
++$count; | |
$r = $l = ''; | |
} | |
} | |
} | |
elsif ($s =~ /\G(L+)/gc) { | |
if ($l) { | |
$l .= $1; | |
++$l_count; | |
} else { | |
$l = $1; | |
} | |
if ($r) { | |
my $l_len = length $l; | |
my $r_len = length $r; | |
if ($r_len == $l_len) { | |
++$count; | |
$r = $l = ''; | |
} elsif (!$l_count && !$r_count && $r_len < $l_len) { | |
pos $s -= $l_len - $r_len; | |
++$count; | |
$r = $l = ''; | |
} | |
} | |
} else { | |
last; | |
} | |
} | |
say sprintf "%s: %d", $s, $count; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment