commit 721ca474b7158d68bc0b45f2047dced7869593f8
parent 170a104bdcd9131bc9ca1778b308257ea1be3f7e
Author: Samir Parikh <noreply@samirparikh.com>
Date: Sun, 1 Jan 2023 17:37:42 +0000
work in progress
Diffstat:
M | day11/day11.pl | | | 60 | ++++++++++++++++++++++++++++++------------------------------ |
1 file changed, 30 insertions(+), 30 deletions(-)
diff --git a/day11/day11.pl b/day11/day11.pl
@@ -9,7 +9,9 @@ use v5.32;
use Log::Log4perl ();
use Log::Log4perl::Level ();
-Log::Log4perl->easy_init(Log::Log4perl::Level::to_priority( 'DEBUG' ));
+Log::Log4perl->easy_init(
+ Log::Log4perl::Level::to_priority( 'DEBUG' )
+); # set to 'DEBUG' or 'OFF'
my $logger = Log::Log4perl->get_logger();
@ARGV = "input" unless @ARGV;
@@ -21,7 +23,7 @@ my $OPERATION = "Operation";
my $TEST = "Test";
my $TRUE = "true";
my $FALSE = "false";
-my $ROUNDS = 1;
+my $ROUNDS = 20;
sub init_monkeys {
my @input = @_;
@@ -56,47 +58,45 @@ sub init_monkeys {
}
my @monkeys1 = init_monkeys( @input );
-#foreach my $monkey ( @monkeys1 ) {
-# say "@{ $monkey->{$ITEMS} }";
-#}
foreach my $round ( 1 .. $ROUNDS ) {
- my $num = 0; # extra
+ my $num = 0;
foreach my $monkey ( @monkeys1 ) {
$logger->debug( "Monkey $num:" );
while ( @{ $monkey->{ $ITEMS } } ) {
my $item = shift @{ $monkey->{ $ITEMS } };
$logger->debug( " Monkey inspects an item with a worry level of $item." );
-# say "operation is ", $monkey->{ $OPERATION }{ operation };
-# say "operand is ", $monkey->{ $OPERATION }{ operand };
my $expression = join " ",
$item,
$monkey->{ $OPERATION }{ operation },
- $monkey->{ $OPERATION }{ operand } eq 'old' ? $item :
- $monkey->{ $OPERATION }{ operand };
- #say $expression;
- #my $result = eval $expression;
- #say "$expression = $result";
+ $monkey->{ $OPERATION }{ operand } eq 'old' ? $item :
+ $monkey->{ $OPERATION }{ operand };
$item = eval $expression;
$logger->debug( " Worry level is now $item." );
$item = int( $item / 3 );
- $logger->debug(" Monkey gets bored with item. Worry level is divided by 3 to $item.");
+ $logger->debug(" 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 ",
+ "$monkey->{ $TEST }." );
+ $logger->debug( " 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 ",
+ "$monkey->{ $TEST }." );
+ $logger->debug( " Item with worry level $item is thrown to monkey ",
+ "$monkey->{ $FALSE }." );
+ push @{ $monkeys1[ $monkey->{ $FALSE } ]->{ $ITEMS } } => $item;
+ }
}
- $num++; # extra
+ $num++;
+ }
+
+ $logger->debug( "After round $round, the monkeys are holding items with these ",
+ "worry levels:" );
+ foreach my $i ( 0 .. $#monkeys1 ) {
+ $logger->debug( "Monkey $i: ",
+ ( join ", ", @{ $monkeys1[ $i ]->{ $ITEMS } } ) );
}
}
-#say Dumper \@monkeys1;
-#
-#
-#my $operation = '+';
-#my $arg1 = 2;
-#my $arg2 = 5;
-#
-#my $expression = join " ", $arg1, $operation, $arg2;
-##my $expression = '$arg1 $operation $arg2';
-#say $expression;
-#
-#my $result = eval $expression;
-#say $result;
-#
-#say int (10/3);