Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/perl
- use strict;
- use warnings;
- use List::AllUtils qw(none sum min max);
- use Math::Combinatorics qw(combine);
- $| = 1;
- my $WIN = 25;
- my @list = map { chomp; $_ } <>;
- # Part 1
- my $part1;
- foreach my $i ($WIN .. $#list) {
- print "Looking at: $list[$i] \r";
- if (none { sum(@$_) == $list[$i] } combine(2, @list[$i-$WIN .. $i-1])) {
- $part1 = $list[$i];
- last;
- }
- }
- print "Part 1: $part1 \n";
- # Part 2
- my ($i, $j);
- my $s = $list[0];
- while ($s != $part1) {
- $s += $list[++$j] if ($s < $part1);
- $s -= $list[$i++] if ($s > $part1);
- }
- print "\nList: ", join( ',', @list[$i .. $j] ), "\n";
- my $part2 = min( @list[$i .. $j] ) + max( @list[$i .. $j] );
- print "Part 2: $part2\n";
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement