From: Jan "Yenya" Kasprzak Date: Mon, 5 Dec 2022 18:39:01 +0000 (+0100) Subject: Day 5: more Perl-ish input parsing X-Git-Url: https://www.fi.muni.cz/~kas/git//home/kas/public_html/git/?p=aoc.git;a=commitdiff_plain;h=0a8fb8f0349879b7647ee36b699047c2da17a3a6 Day 5: more Perl-ish input parsing --- diff --git a/2022/09.pl b/2022/09.pl index ef96b52..ecc3728 100755 --- a/2022/09.pl +++ b/2022/09.pl @@ -8,17 +8,15 @@ my @stacks; while (<>) { last if /^ \d/; - my $i = 1; - my $st = 0; - $stacks[$st] //= []; - while ($i < length) { - my $c = substr($_, $i, 1); - unshift @{ $stacks[$st] }, $c if $c =~ /[A-Z]/; - $i += 4; - $st++; + my $i = 0; + for (/.(.). ?/g) { + unshift @{ $stacks[$i] }, $_ if /[A-Z]/; + $i++; } } +use Data::Dumper; +print Dumper \@stacks; scalar <>; while (<>) { diff --git a/2022/10.pl b/2022/10.pl index 913e078..0dcf15f 100755 --- a/2022/10.pl +++ b/2022/10.pl @@ -8,14 +8,10 @@ my @stacks; while (<>) { last if /^ \d/; - my $i = 1; - my $st = 0; - $stacks[$st] //= []; - while ($i < length) { - my $c = substr($_, $i, 1); - unshift @{ $stacks[$st] }, $c if $c =~ /[A-Z]/; - $i += 4; - $st++; + my $i = 0; + for (/.(.). ?/g) { + unshift @{ $stacks[$i] }, $_ if /[A-Z]/; + $i++; } }