36 lines
609 B
Perl
36 lines
609 B
Perl
use 5.38.0;
|
|
|
|
package Part2;
|
|
|
|
use Part1;
|
|
|
|
use List::AllUtils qw/all /;
|
|
|
|
sub solution_2 ($input) {
|
|
my $p = Part1::parse_input($input);
|
|
my @directions = $p->{directions}->@*;
|
|
my %nodes = $p->{nodes}->%*;
|
|
|
|
my $visited = 0;
|
|
my $next_index = 0;
|
|
my @current = grep { /A$/ } keys %nodes;
|
|
|
|
until ( all { /Z$/ } @current ) {
|
|
# use DDP; p @current;
|
|
# my $foo = <>;
|
|
$visited++;
|
|
|
|
@current = map {
|
|
$nodes{$_}->[ $directions[ $next_index % @directions ] ]
|
|
} @current;
|
|
$next_index++;
|
|
}
|
|
|
|
return $visited;
|
|
|
|
}
|
|
|
|
1;
|
|
|
|
1;
|