This commit is contained in:
Yanick Champoux 2023-12-08 10:35:08 -05:00
parent b1cf1719a3
commit c7b8b6a8c8
4 changed files with 23 additions and 23 deletions

View File

@ -4,36 +4,38 @@ package Part1;
use List::AllUtils qw/ /;
sub parse_input($input) {
sub parse_input ($input) {
my ( $directions, undef, @nodes ) = split "\n", $input;
$directions = [ split "", $directions =~ s/R/1/gr =~ s/L/0/gr ];
my %nodes;
for(@nodes) {
for (@nodes) {
my @n = /([A-Z]+)/g;
$nodes{ shift @n } = \@n;
}
return {
directions => $directions,
nodes => \%nodes };
nodes => \%nodes
};
}
sub solution_1 ($input) {
my $p = parse_input($input);
my $p = parse_input($input);
my @directions = $p->{directions}->@*;
my %nodes = $p->{nodes}->%*;
my %nodes = $p->{nodes}->%*;
my $visited = 0;
my $visited = 0;
my $next_index = 0;
my $current = 'AAA';
my $current = 'AAA';
while( $current ne 'ZZZ') {
while ( $current ne 'ZZZ' ) {
$visited++;
$current = $nodes{$current}->[ $directions[$next_index++ % @directions ] ];
$current =
$nodes{$current}->[ $directions[ $next_index++ % @directions ] ];
}
return $visited;

View File

@ -6,7 +6,6 @@ use Part1;
use List::AllUtils qw/ /;
sub solution_2 ($input) {
...;
}

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 = (

View File

@ -7,19 +7,20 @@ use File::Serialize;
use Part1;
my $input = path('input')->slurp;
my $example = path('example')->slurp;
my $input = path('input')->slurp;
my $example = path('example')->slurp;
my %solutions = deserialize_file('solutions.yml')->%*;
my $parsed = Part1::parse_input($example);
use DDP; p $parsed;
use DDP;
p $parsed;
is $parsed->{directions} => [1,0];
is $parsed->{nodes}{AAA} => [ qw/ BBB CCC /];
is $parsed->{directions} => [ 1, 0 ];
is $parsed->{nodes}{AAA} => [qw/ BBB CCC /];
is Part1::solution_1($example) => 2;
is Part1::solution_1(<<'END') => 6;
is Part1::solution_1($example) => 2;
is Part1::solution_1(<<'END') => 6;
LLR
AAA = (BBB, BBB)
@ -27,11 +28,9 @@ BBB = (AAA, ZZZ)
ZZZ = (ZZZ, ZZZ)
END
SKIP: {
# skip "not there yet" if $solutions{1} eq 'TODO';
SKIP: {
# skip "not there yet" if $solutions{1} eq 'TODO';
is Part1::solution_1($input) => $solutions{1};
}
done_testing;