#!/usr/bin/perl use warnings; use strict; use feature qw{ say }; my %special = ( ... ); # copy from the OP my %reachable = ( 0 => undef ); my $turn = 0; while (not exists $reachable{100}) { my %next; for my $square (keys %reachable) { for my $spin (1 .. 6) { my $target = $square + $spin; next if $target > 100; undef $next{$target}; undef $next{ $special{$target}{end} } if $special{$target}; } } undef @reachable{ keys %next }; say ++$turn, ': ', join ' ', sort { $a <=> $b } keys %reachable; } say "$turn";