Advertisement
musifter

AoC day 15, Perl

Dec 15th, 2020
1,511
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 0.47 KB | None | 0 0
  1. #!/usr/bin/perl
  2.  
  3. use strict;
  4. use warnings;
  5.  
  6. $| = 1;
  7.  
  8. my @list = map { chomp; split /,/ } <>;
  9. my %hash;
  10.  
  11. foreach my $t (0 .. $#list - 1) {
  12.     $hash{$list[$t]} = $t + 1;
  13. }
  14.  
  15. foreach my $t ($#list + 1 .. 30000000) {
  16.     my $next = $list[-1];
  17.     print "[$t] $next      \r"      if ($t % 10000 == 0);
  18.     print "[$t] $next      \n"      if ($t == 2020);
  19.  
  20.     push( @list, (exists $hash{ $next }) ? $t - $hash{ $next } : 0 );
  21.     $hash{ $next } = $t;
  22. }
  23. print "\n";
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement