Sample Solving Algorhythm

This forum is for discussions related to the Runaway Robot puzzle
Post Reply
MaxistXXL
Posts: 13
Joined: Tue Nov 20, 2007 2:32 pm
Location: Earth

Sample Solving Algorhythm

Post by MaxistXXL »

Hi,
I was wondering, if you could post a programm like in the other challanges.
I wrote 750 lines of perl, but sometimes it wont find a path (currently at level 90...). I mean, thats live, no problem, but, aehm, debugging is extremely hard, if I dont know when it should find a path, and when it is correct, not to find one...

Maybe I'll write a brute-force-algo, but maybe you can offer a slightly better one :-D.

greetings:
der Maxist
War is terrorism, but with a higher budget.
MaxistXXL
Posts: 13
Joined: Tue Nov 20, 2007 2:32 pm
Location: Earth

Post by MaxistXXL »

Ok, i did it...
Its Perl, and it is fucking bruteforceing. Its made to show ONE solution, not all, so, if it needs some hours, its not a problem for me. Btw, if it would be more complex, noobs could steel it :-D.

If you just want to have a tool which gives you one solution for debugging, feel free to use this.
Dont forget to change YOURNAME and YOURPASSWORD...

Code: Select all

#!/usr/bin/perl
use strict;
use warnings;

#Benoetigte Variablen:
my @levelinfo;
my ($tempstring, $pfad);
my $i;
&website_auslesen("");

$i = 0;
$levelinfo[3] += 2;
$levelinfo[4] += 2;
$tempstring = "X"x$levelinfo[3];
for (1..($levelinfo[4]-2)){
 $tempstring .= "X".substr($levelinfo[0], $i, $levelinfo[3]-2)."L";
 $i += $levelinfo[3] - 2;
}
$tempstring .= "X"."L"x($levelinfo[3]-2)."X";
$levelinfo[0] = $tempstring;


$i = $levelinfo[2];
do{
 print "$i\n";
 $pfad = "."x$i;
 $tempstring = &bruteforce($pfad);
 $i++;
}while($tempstring eq "falsch");
print "$tempstring\n";

sub bruteforce{
 my $tempstring;
 my $pfad = $_[0];
 my $i = index($pfad, "\.");
 if($i > -1){
  substr($pfad, $i, 1) = "R";
  $tempstring = &bruteforce($pfad);
 }else{
   $tempstring = &verifizieren($pfad);
   if($tempstring eq "falsch"){return $tempstring;}
   else{return $pfad;}
 }
 if($tempstring ne "falsch"){return $tempstring;}
 else{
  substr($pfad, $i, 1) = "D";
  $tempstring = &bruteforce($pfad);
  return $tempstring;
 }
}

sub verifizieren{
 my ($mom_x, $mom_y) = (1, 1);
 my $zaehler = -1;
 do{
  $zaehler++;
  if($zaehler == length($pfad)){$zaehler = 0;}
  if(substr($_[0], $zaehler, 1) eq "R"){$mom_x++;}
  elsif(substr($_[0], $zaehler, 1) eq "D"){$mom_y++;}
 }while(substr($levelinfo[0], $mom_y*$levelinfo[3]+$mom_x, 1) eq ".");
 if(substr($levelinfo[0], $mom_y*$levelinfo[3]+$mom_x, 1) eq "X"){return "falsch";}
 else{return "richtig";}
}

sub website_auslesen {
 use LWP::Simple; #Packet, um den HTML-Inhalt auszulesen, initialisieren
  my $htmlinhalt = get("http://www.hacker.org/runaway/index.php?name=YOURNAME&password=YOURPASSWORD")
   || die "KANN WEBSITE NICHT OEFFNEN"; #auslesen des HTML-Inhaltes
  my $zahl = index($htmlinhalt, "FVterrainString=");
  my $zahl2 = index($htmlinhalt, "\"", $zahl) - $zahl;
 $htmlinhalt = substr($htmlinhalt, index($htmlinhalt, "FVterrainString="), $zahl2);
 @levelinfo = split("&", $htmlinhalt);
 foreach $zahl (0..$#levelinfo) {
  $levelinfo[$zahl] = substr($levelinfo[$zahl], (index($levelinfo[$zahl], "=") + 1));
 }
}
War is terrorism, but with a higher budget.
antirem
Posts: 10
Joined: Mon Dec 29, 2008 6:04 am

Post by antirem »

dyam.. thats a nice piece for that..


what level did you get to?
please dont use DD-WRT
Hacker - One who is proficient at using or programming a computer; a computer buff
Chocoholic
Posts: 44
Joined: Mon Feb 16, 2009 4:11 pm
Location: UK

Scheme

Post by Chocoholic »

Just in case someone is interested, here's a recursive backtracking algorithm written in Scheme.

That language is great, it's by far the most elegant one I know! It's dangerous though, if you have to write something in Java (or a similar language) later on, you easily get frustrated that everything is so complicated and the result is goddamn ugly.

As for the algorithm: it's a very simplistic version of what i'm using at the moment and implements no optimisation whatsoever. The playfield parameter is an array of booleans, where true means a blocked field. The rest should speak for itself.

Code: Select all

; solve by trial and error (backtracking)
(define (solve playfield w h minlen maxlen)
  (define (fail? solution full-solution x y)
    (cond ((null? solution)
           (fail? full-solution full-solution x y))
          ((or (>= x w) (>= y h)) #f) ; win
          ((vector-ref (vector-ref playfield y) x) #t) ; sucks
          ((= (car solution) 1)   ; 1 = D
           (fail? (cdr solution) full-solution x (+ y 1)))
          (else   ; 0 = 'R
           (fail? (cdr solution) full-solution (+ x 1) y))))
  (define (solve-iter solution n)
           (let ((rev (reverse solution)))
             (cond ((and (>= n minlen)
                         (not (fail? rev rev 0 0))) rev)
                   ((< n maxlen)
                    (or (solve-iter (cons 0 solution) (+ n 1))
                        (solve-iter (cons 1 solution) (+ n 1))))
                   (else #f))))
      (or (solve-iter '(0) 1) (solve-iter '(1) 1)))
Edith says: made code prettier
megabreit
Posts: 141
Joined: Sat Jan 03, 2009 3:33 pm

Post by megabreit »

Please don't "inspire" adum to introduce another programming language into the challenge arena. :shock:
The "boiling point" was Ada... but Scheme would be even stranger... it looks a bit like Lisp but with less brackets...
I mean... nothing against Scheme... it might perfectly fit your needs... but I simply won't see ANY programming language in the challenges :lol:

BTW: How long did your solver take? I'm using a similar backtrack approach (in another programming language) but without recursion. My solver took about 5 min to solve level 100... I just want to estimate the runtime.

Edit: No answer necessary... I read some more posts in the forum and know now what to expect. My solver is slowing down rapidly. But I also got some ideas on how to improve my solver... even though I don't think that I could make it as fast as gfoot's.
Chocoholic
Posts: 44
Joined: Mon Feb 16, 2009 4:11 pm
Location: UK

Post by Chocoholic »

megabreit wrote:Please don't "inspire" adum to introduce another programming language into the challenge arena. :shock:
The "boiling point" was Ada... but Scheme would be even stranger... it looks a bit like Lisp but with less brackets...
I mean... nothing against Scheme... it might perfectly fit your needs... but I simply won't see ANY programming language in the challenges :lol:
Actually... that's a FABULOUS idea! If some nasty Schemeish thing pops to my head in near future I'll mail it to adum. Thanks. :D

Oh and by the way scheme interpreters are muuuch easier to come by than an Ada compiler. Or well... maybe not, seen as both present in some linux distribution's repositories.
gfoot
Posts: 269
Joined: Wed Sep 05, 2007 11:34 pm
Location: Brighton, UK

Post by gfoot »

I thought "Cons Car" was in Scheme, but I know very little LISP so it doesn't make much difference to me.
mazetto
Posts: 1
Joined: Thu Apr 16, 2009 1:33 am
Location: indonesia
Contact:

Post by mazetto »

:(
megabreit
Posts: 141
Joined: Sat Jan 03, 2009 3:33 pm

Scheme vs. Lisp

Post by megabreit »

I'm not sure about the difference between Scheme and LISP. There was only one occasion in the past I had to deal with LISP and never saw Scheme in the wild. I solved "Cons Cars" with LISP documentation and interpreter, so I'm pretty sure that it was LISP... but why not ask the author (if known)?
blablaSTX
Posts: 11
Joined: Mon Aug 17, 2009 8:12 pm
Contact:

Post by blablaSTX »

scheme vs. Lisp:
Scheme is a lisp with guaranteed tail-call elimination, guaranteed continuation support and a few other nice semantic highlights. The syntax is lisp (aka functions with parens around). btw. the number and usage of parens is the same, so megabreit probably had too much of dope when looking at the code (;-). It is actually a lot of fun to program in scheme, but occasionally hard to read if you are not used to it. But every hacker should know it - the insight you gain into computing is really worth the effort (and continuations are a fantastic invention - look at the seaside web framework...)

recursive brute force backtracking:
brought me roughly to level 140 with reasonable (an hour or so) computation time (using smalltalk). then, a new algo was needed to "break the sound barrier".
I guess, your milage might vary, but somewhere between 100 and 150, everyone will encounter that limit - no matter which language and optimizations you use (hand tuning your c or asm code will only give you a few levels more - so it is not worth the effort)

edith: a typo
silvermane2
Posts: 1
Joined: Mon Apr 19, 2010 4:23 pm

Post by silvermane2 »

Yo what up community I need some help.....been with this since olden days with programming and ms dos....and back doors....
Post Reply