31 lines
606 B
Perl
31 lines
606 B
Perl
|
use 5.38.0;
|
||
|
|
||
|
package Part1;
|
||
|
|
||
|
use List::AllUtils qw/ product /;
|
||
|
use POSIX qw/ floor ceil /;
|
||
|
|
||
|
sub my_zip($a,$b) {
|
||
|
return map { [ $a->[$_], $b->[$_] ] } 0..$a->@*-1;
|
||
|
}
|
||
|
|
||
|
sub parse_file($input) {
|
||
|
my_zip( map { [/(\d+)/g ] } split "\n", $input );
|
||
|
}
|
||
|
|
||
|
sub race_solution($time,$distance) {
|
||
|
my $min = ( $time - sqrt( $time**2 - 4 * $distance) ) / 2;
|
||
|
my $max = ( $time + sqrt( $time**2 - 4 * $distance) ) / 2;
|
||
|
|
||
|
$min = int($min)+1;
|
||
|
$max = ($max == int $max)?$max-1:int $max;
|
||
|
|
||
|
return $max-$min+1;
|
||
|
}
|
||
|
|
||
|
sub solution_1 ($input) {
|
||
|
product map { race_solution(@$_)} parse_file($input);
|
||
|
}
|
||
|
|
||
|
1;
|