in reply to Challenge: Ricochet Robots
Hmmm...
#!/usr/bin/perl use strict; # https://perlmonks.org/?node_id=11128527 use warnings; local $_ = <<END; --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---. | | R | | . .---. . . . . . . . . . . .---. . | | | | . . . . . . . . . . . . . . . . | | | . . . . . . . . . . .---. . . . . | | | . . . . . .---. . . . . . . . .---. | | ---. . . .---. . . . . . . . . . . . | | | . . . . . . . . . . . . . . . . | | | | .---. . . . . .---.---. .---. . .---. . . | * | | | | . . . . . . . . . . . . . . . . | | | | . . . . . . .---.---. . . .---. . . . | | B | | . . . .---. .---. . . . . . . . .---. | | | ---. . . . . . . . . . . . . . . . | | . . . . . . .---. .---. . . . . . . | | | | .---. . . . . . . . . . . . . . . | Y | | | . . . . . . . . . . . . . .---. . | | | | . . .---. . . . . . . .---. . . . . | | G | | --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---. END my $starpos = /\*/ && $-[0]; my $w = /\n/ && $-[0]; my $gap = qr/.{$w}/s; my @queue = "$_= "; my %seen; while( @queue ) { (local $_, my $moves) = split /=/, shift @queue; $seen{$_}++ and next; if( 'Y' eq substr $_, $starpos, 1 ) { my $numoves = $moves =~ tr/lrud//; print "\n$_\ncompleted in $numoves moves $moves\n"; exit; } print "$_=$moves\n"; for my $robot ( qw( Y G ) ) { /(?:\| |\w )\K[ *]([ *]+)$robot/ and push @queue, (s/(?:\| |\w )\K[ *]([ *]+)$robot/$robot$1 /r) . "=$moves ${robot}l"; /$robot([ *]+)([ *])(?= \w| \|)/ and push @queue, (s/$robot([ *]+)[ *](?= \w| \|)/ $1$robot/r) . "=$moves ${robot}r"; /$robot((?:$gap[ *])*$gap)([ *])(?=${gap}-|$gap $gap\w)/ and push +@queue, (s/$robot((?:$gap[ *])+$gap)[ *](?=${gap}-|$gap $gap\w)/ $1$robo +t/r) . "=$moves ${robot}d"; /(?:-$gap|\w$gap $gap)\K[ *]((?:$gap[ *])*$gap)$robot/ and push @q +ueue, (s/(?:-$gap|\w$gap $gap)\K[ *]((?:$gap[ *])*$gap)$robot/$robot$1 + /r) . "=$moves ${robot}u"; } }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Challenge: Ricochet Robots
by LanX (Saint) on Feb 19, 2021 at 17:03 UTC | |
by tybalt89 (Monsignor) on Feb 19, 2021 at 19:06 UTC | |
by LanX (Saint) on Feb 19, 2021 at 20:54 UTC | |
by tybalt89 (Monsignor) on Feb 19, 2021 at 17:22 UTC | |
by LanX (Saint) on Feb 19, 2021 at 19:19 UTC | |
by tybalt89 (Monsignor) on Feb 19, 2021 at 19:28 UTC | |
by LanX (Saint) on Feb 19, 2021 at 20:47 UTC |