Advertisement
musifter

AoC day 13 (pt2), Perl

Dec 13th, 2020
1,552
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 0.51 KB | None | 0 0
  1. #!/usr/bin/perl
  2.  
  3. use strict;
  4. use warnings;
  5.  
  6. my $toss = <>;
  7. my @buses = map { chomp; split /,/ } <>;
  8.  
  9. my %table;
  10. foreach my $i (0 .. $#buses) {
  11.     next if ($buses[$i] eq 'x');
  12.     $table{ $buses[$i] } = ($buses[$i] - $i) % $buses[$i];
  13. }
  14.  
  15. my @mods = sort { $b <=> $a } keys %table;
  16.  
  17. my $jump = shift @mods;
  18. my $time = $table{$jump};
  19. while (@mods) {
  20.     my $next = shift @mods;
  21.     while ($time % $next != $table{$next}) {
  22.         $time += $jump;
  23.     }
  24.  
  25.     $jump *= $next;
  26. }
  27.  
  28. print "Part 2: $time\n";
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement