2021-05
This commit is contained in:
parent
1bd00d8e73
commit
fdee8edce1
52
2021/05/part1.pm
Normal file
52
2021/05/part1.pm
Normal file
@ -0,0 +1,52 @@
|
||||
package part1;
|
||||
|
||||
use 5.34.0;
|
||||
use warnings;
|
||||
use experimental 'signatures';
|
||||
|
||||
use Path::Tiny;
|
||||
use List::AllUtils qw/ any min max sum zip pairmap/;
|
||||
use Math::Vector::Real;
|
||||
|
||||
sub score(%grid) {
|
||||
return sum map { $_ > 1 ? 1 : 0 } map { values %$_ } values %grid;
|
||||
}
|
||||
|
||||
sub delta($from,$to) {
|
||||
$from == $to ? 0 : $from < $to ? 1 : -1;
|
||||
}
|
||||
|
||||
sub steps($pa,$pb) {
|
||||
my $incr = V( pairmap { delta($a,$b) } zip @$pa, @$pb );
|
||||
|
||||
my @steps = ( V(@$pa) );
|
||||
my $dest = V(@$pb);
|
||||
while( $steps[-1] != $dest) {
|
||||
push @steps, V($steps[-1]->@*) + $incr;
|
||||
}
|
||||
|
||||
return @steps;
|
||||
|
||||
}
|
||||
|
||||
sub readFile ($filename) {
|
||||
return map {
|
||||
[ map { [split ',' ] } split ' -> ' ]
|
||||
} path($filename)->lines;
|
||||
}
|
||||
|
||||
sub solution (@sample) {
|
||||
my %grid;
|
||||
|
||||
for my $line (@sample) {
|
||||
next unless any { $line->[0][$_] == $line->[1][$_] } 0 .. 1;
|
||||
for my $step ( steps(@$line) ) {
|
||||
$grid{$step->[0]}{$step->[1]}++;
|
||||
}
|
||||
}
|
||||
|
||||
return score(%grid);
|
||||
|
||||
}
|
||||
|
||||
1;
|
26
2021/05/part2.pm
Normal file
26
2021/05/part2.pm
Normal file
@ -0,0 +1,26 @@
|
||||
package part2;
|
||||
|
||||
use 5.20.0;
|
||||
use warnings;
|
||||
use experimental 'signatures';
|
||||
|
||||
use List::AllUtils qw/ /;
|
||||
|
||||
require './part1.pm';
|
||||
|
||||
use experimental qw/ signatures postderef /;
|
||||
use List::AllUtils qw/ any min max sum zip pairmap /;
|
||||
|
||||
sub solution(@sample) {
|
||||
my %grid;
|
||||
|
||||
for my $line (@sample) {
|
||||
for my $step ( part1::steps(@$line) ) {
|
||||
$grid{$step->[0]}{$step->[1]}++;
|
||||
}
|
||||
}
|
||||
|
||||
return part1::score(%grid);
|
||||
}
|
||||
|
||||
1;
|
10
2021/05/sample
Normal file
10
2021/05/sample
Normal file
@ -0,0 +1,10 @@
|
||||
0,9 -> 5,9
|
||||
8,0 -> 0,8
|
||||
9,4 -> 3,4
|
||||
2,2 -> 2,1
|
||||
7,0 -> 7,4
|
||||
6,4 -> 2,0
|
||||
0,9 -> 2,9
|
||||
3,4 -> 1,4
|
||||
0,0 -> 8,8
|
||||
5,5 -> 8,2
|
26
2021/05/test.t
Normal file
26
2021/05/test.t
Normal file
@ -0,0 +1,26 @@
|
||||
use 5.34.0;
|
||||
|
||||
use Test2::V0;
|
||||
use Path::Tiny;
|
||||
|
||||
require './part1.pm';
|
||||
require './part2.pm';
|
||||
|
||||
my @sample = part1::readFile('./sample');
|
||||
my @input = part1::readFile('./input');
|
||||
|
||||
use DDP;
|
||||
|
||||
subtest part1 => sub {
|
||||
is [part1::steps([1,1],[3,3])] => [ [ 1,1 ],[2,2],[3,3] ];
|
||||
is [part1::steps([3,3],[1,1])] => [ [ 3,3 ],[2,2],[1,1] ];
|
||||
is [part1::steps([1,3],[1,1])] => [ [ 1,3 ],[1,2],[1,1] ];
|
||||
|
||||
is part1::solution(@sample ) => 5;
|
||||
is part1::solution(@input ) => 5576;
|
||||
};
|
||||
|
||||
subtest part2 => sub {
|
||||
is part2::solution(@sample ) => 12;
|
||||
is part2::solution(@input ) => 18144;
|
||||
};
|
Loading…
Reference in New Issue
Block a user