Ancestors

Toot

Written by Füsilier Breitlinger on 2024-12-05 at 05:56

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

Descendants

Written by Mina No No No on 2024-12-05 at 10:48

@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

Written by E. Choroba on 2024-12-05 at 15:44

@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

Written by Mina No No No on 2024-12-05 at 16:07

@choroba

Thanks for the invitation, I joined (last place, for now).

@barubary

=> More informations about this toot | More toots from mina@berlin.social

Proxy Information
Original URL
gemini://mastogem.picasoft.net/thread/113598671970457886
Status Code
Success (20)
Meta
text/gemini
Capsule Response Time
274.754328 milliseconds
Gemini-to-HTML Time
1.00526 milliseconds

This content has been proxied by September (3851b).