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/day1/sumto.pl | |
| parent | ae3853b6e8ab02023ccd74baac6dc177b1ee879a (diff) | |
| download | aoc-4bb6f8d06c0e384f3394012b1d48da58ed28cc5e.tar.gz aoc-4bb6f8d06c0e384f3394012b1d48da58ed28cc5e.tar.bz2 aoc-4bb6f8d06c0e384f3394012b1d48da58ed28cc5e.zip | |
2020, tracking
Diffstat (limited to '2020/day1/sumto.pl')
| -rw-r--r-- | 2020/day1/sumto.pl | 28 |
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 @@ | |||
| 1 | use strict; | ||
| 2 | use warnings; | ||
| 3 | use DDP; | ||
| 4 | use Smart::Comments; | ||
| 5 | |||
| 6 | open my $fh, '<', "input" or die "no input present, $!"; | ||
| 7 | chomp(my @nums = <$fh>); | ||
| 8 | close $fh; | ||
| 9 | |||
| 10 | @nums = sort { $a <=> $b } @nums; | ||
| 11 | |||
| 12 | my $l_idx = 0; | ||
| 13 | my $r_idx = $#nums; | ||
| 14 | |||
| 15 | my $total = $nums[$l_idx] + $nums[$r_idx]; | ||
| 16 | |||
| 17 | while ($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 | |||
| 28 | print $nums[$l_idx] * $nums[$r_idx]; | ||
