summaryrefslogtreecommitdiffstats
path: root/2020/day1/sumto.pl
diff options
context:
space:
mode:
authorYigit Sever2021-12-13 10:38:11 +0300
committerYigit Sever2021-12-13 10:38:11 +0300
commit74b27ccca31bb757c737dd7fdc02f513f57561b2 (patch)
treee27db4cd0873c81a53d32277446d926d176304e0 /2020/day1/sumto.pl
parent3919f90cfbfbba26c8e39f979280649f5e08aea8 (diff)
parentac8125750abed263619da4cc6d653bb5ab76f007 (diff)
downloadaoc-74b27ccca31bb757c737dd7fdc02f513f57561b2.tar.gz
aoc-74b27ccca31bb757c737dd7fdc02f513f57561b2.tar.bz2
aoc-74b27ccca31bb757c737dd7fdc02f513f57561b2.zip
Merge remote-tracking branch 'origin/main'
Diffstat (limited to '2020/day1/sumto.pl')
-rw-r--r--2020/day1/sumto.pl28
1 files changed, 28 insertions, 0 deletions
diff --git a/2020/day1/sumto.pl b/2020/day1/sumto.pl
new file mode 100644
index 0000000..931f5e2
--- /dev/null
+++ b/2020/day1/sumto.pl
@@ -0,0 +1,28 @@
1use strict;
2use warnings;
3use DDP;
4use Smart::Comments;
5
6open my $fh, '<', "input" or die "no input present, $!";
7chomp(my @nums = <$fh>);
8close $fh;
9
10@nums = sort { $a <=> $b } @nums;
11
12my $l_idx = 0;
13my $r_idx = $#nums;
14
15my $total = $nums[$l_idx] + $nums[$r_idx];
16
17while ($total != 2020) {
18
19 if ($total < 2020) {
20 $l_idx++; # total too low, increase
21 } else {
22 $r_idx--; # total too high, decrease
23 }
24
25 $total = $nums[$l_idx] + $nums[$r_idx];
26}
27
28print $nums[$l_idx] * $nums[$r_idx];