diff options
author | Yigit Sever | 2021-12-12 01:24:32 +0300 |
---|---|---|
committer | Yigit Sever | 2021-12-12 01:24:32 +0300 |
commit | 4bb6f8d06c0e384f3394012b1d48da58ed28cc5e (patch) | |
tree | d6478c85c0488a1059567ccd2882cb10039c2546 /2020/day9/xmas_encoder.pl | |
parent | ae3853b6e8ab02023ccd74baac6dc177b1ee879a (diff) | |
download | aoc-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.pl | 70 |
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 @@ | |||
1 | use strict; | ||
2 | use warnings; | ||
3 | use DDP; | ||
4 | use Smart::Comments; | ||
5 | use Tie::File; | ||
6 | use List::Util qw(min max); | ||
7 | |||
8 | tie my @xmas, 'Tie::File', "input" or die "no input present, $!"; | ||
9 | |||
10 | my %preamble; | ||
11 | # because it's easier this way trust me | ||
12 | my @also_queue; | ||
13 | my $goalnum; | ||
14 | |||
15 | foreach 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 | |||
44 | my @contiguous; | ||
45 | my $total = 0; | ||
46 | |||
47 | foreach 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 | |||
70 | untie @xmas; | ||