summary refs log tree commit diff homepage
path: root/2023/day08.pl
blob: 12b41ec0a3b9054e497c615e46b96505f10a5e33 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
use strict;
use warnings;
my (@dirs, %left, %right);
while (<>) {
	chomp;
	@dirs = split(//) if /^[LR]+$/;
	next unless /([A-Z]{3}) = \(([A-Z]{3}), ([A-Z]{3})\)/;
	$left{$1} = $2;
	$right{$1} = $3;
}
my ($node, $step) = ("AAA", 0);
while ($node ne "ZZZ") {
	my $dir = $dirs[$step++ % @dirs];
	$node = ($dir eq "L" ? $left{$node} : $right{$node});
}
print $step, "\n";