![Smile :)](./images/smilies/icon_smile.gif)
btw, I was amazed how simple my program was at the end. At first I had matrix and kept track of everything, and was going to traverse it myself with keyboard input... at the end i just needed to keep track of the crossroads and the current string...
Code: Select all
#!/usr/bin/ruby
require 'open-uri'
NAME = 'username'; PW = 'password'
BASE_URI = "http://www.hacker.org/challenge/misc/maze.php?name=#{NAME}&password=#{PW}&steps="
moves = ['D']
while( not moves.empty? )
puts( current = moves.pop )
result = open(BASE_URI + current).read
if result =~ /keep moving/
%w{L R U D}.each {|d| moves.push( current + d )}
elsif not result =~ /(boom|off the edge)/
puts result
exit
end
end
No, you are right. Backtracking and Right/Left-Hand methods are different. (Both have in common that they don't necessarily find the optimal solution).tails wrote:I thought gfoot did just the same as your code above does. And it's not the right hand method, I think.
Code: Select all
#!/usr/bin/perl
use LWP::Simple;
my $url='http://www.hacker.org/challenge/misc/maze.php?name=NAME&password=PASSWORD&steps=';
my $path="D";
my @backtrack;
my @directions=qw( U D L R);
my %opposite=("U"=>"D", "D"=>"U", "L"=>"R", "R"=>"L");
my $content;
while (true) {
foreach $dir (@directions) {
next if ( substr($path,length($path)-1,1) eq $opposite{$dir} ); # don't turn around
push @backtrack,"$path$dir";
}
while (true) {
$path=pop @backtrack;
$content = get "$url$path";
die "Couldn't get $url" unless defined $content;
$content=~ s/<[^>]*>//gs;
$content=~ s/\n//gs;
last if ($content ne "boom");
}
if ($content ne "keep moving...") {
print "The End: $content\n";
last;
}
}
Code: Select all
trypath() {
printf "Try: %s " $1
res=`w3m -dump 'http://www.hacker.org/challenge/misc/maze.php?steps='$1`
echo $res
if echo $res | grep moving &>/dev/null; then
for i in U D L R; do trypath $1$i; done
fi
}
trypath 'D'
For PHP, use "file_get_contents($url)" to retrieve a URL. In this case, the URL is "http://www.hacker.org/challenge/misc/ma ... teps=DRRDD", plus the rest of the path on the end. Don't need PHP session, cookies, headers, or anything.fido2509 wrote:Hi,
I solved the maze manually with some support of pen and paper.
That way I chose because the server didn't answer correctly on my requests.
I used PHP with fsockopen(..). I included cookies, referer, user-agent, accept-encoding and all the headerlines firefox uses too. But instead of "keep walking.." and "boom" I got blank pages. with blank I mean HTTP Headers followed by <html><body>\r\n and thats all, meaning EOF($socket) occured.
Could someone please hint me how to get a usable answer?
I solved the maze already, but I want to know why my program didn't work.
Sources at LazyMaze.zip (3kB)
Oh, btw I used a simple DFS to get through.
Fido