part 1
This commit is contained in:
parent
187bcba8b5
commit
462dfc9c7f
31
2023/06/Part1.pm
Normal file
31
2023/06/Part1.pm
Normal file
@ -0,0 +1,31 @@
|
||||
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;
|
||||
|
||||
warn "$min - $max";
|
||||
return $max-$min+1;
|
||||
}
|
||||
|
||||
sub solution_1 ($input) {
|
||||
product map { race_solution(@$_)} parse_file($input);
|
||||
}
|
||||
|
||||
1;
|
14
2023/06/Part2.pm
Normal file
14
2023/06/Part2.pm
Normal file
@ -0,0 +1,14 @@
|
||||
use 5.38.0;
|
||||
|
||||
package Part2;
|
||||
|
||||
use Part1;
|
||||
|
||||
use List::AllUtils qw/ /;
|
||||
|
||||
|
||||
sub solution_2 ($input) {
|
||||
...;
|
||||
}
|
||||
|
||||
1;
|
41
2023/06/benchmark.pl
Normal file
41
2023/06/benchmark.pl
Normal file
@ -0,0 +1,41 @@
|
||||
use 5.38.0;
|
||||
|
||||
use Benchmark ':hireswallclock';
|
||||
use Path::Tiny;
|
||||
use JSON qw/ to_json /;
|
||||
use DateTime;
|
||||
|
||||
use Part1;
|
||||
use Part2;
|
||||
|
||||
my $day = path('.')->absolute->basename =~ s/^0//r;
|
||||
my $year = path('.')->absolute->parent->basename;
|
||||
|
||||
my @parts = (
|
||||
{ part => 1, sub => \&Part1::solution_1, expected => 'TODO' },
|
||||
{ part => 2, sub => \&Part2::solution_2, expected => 'TODO' },
|
||||
);
|
||||
|
||||
my $input = path('./input')->slurp;
|
||||
|
||||
for my $part (@parts) {
|
||||
my $res = Benchmark::countit(
|
||||
10,
|
||||
sub {
|
||||
$part->{sub}->($input) == $part->{expected} or die;
|
||||
}
|
||||
);
|
||||
|
||||
my $result = {
|
||||
day => $day,
|
||||
year => $year,
|
||||
|
||||
#variant => '',
|
||||
language => 'perl',
|
||||
part => $part->{part},
|
||||
time => $res->cpu_a / $res->iters,
|
||||
timestamp => DateTime->now->iso8601,
|
||||
};
|
||||
say to_json $result;
|
||||
}
|
||||
|
2
2023/06/example
Normal file
2
2023/06/example
Normal file
@ -0,0 +1,2 @@
|
||||
Time: 7 15 30
|
||||
Distance: 9 40 200
|
20
2023/06/part1.t
Normal file
20
2023/06/part1.t
Normal file
@ -0,0 +1,20 @@
|
||||
use 5.38.0;
|
||||
|
||||
use Test2::V0;
|
||||
|
||||
use Path::Tiny;
|
||||
|
||||
use Part1;
|
||||
|
||||
my $input = path('input')->slurp;
|
||||
my $example = path('example')->slurp;
|
||||
|
||||
is [ map { Part1::race_solution(@$_)} Part1::parse_file($example)]
|
||||
=> [4,8,9];
|
||||
|
||||
is Part1::solution_1($example) => 288;
|
||||
|
||||
cmp_ok Part1::solution_1($input), '<' ,230202;
|
||||
is Part1::solution_1($input) => 'TODO';
|
||||
|
||||
done_testing;
|
13
2023/06/part2.t
Normal file
13
2023/06/part2.t
Normal file
@ -0,0 +1,13 @@
|
||||
use 5.38.0;
|
||||
|
||||
use Test2::V0;
|
||||
|
||||
use Path::Tiny;
|
||||
|
||||
use Part2;
|
||||
|
||||
my $input = path('input')->slurp;
|
||||
|
||||
is Part2::solution_2($input) => 'TODO';
|
||||
|
||||
done_testing;
|
2
2023/06/solutions.yml
Normal file
2
2023/06/solutions.yml
Normal file
@ -0,0 +1,2 @@
|
||||
1: 211904
|
||||
2: TODO
|
Loading…
Reference in New Issue
Block a user