Skip to content

Instantly share code, notes, and snippets.

@jadiunr
Created Aug 8, 2019
Embed
What would you like to do?
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