Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re^3: Challenge: Ricochet Robots

by tybalt89 (Monsignor)
on Feb 19, 2021 at 19:06 UTC ( [id://11128571]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Challenge: Ricochet Robots
in thread Challenge: Ricochet Robots

Just a litle tweaking :)

#!/usr/bin/perl use strict; # https://perlmonks.org/?node_id=11128527 use warnings; local $_ = <<END; --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---. | | R | | . .---. . . . . . . . . . . .---. . | | | | . . . . . . . . . . . . . . . . | | | . . . . . . . . . . .---. . . . . | | | . . . . . .---. . . . . . . . .---. | | ---. . . .---. . . . . . . . . . . . | | | . . . . . . . . . . . . . . . . | | | | .---. . . . . .---.---. .---. . .---. . . | | | | | . . . . . . . . . . . . . . . . | * | | | . . . . . . .---.---. . . .---. . . . | | B | | . . . .---. .---. . . . . . . . .---. | | | ---. . . . . . . . . . . . . . . . | | . . . . . . .---. .---. . . . . . . | | | | .---. . . . . . . . . . . . . . . | Y | | | . . . . . . . . . . . . . .---. . | | | | . . .---. . . . . . . .---. . . . . | | G | | --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---. END my $starpos = /\*/ && $-[0]; my $base = tr/RGBY*/ /r; my $w = /\n/ && $-[0]; my $gap = qr/.{$w}/s; my @queue = code($_) . "= "; my %seen; my %before; my $max = 1025; sub code { lc(shift) =~ tr/rgby/\0/cr =~ s/\0+/length $&/ger } while( @queue ) { my ($grid, $moves) = split /=/, shift @queue; $seen{$grid}++ and next; local $_ = $grid =~ s/\d+/"\0" x $&/ger ^ $base; 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 R ) ) { /(?:\| |\w )\K[ *]([ *]+)$robot/ && $before{$-[0]}++ < $max and push @queue, code(s/(?:\| |\w )\K[ *]([ *]+)$robot/$robot$1 /r) . "=$moves ${robot}l"; /$robot([ *]+)([ *])(?= \w| \|)/ && $before{$-[2]}++ < $max and push @queue, code(s/$robot([ *]+)[ *](?= \w| \|)/ $1$robot/r) . "=$moves ${robot}r"; /$robot((?:$gap[ *])*$gap)([ *])(?=${gap}-|$gap $gap\w)/ && $before{$-[2]}++ < $max and push @queue, code(s/$robot((?:$gap[ *])+$gap)[ *](?=${gap}-|$gap $gap\w)/ $1$ +robot/r) . "=$moves ${robot}d"; /(?:-$gap|\w$gap $gap)\K[ *]((?:$gap[ *])*$gap)$robot/ && $before{$-[0]}++ < $max and push @queue, code(s/(?:-$gap|\w$gap $gap)\K[ *]((?:$gap[ *])*$gap)$robot/$rob +ot$1 /r) . "=$moves ${robot}u"; } }

Replies are listed 'Best First'.
Re^4: Challenge: Ricochet Robots
by LanX (Saint) on Feb 19, 2021 at 20:54 UTC
    >   for my $robot ( qw( Y G R ) )

    What about "B"? ;)

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11128571]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (3)
As of 2024-04-16 22:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found