AoC day 5 in Perl. I'm not quite happy with the second loop. It feels a bit ad-hoc, like maybe there is a simpler algorithm waiting to be revealed. But it's not too bad, and it works:
use builtin qw(true);
my %after;
while (readline) {
chomp;
last if $_ eq '';
my ($fst, $snd) = /^ (\d+) \| (\d+) \z/xa
or die "Malformed input: $_";
push @{$after{$fst}}, $snd;
}
my $good_total = 0;
my $bad_total = 0;
while (readline) {
chomp;
my @pgs = split /,/;
my $ok = true;
my (%seen, %before);
for my $pg (@pgs) {
$seen{$pg} = true;
for my $x (@{$after{$pg} // []}) {
push @{$before{$x}}, $pg;
$ok &&= !$seen{$x};
}
}
my ($mid) = grep { @{$before{$_} // []} == $#pgs / 2 } @pgs;
${$ok ? \$good_total : \$bad_total} += $mid;
}
say $good_total;
say $bad_total;
#AdventOfCode #perl
=> More informations about this toot | More toots from barubary@infosec.exchange
@barubary
I see, you even checked for bad input. Great!
For part 2, I used the built in "sort" function. Lazy, but worked fine. (I posted my Perl solution)
=> More informations about this toot | More toots from mina@berlin.social
@mina @barubary Also, feel free to join my Perl-oriented private leaderboard 430385-e28eb36e.
=> More informations about this toot | More toots from choroba@mastodon.social
@choroba
Thanks for the invitation, I joined (last place, for now).
@barubary
=> More informations about this toot | More toots from mina@berlin.social This content has been proxied by September (3851b).Proxy Information
text/gemini