Advertisement
musifter

AoC day 2, Perl

Dec 2nd, 2021
1,767
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 0.44 KB | None | 0 0
  1. #!/usr/bin/perl
  2.  
  3. use strict;
  4. use warnings;
  5.  
  6. my $horz  = 0;
  7. my $aim   = 0;
  8. my $depth = 0;
  9.  
  10. while (<>) {
  11.     my ($cmd, $mag) = split;
  12.  
  13.     if ($cmd eq 'forward') {
  14.         $horz  += $mag;
  15.         $depth += $mag * $aim;
  16.     } else {
  17.         # Evil! level just happens to be a word between up and down
  18.         $aim += ('level' cmp $cmd) * $mag;
  19.     }
  20. }
  21.  
  22. print "Part 1: ", $horz * $aim,   "\n";
  23. print "Part 2: ", $horz * $depth, "\n";
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement