part 1 done, part2 wip
This commit is contained in:
parent
692e0301e7
commit
c446579923
24
2023/09/Part1.pm
Normal file
24
2023/09/Part1.pm
Normal file
@ -0,0 +1,24 @@
|
||||
use 5.38.0;
|
||||
|
||||
package Part1;
|
||||
|
||||
use List::AllUtils qw/ all sum /;
|
||||
|
||||
sub predict(@entries) {
|
||||
my $sum = 0;
|
||||
|
||||
until( all { !$_ } @entries ) {
|
||||
$sum += $entries[-1];
|
||||
@entries = map { $entries[$_]-$entries[$_-1] } 1..$#entries;
|
||||
}
|
||||
|
||||
return $sum;
|
||||
}
|
||||
|
||||
sub solution_1 ($input) {
|
||||
my @lines = map { [split]} split "\n", $input;
|
||||
|
||||
return sum map { predict(@$_) } @lines;
|
||||
}
|
||||
|
||||
1;
|
26
2023/09/Part2.pm
Normal file
26
2023/09/Part2.pm
Normal file
@ -0,0 +1,26 @@
|
||||
use 5.38.0;
|
||||
|
||||
package Part2;
|
||||
|
||||
use Part1;
|
||||
|
||||
use List::AllUtils qw/ all sum /;
|
||||
|
||||
sub predict(@entries) {
|
||||
my $sum = 0;
|
||||
|
||||
until( all { !$_ } @entries ) {
|
||||
$sum = $entries[0]-$sum;
|
||||
@entries = map { $entries[$_]-$entries[$_-1] } 1..$#entries;
|
||||
}
|
||||
|
||||
return -$sum;
|
||||
}
|
||||
|
||||
sub solution_2 ($input) {
|
||||
my @lines = map { [split]} split "\n", $input;
|
||||
|
||||
return sum map { predict(@$_) } @lines;
|
||||
}
|
||||
|
||||
1;
|
43
2023/09/benchmark.pl
Normal file
43
2023/09/benchmark.pl
Normal file
@ -0,0 +1,43 @@
|
||||
use 5.38.0;
|
||||
|
||||
use Benchmark ':hireswallclock';
|
||||
use Path::Tiny;
|
||||
use JSON qw/ to_json /;
|
||||
use DateTime;
|
||||
use File::Serialize;
|
||||
|
||||
use Part1;
|
||||
use Part2;
|
||||
|
||||
my $day = path('.')->absolute->basename =~ s/^0//r;
|
||||
my $year = path('.')->absolute->parent->basename;
|
||||
my $solutions = deserialize_file('solutions.yml');
|
||||
|
||||
my @parts = (
|
||||
{ part => 1, sub => \&Part1::solution_1, expected => $solutions->{1} },
|
||||
{ part => 2, sub => \&Part2::solution_2, expected => $solutions->{2} },
|
||||
);
|
||||
|
||||
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;
|
||||
}
|
||||
|
17
2023/09/part1.t
Normal file
17
2023/09/part1.t
Normal file
@ -0,0 +1,17 @@
|
||||
use 5.38.0;
|
||||
|
||||
use Test2::V0;
|
||||
|
||||
use Path::Tiny;
|
||||
|
||||
use Part1;
|
||||
|
||||
my $input = path('input')->slurp;
|
||||
|
||||
is Part1::predict(qw/0 3 6 9 12 15/) => 18;
|
||||
is Part1::predict(qw/1 3 6 10 15 21/) => 28;
|
||||
is Part1::predict(qw/10 13 16 21 30 45/) => 68;
|
||||
|
||||
is Part1::solution_1($input) => 'TODO';
|
||||
|
||||
done_testing;
|
24
2023/09/part2.t
Normal file
24
2023/09/part2.t
Normal file
@ -0,0 +1,24 @@
|
||||
use 5.38.0;
|
||||
|
||||
use Test2::V0;
|
||||
|
||||
use Path::Tiny;
|
||||
|
||||
use Part2;
|
||||
|
||||
my $input = path('input')->slurp;
|
||||
|
||||
is Part2::predict(qw/0 3 6 9 12 15/) => -3;
|
||||
is Part2::predict(qw/1 3 6 10 15 21/) => 0;
|
||||
is Part2::predict(qw/10 13 16 21 30 45/) => 5;
|
||||
|
||||
is Part2::solution_2(<<END) => 2;
|
||||
0 3 6 9 12 15
|
||||
1 3 6 10 15 21
|
||||
10 13 16 21 30 45
|
||||
END
|
||||
|
||||
isnt Part2::solution_2($input) => -59;
|
||||
is Part2::solution_2($input) => 'TODO';
|
||||
|
||||
done_testing;
|
2
2023/09/solutions.yml
Normal file
2
2023/09/solutions.yml
Normal file
@ -0,0 +1,2 @@
|
||||
1: 1884768153
|
||||
2: TODO
|
Loading…
Reference in New Issue
Block a user