Advertisement
musifter

AoC 2022, day 10 (perl)

Dec 10th, 2022
1,583
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 0.56 KB | Source Code | 0 0
  1. #!/usr/bin/perl
  2.  
  3. use v5.12;
  4. use warnings;
  5.  
  6. my $regX  = 1;
  7. my @event = (0);
  8. my $time  = 0;
  9.  
  10. my $part1 = 0;
  11. my $display;
  12.  
  13. my @input = <>;
  14.  
  15. while ($_ = shift @input) {
  16.     if (m#^addx (.*)#) {
  17.         push( @event, $1 );
  18.         unshift( @input, "noop" );
  19.     } else { # noop
  20.         push( @event, 0 );
  21.     }
  22.  
  23.     $display .= (abs($regX - $time % 40) <= 1) ? '#' : ' ';
  24.  
  25.     $part1 += $time * $regX  if (++$time % 40 == 20);
  26.     $regX  += shift @event;
  27. }
  28.  
  29. say "Part 1: $part1";
  30.  
  31. say "Part 2:";
  32. say substr($display, 0, 40, '')  while( $display );
  33.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement