summaryrefslogtreecommitdiffstats
path: root/2020/day9/xmas_encoder.pl
diff options
context:
space:
mode:
authorYigit Sever2021-12-12 01:24:32 +0300
committerYigit Sever2021-12-12 01:24:32 +0300
commit4bb6f8d06c0e384f3394012b1d48da58ed28cc5e (patch)
treed6478c85c0488a1059567ccd2882cb10039c2546 /2020/day9/xmas_encoder.pl
parentae3853b6e8ab02023ccd74baac6dc177b1ee879a (diff)
downloadaoc-4bb6f8d06c0e384f3394012b1d48da58ed28cc5e.tar.gz
aoc-4bb6f8d06c0e384f3394012b1d48da58ed28cc5e.tar.bz2
aoc-4bb6f8d06c0e384f3394012b1d48da58ed28cc5e.zip
2020, tracking
Diffstat (limited to '2020/day9/xmas_encoder.pl')
-rw-r--r--2020/day9/xmas_encoder.pl70
1 files changed, 70 insertions, 0 deletions
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 @@
1use strict;
2use warnings;
3use DDP;
4use Smart::Comments;
5use Tie::File;
6use List::Util qw(min max);
7
8tie my @xmas, 'Tie::File', "input" or die "no input present, $!";
9
10my %preamble;
11# because it's easier this way trust me
12my @also_queue;
13my $goalnum;
14
15foreach my $idx (0 .. $#xmas) {
16 if ($idx < 25) {
17 my $t = int($xmas[$idx]);
18 $preamble{$t} = 1;
19 push @also_queue, $t;
20 } else {
21 my $nextnum = int($xmas[$idx]);
22 my $tester = 0;
23 foreach my $num (keys %preamble) {
24 $tester++;
25 if (exists $preamble{$nextnum - $num}) {
26 my $old = shift @also_queue;
27 delete $preamble{$old};
28 push @also_queue, $nextnum;
29 $preamble{$nextnum} = 1;
30 last;
31 }
32 }
33
34 if (not scalar grep { $_ == $nextnum } @also_queue) {
35 $goalnum = $nextnum;
36 print("XMAS weak num: $goalnum\n");
37 last;
38 }
39 }
40}
41
42# find the contiguous set
43
44my @contiguous;
45my $total = 0;
46
47foreach my $curr (@xmas) {
48 if ($total + $curr < $goalnum) {
49 $total += $curr;
50 push @contiguous, $curr;
51 } elsif ($total + $curr > $goalnum) {
52 while ($total + $curr > $goalnum) {
53 my $evictee = shift @contiguous;
54 if (not defined $evictee) {
55 last;
56 }
57 $total -= $evictee;
58 }
59 push @contiguous, $curr;
60 $total += $curr;
61 }
62
63 if ($total == $goalnum) {
64 print(min(@contiguous) + max(@contiguous));
65 p @contiguous;
66 exit;
67 }
68}
69
70untie @xmas;