adventofcode/2023/08/Part1.pm

46 lines
819 B
Perl

use 5.38.0;
package Part1;
use List::AllUtils qw/ /;
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) {
my @n = /([A-Z]+)/g;
$nodes{ shift @n } = \@n;
}
return {
directions => $directions,
nodes => \%nodes
};
}
sub solution_1 ($input) {
my $p = parse_input($input);
my @directions = $p->{directions}->@*;
my %nodes = $p->{nodes}->%*;
my $visited = 0;
my $next_index = 0;
my $current = 'AAA';
while ( $current ne 'ZZZ' ) {
$visited++;
$current =
$nodes{$current}->[ $directions[ $next_index++ % @directions ] ];
}
return $visited;
}
1;