Advertisement
musifter

AoC day 15, Perl (faster)

Dec 15th, 2020 (edited)
1,451
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 0.37 KB | None | 0 0
  1. #!/usr/bin/perl
  2.  
  3. my @list = map { chomp; split /,/ } <>;
  4. my @hash;
  5. $hash[29999999] = undef;
  6.  
  7. foreach my $t (0 .. $#list - 1) {
  8.     $hash[ $list[$t] ] = $t + 1;
  9. }
  10.  
  11. my ($next, $curr) = (int($list[-1]), 0);
  12. foreach my $t ($#list + 1 .. 29999999) {
  13.     $curr = $next;
  14.     $next = (defined $hash[$curr]) ? $t - $hash[$curr] : 0;
  15.     $hash[$curr] = $t;
  16. }
  17. print "$next\n";
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement