sol2.pl (529B) [raw]
1 #!/usr/bin/env perl 2 # idea: 3 # HAS to be O(N) because have to check each item in worst case 4 use strict; 5 use warnings; 6 7 # first read the whole file in 8 my @all; 9 10 open(my $fh, '<', 'input') or die "$!"; 11 for (<$fh>) { 12 chomp; 13 push @all, $_; 14 } 15 16 for (my $i = 0; $i < scalar @all; $i++) { 17 my $cand = shift @all; 18 my $magic = 2020 - $cand; 19 my %seen; 20 for (@all) { 21 my $targ = $magic - $_; 22 if (exists $seen{$targ}) { 23 print $targ * $_ * $cand . "\n"; 24 exit(0); 25 } else { 26 $seen{$_} = 1; 27 } 28 } 29 push @all, $cand; 30 } 31 exit(1);