prettier
This commit is contained in:
parent
b1cf1719a3
commit
c7b8b6a8c8
@ -4,36 +4,38 @@ package Part1;
|
|||||||
|
|
||||||
use List::AllUtils qw/ /;
|
use List::AllUtils qw/ /;
|
||||||
|
|
||||||
sub parse_input($input) {
|
sub parse_input ($input) {
|
||||||
my ( $directions, undef, @nodes ) = split "\n", $input;
|
my ( $directions, undef, @nodes ) = split "\n", $input;
|
||||||
|
|
||||||
$directions = [ split "", $directions =~ s/R/1/gr =~ s/L/0/gr ];
|
$directions = [ split "", $directions =~ s/R/1/gr =~ s/L/0/gr ];
|
||||||
|
|
||||||
my %nodes;
|
my %nodes;
|
||||||
|
|
||||||
for(@nodes) {
|
for (@nodes) {
|
||||||
my @n = /([A-Z]+)/g;
|
my @n = /([A-Z]+)/g;
|
||||||
$nodes{ shift @n } = \@n;
|
$nodes{ shift @n } = \@n;
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
directions => $directions,
|
directions => $directions,
|
||||||
nodes => \%nodes };
|
nodes => \%nodes
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
sub solution_1 ($input) {
|
sub solution_1 ($input) {
|
||||||
my $p = parse_input($input);
|
my $p = parse_input($input);
|
||||||
my @directions = $p->{directions}->@*;
|
my @directions = $p->{directions}->@*;
|
||||||
my %nodes = $p->{nodes}->%*;
|
my %nodes = $p->{nodes}->%*;
|
||||||
|
|
||||||
my $visited = 0;
|
my $visited = 0;
|
||||||
my $next_index = 0;
|
my $next_index = 0;
|
||||||
my $current = 'AAA';
|
my $current = 'AAA';
|
||||||
|
|
||||||
while( $current ne 'ZZZ') {
|
while ( $current ne 'ZZZ' ) {
|
||||||
$visited++;
|
$visited++;
|
||||||
|
|
||||||
$current = $nodes{$current}->[ $directions[$next_index++ % @directions ] ];
|
$current =
|
||||||
|
$nodes{$current}->[ $directions[ $next_index++ % @directions ] ];
|
||||||
}
|
}
|
||||||
|
|
||||||
return $visited;
|
return $visited;
|
||||||
|
@ -6,7 +6,6 @@ use Part1;
|
|||||||
|
|
||||||
use List::AllUtils qw/ /;
|
use List::AllUtils qw/ /;
|
||||||
|
|
||||||
|
|
||||||
sub solution_2 ($input) {
|
sub solution_2 ($input) {
|
||||||
...;
|
...;
|
||||||
}
|
}
|
||||||
|
@ -9,8 +9,8 @@ use File::Serialize;
|
|||||||
use Part1;
|
use Part1;
|
||||||
use Part2;
|
use Part2;
|
||||||
|
|
||||||
my $day = path('.')->absolute->basename =~ s/^0//r;
|
my $day = path('.')->absolute->basename =~ s/^0//r;
|
||||||
my $year = path('.')->absolute->parent->basename;
|
my $year = path('.')->absolute->parent->basename;
|
||||||
my $solutions = deserialize_file('solutions.yml');
|
my $solutions = deserialize_file('solutions.yml');
|
||||||
|
|
||||||
my @parts = (
|
my @parts = (
|
||||||
|
@ -7,19 +7,20 @@ use File::Serialize;
|
|||||||
|
|
||||||
use Part1;
|
use Part1;
|
||||||
|
|
||||||
my $input = path('input')->slurp;
|
my $input = path('input')->slurp;
|
||||||
my $example = path('example')->slurp;
|
my $example = path('example')->slurp;
|
||||||
my %solutions = deserialize_file('solutions.yml')->%*;
|
my %solutions = deserialize_file('solutions.yml')->%*;
|
||||||
|
|
||||||
my $parsed = Part1::parse_input($example);
|
my $parsed = Part1::parse_input($example);
|
||||||
|
|
||||||
use DDP; p $parsed;
|
use DDP;
|
||||||
|
p $parsed;
|
||||||
|
|
||||||
is $parsed->{directions} => [1,0];
|
is $parsed->{directions} => [ 1, 0 ];
|
||||||
is $parsed->{nodes}{AAA} => [ qw/ BBB CCC /];
|
is $parsed->{nodes}{AAA} => [qw/ BBB CCC /];
|
||||||
|
|
||||||
is Part1::solution_1($example) => 2;
|
is Part1::solution_1($example) => 2;
|
||||||
is Part1::solution_1(<<'END') => 6;
|
is Part1::solution_1(<<'END') => 6;
|
||||||
LLR
|
LLR
|
||||||
|
|
||||||
AAA = (BBB, BBB)
|
AAA = (BBB, BBB)
|
||||||
@ -27,11 +28,9 @@ BBB = (AAA, ZZZ)
|
|||||||
ZZZ = (ZZZ, ZZZ)
|
ZZZ = (ZZZ, ZZZ)
|
||||||
END
|
END
|
||||||
|
|
||||||
SKIP: {
|
SKIP: {
|
||||||
# skip "not there yet" if $solutions{1} eq 'TODO';
|
# skip "not there yet" if $solutions{1} eq 'TODO';
|
||||||
is Part1::solution_1($input) => $solutions{1};
|
is Part1::solution_1($input) => $solutions{1};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
done_testing;
|
done_testing;
|
||||||
|
Loading…
Reference in New Issue
Block a user