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/day1/sumto.pl | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 2020/day1/sumto.pl (limited to '2020/day1/sumto.pl') 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 @@ +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]; -- cgit v1.2.3-70-g09d2