From 0a8fb8f0349879b7647ee36b699047c2da17a3a6 Mon Sep 17 00:00:00 2001 From: "Jan \"Yenya\" Kasprzak" Date: Mon, 5 Dec 2022 19:39:01 +0100 Subject: [PATCH] Day 5: more Perl-ish input parsing --- 2022/09.pl | 14 ++++++-------- 2022/10.pl | 12 ++++-------- 2 files changed, 10 insertions(+), 16 deletions(-) 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++; } } -- 2.43.0