commit a6fe4e87369bbf126742b5e2af65e4de02d6e856
parent 5465ac915815f35941c8e54d017aa587e109f251
Author: Samir Parikh <noreply@samirparikh.com>
Date: Fri, 30 Dec 2022 20:18:58 +0000
solve part 1 of day10
Diffstat:
1 file changed, 19 insertions(+), 6 deletions(-)
diff --git a/day10/day10.pl b/day10/day10.pl
@@ -8,25 +8,38 @@ use v5.32;
@ARGV = "input" unless @ARGV;
chomp( my $input = do { local $/; <> } );
+use List::Util qw( sum );
+
my @x;
my $cycle = 0;
$x[ $cycle ] = 1;
-my $ADDX_CYCLE = 2;
-my $NOOP_CYCLE = 1;
+#my $ADDX_CYCLE = 2;
+#my $NOOP_CYCLE = 1;
foreach ( split /\n/, $input ) {
#say $_;
my ( $instruction, $value ) = m/(noop|addx)\s?(-?\d+)?/;
- print "$instruction ";
- print $value if $value;
- say '';
+# print "$instruction ";
+# print $value if $value;
+# say '';
$cycle++;
if ( $instruction eq 'noop' ) {
$x[ $cycle ] = $x[ $cycle - 1 ];
} else {
$x[ $cycle ] = $x[ $cycle - 1 ];
+ #say "after cycle $cycle, X is $x[$cycle]";
$cycle++;
$x[ $cycle ] = $x[ $cycle -1 ] + $value;
}
- say "after cycle $cycle, X is $x[$cycle]";
+ #say "after cycle $cycle, X is $x[$cycle]";
}
+
+my @cycles = (20,60,100,140,180,220);
+#my $sum=0;
+#foreach (@cycles) {
+# #say $_, " ", $x[$_-1];
+# $sum+= $_ * $x[$_-1];
+#}
+
+#say "part 1: ", $sum;
+say "part 1: ", sum( map{ $_ * $x[ $_ - 1 ] } @cycles );