aoc2022

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

day10.pl (568B) - raw


      1 #!/usr/local/bin/perl
      2 # day 2022-10
      3 
      4 use strict;
      5 use warnings;
      6 use v5.32;
      7 
      8 @ARGV = "input" unless @ARGV;
      9 chomp( my $input = do { local $/; <> } );
     10 
     11 use List::Util qw( sum );
     12 
     13 my @x;
     14 my $cycle = 0;
     15 $x[ $cycle ] = 1;
     16 
     17 foreach ( split /\n/, $input ) {
     18     my ( $instruction, $value ) = m/(noop|addx)\s?(-?\d+)?/;
     19     $cycle++;
     20     $x[ $cycle ] = $x[ $cycle - 1 ];
     21     if ( $instruction eq 'addx' ) {
     22         $cycle++;
     23         $x[ $cycle ] = $x[ $cycle -1 ] + $value;
     24     }
     25 }
     26 
     27 my @cycles = (20,60,100,140,180,220);
     28 say "part 1: ", sum( map{ $_ * $x[ $_ - 1 ] } @cycles );