Merge branch '2023-09'

This commit is contained in:
Yanick Champoux 2023-12-09 13:03:42 -05:00
commit 9856545a59
8 changed files with 148 additions and 2 deletions

24
2023/09/Part1.pm Normal file
View 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;

32
2023/09/Part2.pm Normal file
View File

@ -0,0 +1,32 @@
use 5.38.0;
package Part2;
use Part1;
use List::AllUtils qw/ all sum /;
sub predict(@entries) {
my $sum = 0;
my @first = ();
until( all { !$_ } @entries ) {
push @first, $entries[0];
@entries = map { $entries[$_]-$entries[$_-1] } 1..$#entries;
}
while(@first) {
$sum = pop( @first ) - $sum;
}
return $sum;
}
sub solution_2 ($input) {
my @lines = map { [split]} split "\n", $input;
return sum map { predict(@$_) } @lines;
}
1;

44
2023/09/benchmark.pl Normal file
View File

@ -0,0 +1,44 @@
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,
persec =>$res->iters/$res->cpu_a ,
timestamp => DateTime->now->iso8601,
};
say to_json $result;
}

17
2023/09/part1.t Normal file
View 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) => 1884768153;
done_testing;

24
2023/09/part2.t Normal file
View 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) => 1031;
done_testing;

2
2023/09/solutions.yml Normal file
View File

@ -0,0 +1,2 @@
1: 1884768153
2: 1031

View File

@ -16,3 +16,5 @@
{"year":"2023","part":2,"language":"perl","day":"6","time":7.07875059716474e-06,"timestamp":"2023-12-06T15:43:13","persec":141267.867298578}
{"language":"perl","persec":85.2380952380952,"year":"2023","timestamp":"2023-12-07T16:08:30","time":0.011731843575419,"part":1,"day":"7"}
{"day":"7","part":2,"persec":85.2380952380952,"year":"2023","timestamp":"2023-12-07T16:08:43","time":0.011731843575419,"language":"perl"}
{"persec":113.488372093023,"timestamp":"2023-12-09T17:57:35","language":"perl","year":"2023","time":0.00881147540983607,"day":"9","part":1}
{"persec":109.783631232361,"timestamp":"2023-12-09T17:57:47","language":"perl","time":0.00910882604970008,"day":"9","part":2,"year":"2023"}

View File

@ -9,8 +9,8 @@ use File::Serialize;
use Part1;
use Part2;
my $day = path('.')->absolute->basename =~ s/^0//r;
my $year = path('.')->absolute->parent->basename;
my $day = path('.')->absolute->basename =~ s/^0//r;
my $year = path('.')->absolute->parent->basename;
my $solutions = deserialize_file('solutions.yml');
my @parts = (
@ -36,6 +36,7 @@ for my $part (@parts) {
language => 'perl',
part => $part->{part},
time => $res->cpu_a / $res->iters,
persec => $res->iters / $res->cpu_a,
timestamp => DateTime->now->iso8601,
};
say to_json $result;