adventofcode/2023/08/Part2.pm

36 lines
609 B
Perl
Raw Normal View History

2023-12-08 15:31:44 +00:00
use 5.38.0;
package Part2;
use Part1;
2023-12-08 15:52:54 +00:00
use List::AllUtils qw/all /;
2023-12-08 15:31:44 +00:00
sub solution_2 ($input) {
2023-12-08 15:52:54 +00:00
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;
2023-12-08 15:31:44 +00:00
}
1;
2023-12-08 15:52:54 +00:00
1;