summaryrefslogtreecommitdiffstats
path: root/2020/day1/sumto.pl
blob: 931f5e228d2cf9f19eb0ae84833e828c21aa44e4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
use strict;
use warnings;
use DDP;
use Smart::Comments;

open my $fh, '<', "input" or die "no input present, $!";
chomp(my @nums = <$fh>);
close $fh;

@nums = sort { $a <=> $b } @nums;

my $l_idx = 0;
my $r_idx = $#nums;

my $total = $nums[$l_idx] + $nums[$r_idx];

while ($total != 2020) {

    if ($total < 2020) {
        $l_idx++; # total too low, increase
    } else {
        $r_idx--; # total too high, decrease
    }

    $total = $nums[$l_idx] + $nums[$r_idx];
}

print $nums[$l_idx] * $nums[$r_idx];