day01.pl (744B) - raw
1 #!/usr/local/bin/perl 2 # day 2022-01 3 4 use strict; 5 use warnings; 6 use v5.32; 7 use List::Util qw( sum ); 8 9 @ARGV = "input" unless @ARGV; 10 chomp( my $input = do { local $/; <> } ); 11 12 my @items = split /\n/, $input; 13 my $max = 0; 14 my $current = 0; 15 my @top_3 = ( 0, 0, 0 ); 16 17 foreach my $item ( @items ) { 18 unless ( $item ) { 19 $max = $current if ( $current > $max ); 20 if ( $current > $top_3[0] ) { 21 shift @top_3; 22 push @top_3 => $current; 23 @top_3 = sort { $a <=> $b } @top_3; 24 } 25 $current = 0; 26 } else { 27 $current += $item; 28 } 29 } 30 31 # check last elf 32 if ( $current > $top_3[0] ) { 33 shift @top_3; 34 push @top_3 => $current; 35 } 36 37 say "part 1: ", $max; 38 say "part 2: ", sum @top_3;