aoc2022

Advent of Code 2022 solutions in Perl.
git clone git://git.samirparikh.com/aoc2022
Log | Files | Refs | README

commit 3a8cebbbee04e96852d64f926a17fc437d525f99
parent 91457ebd8a6248533fadf2f611b699f4296f79df
Author: Samir Parikh <noreply@samirparikh.com>
Date:   Tue, 20 Dec 2022 22:39:42 +0000

solve part 2 of day 01

Diffstat:
Mday01/day01.pl | 14++++++++++++++
1 file changed, 14 insertions(+), 0 deletions(-)

diff --git a/day01/day01.pl b/day01/day01.pl @@ -4,6 +4,7 @@ use strict; use warnings; use v5.32; +use List::Util qw( sum ); @ARGV = "input" unless @ARGV; chomp( my $input = do { local $/; <> } ); @@ -11,14 +12,27 @@ chomp( my $input = do { local $/; <> } ); my @items = split /\n/, $input; my $max = 0; my $current = 0; +my @top_3 = ( 0, 0, 0 ); foreach my $item ( @items ) { unless ( $item ) { $max = $current if ( $current > $max ); + if ( $current > $top_3[0] ) { + shift @top_3; + push @top_3 => $current; + @top_3 = sort { $a <=> $b } @top_3; + } $current = 0; } else { $current += $item; } } +# check last elf +if ( $current > $top_3[0] ) { + shift @top_3; + push @top_3 => $current; +} + say "part 1: ", $max; +say "part 2: ", sum @top_3;