Advertisement
Guest User

The Art of Approximation

a guest
Jun 16th, 2009
1,301
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.85 KB | None | 0 0
  1. import telnetlib
  2. import re
  3. import queue
  4.  
  5. PASS = b"MAZE4J002PLAY"
  6. CLIENT = telnetlib.Telnet("pwn2.ddtek.biz", 11511)
  7.  
  8. CLIENT.read_until(b"Password: ")
  9. CLIENT.write(PASS)
  10. print(CLIENT.read_until(b"Maze solutions must be presented as a single
  11. line of input.\n"))
  12.  
  13. def is_boundry(l):
  14.    return re.match(r"^\#+$", l)
  15.  
  16. def read_line():
  17.    return CLIENT.read_until(b"\n").decode().rstrip()
  18.  
  19. def read_board():
  20.    first_line = read_line()
  21.    if not is_boundry(first_line):
  22.        raise Exception("invalid board start: %s" % first_line)
  23.  
  24.    board = []
  25.    while True:
  26.        l = read_line()
  27.        if is_boundry(l):
  28.            break
  29.        else:
  30.            board.append(l)
  31.  
  32.    return board
  33.  
  34. WALL = "#"
  35. SPACE = "."
  36. START = "s"
  37. FINISH = "f"
  38.  
  39. def solve_board(board):
  40.    height = len(board)
  41.    width = len(board[0])
  42.  
  43.    print("Solving board:")
  44.    print("#" * width)
  45.    for l in board: print(l)
  46.    print("#" * width)
  47.  
  48.    for ypos, line in enumerate(board):
  49.        start_x = line.find(START)
  50.        if start_x != -1:
  51.            start_y = ypos
  52.            break
  53.  
  54.    visited = [[False]*width for i in range(height)]
  55.    sq = queue.Queue()
  56.    sq.put((start_x, start_y, ""))
  57.  
  58.    while sq:
  59.        posx, posy, moves = sq.get()
  60.        if posx < 0 or posx >= width or posy < 0 or posy >= height:
  61.            continue
  62.  
  63.        if visited[posy][posx] == True:
  64.            continue
  65.  
  66.        visited[posy][posx] = True
  67.  
  68.        if board[posy][posx] == FINISH:
  69.            print("Solution: %s" % moves)
  70.            return moves
  71.  
  72.        if board[posy][posx] == WALL:
  73.            continue
  74.  
  75.        sq.put((posx+1, posy, moves+"e"))
  76.        sq.put((posx-1, posy, moves+"w"))
  77.        sq.put((posx, posy+1, moves+"s"))
  78.        sq.put((posx, posy-1, moves+"n"))
  79.  
  80. while True:
  81.    b = read_board()
  82.    solution = solve_board(b)
  83.    CLIENT.write(solution.encode())
  84.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement