Advertisement
musifter

AoC day 9, Perl

Dec 9th, 2020
1,408
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 0.75 KB | None | 0 0
  1. #!/usr/bin/perl
  2.  
  3. use strict;
  4. use warnings;
  5.  
  6. use List::AllUtils       qw(none sum min max);
  7. use Math::Combinatorics  qw(combine);
  8.  
  9. $| = 1;
  10. my $WIN = 25;
  11.  
  12. my @list = map { chomp; $_ } <>;
  13.  
  14. # Part 1
  15. my $part1;
  16. foreach my $i ($WIN .. $#list) {
  17.     print "Looking at: $list[$i] \r";
  18.     if (none { sum(@$_) == $list[$i] } combine(2,  @list[$i-$WIN .. $i-1])) {
  19.         $part1 = $list[$i];
  20.         last;
  21.     }
  22. }
  23.  
  24. print "Part 1: $part1    \n";
  25.  
  26. # Part 2
  27. my ($i, $j);
  28. my $s = $list[0];
  29.  
  30. while ($s != $part1) {
  31.     $s += $list[++$j]  if ($s < $part1);
  32.     $s -= $list[$i++]  if ($s > $part1);
  33. }
  34.  
  35. print "\nList: ", join( ',', @list[$i .. $j] ), "\n";
  36.  
  37. my $part2 = min( @list[$i .. $j] ) + max( @list[$i .. $j] );
  38. print "Part 2: $part2\n";
  39.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement