aoc2022

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

commit 22b3dbc918368c5c876a085e659e7fe82291d965
parent 721ca474b7158d68bc0b45f2047dced7869593f8
Author: Samir Parikh <noreply@samirparikh.com>
Date:   Sun,  1 Jan 2023 19:32:28 +0000

solve part 1 of day11

Diffstat:
Mday11/day11.pl | 35++++++++++++++++++++++-------------
1 file changed, 22 insertions(+), 13 deletions(-)

diff --git a/day11/day11.pl b/day11/day11.pl @@ -4,14 +4,14 @@ use strict; use warnings; use v5.32; -#use Data::Dumper; -#$Data::Dumper::Indent = 1; + +use List::Util qw( product ); + use Log::Log4perl (); use Log::Log4perl::Level (); - Log::Log4perl->easy_init( - Log::Log4perl::Level::to_priority( 'DEBUG' ) -); # set to 'DEBUG' or 'OFF' + Log::Log4perl::Level::to_priority( 'OFF' ) +); # set to 'OFF', 'INFO', 'DEBUG' or 'TRACE' for successively more information my $logger = Log::Log4perl->get_logger(); @ARGV = "input" unless @ARGV; @@ -58,34 +58,36 @@ sub init_monkeys { } my @monkeys1 = init_monkeys( @input ); +my %monkeys1_count; foreach my $round ( 1 .. $ROUNDS ) { my $num = 0; foreach my $monkey ( @monkeys1 ) { - $logger->debug( "Monkey $num:" ); + $logger->trace( "Monkey $num:" ); while ( @{ $monkey->{ $ITEMS } } ) { my $item = shift @{ $monkey->{ $ITEMS } }; - $logger->debug( " Monkey inspects an item with a worry level of $item." ); + $logger->trace( " Monkey inspects an item with a worry level of $item." ); + $monkeys1_count{ $num }++; my $expression = join " ", $item, $monkey->{ $OPERATION }{ operation }, $monkey->{ $OPERATION }{ operand } eq 'old' ? $item : $monkey->{ $OPERATION }{ operand }; $item = eval $expression; - $logger->debug( " Worry level is now $item." ); + $logger->trace( " Worry level is now $item." ); $item = int( $item / 3 ); - $logger->debug(" Monkey gets bored with item. ", + $logger->trace(" Monkey gets bored with item. ", "Worry level is divided by 3 to $item."); if ( $item % $monkey->{ $TEST } == 0 ) { # true - $logger->debug( " Current worry level is divisible by ", + $logger->trace( " Current worry level is divisible by ", "$monkey->{ $TEST }." ); - $logger->debug( " Item with worry level $item is thrown to monkey ", + $logger->trace( " Item with worry level $item is thrown to monkey ", "$monkey->{ $TRUE }." ); push @{ $monkeys1[ $monkey->{ $TRUE } ]->{ $ITEMS } } => $item; } else { # false - $logger->debug( " Current worry level is not divisible by ", + $logger->trace( " Current worry level is not divisible by ", "$monkey->{ $TEST }." ); - $logger->debug( " Item with worry level $item is thrown to monkey ", + $logger->trace( " Item with worry level $item is thrown to monkey ", "$monkey->{ $FALSE }." ); push @{ $monkeys1[ $monkey->{ $FALSE } ]->{ $ITEMS } } => $item; } @@ -100,3 +102,10 @@ foreach my $round ( 1 .. $ROUNDS ) { ( join ", ", @{ $monkeys1[ $i ]->{ $ITEMS } } ) ); } } + +foreach my $monkey ( sort keys %monkeys1_count ) { + $logger->info( "Monkey $monkey inspected items $monkeys1_count{ $monkey } times." ); +} + +my @inspections = reverse( sort { $a <=> $b } ( values %monkeys1_count ) ); +say "part 1: ", product @inspections[ 0 .. 1 ];