musifter

AoC 2025, day 1 (Perl)

Dec 1st, 2025
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 0.59 KB | None | 0 0
  1. #!/usr/bin/perl
  2.  
  3. use strict;
  4. use warnings;
  5.  
  6. use feature         qw(say);
  7.  
  8. my @input = map { y#LR#- #; int $_ } <>;
  9.  
  10. my $part1 = 0;
  11. my $part2 = 0;
  12.  
  13. my $pos = 50;
  14. foreach my $move (@input) {
  15.     # Multiple rotations
  16.     $part2 += int(abs($move) / 100);
  17.     $move = $move % (100 * ($move <=> 0));  # mod maintaining direction (sign)
  18.  
  19.     # Move through/to 0 (but not from, as that would double count)
  20.     my $new_pos = $pos + $move;
  21.     $part2++ if ($pos and !(0 < $new_pos < 100));
  22.  
  23.     $pos = $new_pos % 100;
  24.     $part1++ if (!$pos);
  25. }
  26.  
  27. say "Part 1: $part1";
  28. say "Part 2: $part2";
Advertisement
Add Comment
Please, Sign In to add comment