commit 37c9591e1973959bb87c6c8e5c832f5acb43da13
parent 0ccee0c41a740c754eaa822320dfdeb4c3d4b833
Author: Samir Parikh <noreply@samirparikh.com>
Date: Wed, 26 Oct 2022 12:51:18 +0000
work in progress for part 1 of day07
Diffstat:
2 files changed, 20 insertions(+), 3 deletions(-)
diff --git a/day07/Day07.pm b/day07/Day07.pm
@@ -4,6 +4,7 @@ use strict;
use warnings;
use v5.32;
use Data::Dumper;
+use Scalar::Util qw(looks_like_number);
use Exporter qw( import );
our @EXPORT = qw( evaluate );
@@ -16,11 +17,21 @@ sub evaluate {
#say "my circuit is ", Dumper $circuit;
say "my index is ", $index;
+ # numbers evaluate to themselves
+ return $index if looks_like_number( $index );
+
# return the value if we've already determined it
return $signal{ $index } if ( exists $signal{ $index } );
my $operation = $circuit->{$index};
say "my operation is $operation";
+ die "no operation defined" unless defined $operation;
+
+ # Direct connection, e.g. lx -> a
+ if ($operation =~ /^(\w+)$/) {
+ print "we have a direct connection with '$index = $1'\n";
+ $signal{$index} = evaluate($circuit, $1);
+ }
# specific value
if ( $operation =~ m/\A(\d+)\z/ ) {
@@ -36,17 +47,23 @@ sub evaluate {
}
else {
+ say "coming into the else, we've got operation = $operation";
my ( $left, $operand, $right ) = $operation =~ m/(\w+) (\w+) (\w+)/;
- say "$left, $operand, $right";
- ($left, $right) = (evaluate($circuit, $left), evaluate ($circuit, $left));
+ say "my operation is '$left, $operand, $right'";
+ ($left, $right) = (evaluate($circuit, $left), evaluate ($circuit, $right));
+ say "my operation is now '$left, $operand, $right'";
if ( $operand eq 'AND' ) {
+ say "we've got an AND in $operation";
$signal{ $index } = $left & $right;
} elsif ( $operand eq 'OR' ) {
+ say "we've got an OR in $operation";
$signal{ $index } = $left | $right;
} elsif ( $operand eq 'LSHIFT' ) {
+ say "we've got an LSHIFT in $operation";
$signal{ $index } = $left << $right;
} elsif ( $operand eq 'RSHIFT' ) {
+ say "we've got an RSHIFT in $operation";
$signal{ $index } = $left >> $right;
} else {
die "Could not parse operation.";
diff --git a/day07/day07.pl b/day07/day07.pl
@@ -22,5 +22,5 @@ foreach ( @instructions ) {
#say Dumper \%circuit;
-my $signal = evaluate( \%circuit, 'e' );
+my $signal = evaluate( \%circuit, 'a' );
say "part 1: ", $signal;