From 4bb6f8d06c0e384f3394012b1d48da58ed28cc5e Mon Sep 17 00:00:00 2001 From: Yigit Sever Date: Sun, 12 Dec 2021 01:24:32 +0300 Subject: 2020, tracking --- 2020/day9/xmas_encoder.pl | 70 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 2020/day9/xmas_encoder.pl (limited to '2020/day9/xmas_encoder.pl') diff --git a/2020/day9/xmas_encoder.pl b/2020/day9/xmas_encoder.pl new file mode 100644 index 0000000..4fdcb9b --- /dev/null +++ b/2020/day9/xmas_encoder.pl @@ -0,0 +1,70 @@ +use strict; +use warnings; +use DDP; +use Smart::Comments; +use Tie::File; +use List::Util qw(min max); + +tie my @xmas, 'Tie::File', "input" or die "no input present, $!"; + +my %preamble; +# because it's easier this way trust me +my @also_queue; +my $goalnum; + +foreach my $idx (0 .. $#xmas) { + if ($idx < 25) { + my $t = int($xmas[$idx]); + $preamble{$t} = 1; + push @also_queue, $t; + } else { + my $nextnum = int($xmas[$idx]); + my $tester = 0; + foreach my $num (keys %preamble) { + $tester++; + if (exists $preamble{$nextnum - $num}) { + my $old = shift @also_queue; + delete $preamble{$old}; + push @also_queue, $nextnum; + $preamble{$nextnum} = 1; + last; + } + } + + if (not scalar grep { $_ == $nextnum } @also_queue) { + $goalnum = $nextnum; + print("XMAS weak num: $goalnum\n"); + last; + } + } +} + +# find the contiguous set + +my @contiguous; +my $total = 0; + +foreach my $curr (@xmas) { + if ($total + $curr < $goalnum) { + $total += $curr; + push @contiguous, $curr; + } elsif ($total + $curr > $goalnum) { + while ($total + $curr > $goalnum) { + my $evictee = shift @contiguous; + if (not defined $evictee) { + last; + } + $total -= $evictee; + } + push @contiguous, $curr; + $total += $curr; + } + + if ($total == $goalnum) { + print(min(@contiguous) + max(@contiguous)); + p @contiguous; + exit; + } +} + +untie @xmas; -- cgit v1.2.3-70-g09d2