This commit is contained in:
Yanick Champoux 2017-12-10 13:41:54 -05:00
parent 78d3e14c07
commit 2add78c252
2 changed files with 44 additions and 0 deletions

43
09/1.pl Normal file
View File

@ -0,0 +1,43 @@
use 5.20.0;
my $stream = <>;
chomp $stream;
$stream = clean_stream($stream);
use Text::Balanced qw/ extract_bracketed /;
use List::AllUtils qw/ sum /;
use experimental qw/signatures /;
say score( $stream );
sub score($stream,$level=1) {
$stream =~ s/.*?\{(.*)\}.*?/$1/;
my @children;
my $extracted;
while( length $stream ) {
( $extracted, $stream ) = extract_bracketed( $stream, '{}', qr/,?/ );
push @children, $extracted if length $extracted;
$stream =~ s/^,|,$//;
}
return sum $level, map { score($_,$level+1) } @children;
}
sub clean_stream {
my $s = shift;
return $s unless $s =~ s/^(.*?)<//;
my $clean = $1;
while( $s =~ s/^.*?(!!|!>|>)// ) {
last if $1 eq '>';
}
return $clean . clean_stream($s);
}

1
09/input.txt Normal file

File diff suppressed because one or more lines are too long