Posted: Wed Jun 16, 2010 3:09 pm
I solved it with pen and paper which actually wasn't that time-consuming once I knew some properties of the maze and correctly guessed where the exit might be.
Code: Select all
#!/usr/bin/python
# -*- coding: utf-8 -*-
import urllib, sys
from copy import deepcopy
sendto = "http://www.wechall.net/challenge/Z/blackhattale/login.php?action=login&"
cookie = 'PHPSESSID=%s;phpbb2mysql_data=%s;phpbb2mysql_sid=%s;phpbb2mysql_t=%s'
def get(url,cookie):
u=urllib.URLopener();u.addheader('Cookie',cookie);x = u.open(url)
return x.read()
def move(direction, cookie):
raw = get('http://www.hacker.org/challenge/misc/maze.php?steps='+direction, cookie)
raw = raw.split("<html><body>\n")
return raw[1].strip()
def makeMove(correct, cookie, currentPos):
lastStep = correct[-1]
for i in ['U', 'D', 'L', 'R']:
if (lastStep == 'U' and i == 'D') or (lastStep == 'D' and i == 'U') and (lastStep == 'L' and i == 'R') and (lastStep == 'R' and i == 'L'):
continue
tmp = correct + i
got = move(tmp, cookie)
if got != 'boom' and got != 'off the edge of the world':
if i == 'U':
c = [currentPos[0],currentPos[1]+1]
elif i == 'D':
c = [currentPos[0],currentPos[1]-1]
elif i == 'L':
c = [currentPos[0]-1,currentPos[1]]
elif i == 'R':
c = [currentPos[0]+1,currentPos[1]]
print("(%i|%i), %s\t%s" % (c[0], c[1], correct, got))
if got != 'keep moving...':
sys.exit()
makeMove(tmp, cookie, deepcopy(c))
print "Ok, service started"
makeMove('D', cookie, [0,0] )
Code: Select all
import requests
base_url = "http://www.hacker.org/challenge/misc/maze.php?steps="
def dfs(cur):
r = requests.get(base_url + cur);
print cur
if r.content.find('boom') >= 0:
return
elif r.content.find('keep moving') >= 0:
dfs(cur + 'L')
dfs(cur + 'R')
dfs(cur + 'D')
dfs(cur + 'U')
else:
print r.content
sys.exit()
Code: Select all
dfs('')
Code: Select all
url="http://www.hacker.org/challenge/misc/maze.php?steps="
def main(args):
dirs = 'ULDR'
path = ""
minX=0;maxX=0;posX=0
minY=0;maxY=0;posY=0
dir = ' '
difs = ((0,1),(-1,0),(0,-1),(1,0))
reached = {(posX,posY)}
response=urllib2.urlopen(url+"D")
body = response.read()
responses = {body}
responsefor = {"D"}
msg_cont = body
# mozna bude potreba IDA, abychom neminuli nejaky blizky cil,
# nejprve bych ale vyzkousel DFS
cont = True
statcnt=80
while len(path)>0 or cont:
statcnt+=1
if statcnt==100:
statcnt=0
for y in xrange(maxY,minY-1,-1):
msg=""
for x in xrange(minX,maxX+1):
msg=msg+'#.'[(x,y)in reached]
print msg
if cont:
ind=0
else:
dir = path[-1]
#print "backing "+path+"(%d,%d)" % (posX,posY)
path = path[:-1]
ind = dirs.index(dir)
posX-=difs[ind][0]
posY-=difs[ind][1]
ind +=1
cont = False
if ind<4:
path = path+dirs[ind]
posX+=difs[ind][0]
posY+=difs[ind][1]
if not (posX,posY) in reached:
if posX<minX:
minX=posX
if posX>maxX:
maxX=posX
if posY<minY:
minY=posY
if posY>maxY:
maxY=posY
responded = False
while not responded:
try:
response=urllib2.urlopen(url+path)
responded = True
finally:
if not responded:
statcnt+=1
if statcnt==100:
statcnt=0
print "Connection problems. State so far:\n"
for y in xrange(maxY,minY-1,-1):
msg=""
for x in xrange(minX,maxX+1):
msg=msg+'#.'[(x,y)in reached]
print msg
body = response.read()
if not body in responses:
responses = responses | {body}
responsefor = responsefor | {path}
print path+":\n"+body
if body == msg_cont:
cont = True
reached = reached | {(posX,posY)}
print responses
print responsefor
for y in xrange(maxY,minY-1,-1):
msg=""
for x in xrange(minX,maxX+1):
msg=msg+'#.'[(x,y)in reached]
print msg
I suppose the typo is just here, otherwise I would expect the code to loop forever.moose wrote:Code: Select all
if (lastStep == 'U' and i == 'D') or (lastStep == 'D' and i == 'U') and (lastStep == 'L' and i == 'R') and (lastStep == 'R' and i == 'L'): continue