commit 80f68eaccb59e2fc70f1f0172f6d247549ff1d41
parent 6851cf3763e5861ab7c39fbc18f9f5a9c30cce9b
Author: Samir Parikh <noreply@samirparikh.com>
Date: Wed, 26 Oct 2022 19:23:49 +0000
cleanup code
Diffstat:
2 files changed, 1 insertion(+), 22 deletions(-)
diff --git a/day07/Day07.pm b/day07/Day07.pm
@@ -3,19 +3,15 @@ package Day07;
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 );
-my %signal = ( samir => 22 );
+my %signal = ();
sub evaluate {
my ( $circuit, $index ) = @_;
- #say "my signal is ", Dumper \%signal;
- #say "my circuit is ", Dumper $circuit;
- say "my index is ", $index;
# numbers evaluate to themselves
return $index if looks_like_number( $index );
@@ -24,46 +20,33 @@ sub evaluate {
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
elsif ( $operation =~ m/\A(\d+)\z/ ) {
- say "kick it";
$signal{ $index } = $1;
- #return $signal{ $index };
}
# NOT
elsif ( $operation =~ m/NOT (\w+)/ ) {
- say "we got a NOT";
$signal{ $index } = ~ evaluate( $circuit, $1 );
}
else {
- say "coming into the else, we've got operation = $operation";
my ( $left, $operand, $right ) = $operation =~ m/(\w+) (\w+) (\w+)/;
- 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. Evaluating $left and $right.";
$signal{ $index } = $left & $right;
} elsif ( $operand eq 'OR' ) {
- say "we've got an OR in $operation. Evaluating $left and $right.";
$signal{ $index } = $left | $right;
} elsif ( $operand eq 'LSHIFT' ) {
- say "we've got an LSHIFT in $operation. Evaluating $left and $right.";
$signal{ $index } = $left << $right;
} elsif ( $operand eq 'RSHIFT' ) {
- say "we've got an RSHIFT in $operation. Evaluating $left and $right.";
$signal{ $index } = $left >> $right;
} else {
die "Could not parse operation.";
diff --git a/day07/day07.pl b/day07/day07.pl
@@ -6,13 +6,11 @@ use warnings;
use v5.32;
use lib '.';
use Day07 qw< evaluate >;
-use Data::Dumper;
@ARGV = "input" unless @ARGV;
chomp( my $input = do { local $/; <> } );
my %circuit;
-#my %signal = ( samir => 22 );
my @instructions = split /\n/, $input;
foreach ( @instructions ) {
@@ -20,7 +18,5 @@ foreach ( @instructions ) {
$circuit{ $2 } = $1;
}
-#say Dumper \%circuit;
-
my $signal = evaluate( \%circuit, 'a' );
say "part 1: ", $signal;