Skip to content

Instantly share code, notes, and snippets.

@jadiunr
Created August 8, 2019 09:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jadiunr/a7181897fe2f0abddf572d60ca164e41 to your computer and use it in GitHub Desktop.
Save jadiunr/a7181897fe2f0abddf572d60ca164e41 to your computer and use it in GitHub Desktop.
BLYAT
use strict;
use warnings;
use feature 'say';
use Data::Dumper;
my $S = <>; chomp($S);
my $out = [];
for my $S_group ($S =~ /(R+L+)/g) {
my $r_children = 0;
my $l_children = 0;
my $r_count = () = $S_group =~ /R/g;
my $l_count = () = $S_group =~ /L/g;
if ($r_count % 2 == 0) {
$r_children += $r_count / 2;
$l_children += $r_count / 2;
} else {
$r_children += $r_count - int($r_count / 2);
$l_children += int($r_count / 2);
}
if ($l_count % 2 == 0) {
$r_children += $l_count / 2;
$l_children += $l_count / 2;
} else {
$r_children += int($l_count / 2);
$l_children += $l_count - int($l_count / 2);
}
for (1..$r_count) {
if ($_ == $r_count) {
push(@$out, $r_children);
} else {
push(@$out, 0);
}
}
for (1..$l_count) {
if ($_ == 1) {
push(@$out, $l_children);
} else {
push(@$out, 0);
}
}
}
say "@$out";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment