pastebin - collaborative debugging

pastebin is a collaborative debugging tool allowing you to share and modify code snippets while chatting on IRC, IM or a message board.

This site is developed to XHTML and CSS2 W3C standards. If you see this paragraph, your browser does not support those standards and you need to upgrade. Visit WaSP for a variety of options.

Python pastebin - collaborative debugging tool View Help


Posted by The Art of Approximation on Wed 17 Jun 01:39
report abuse | View followups from Anonymous | download | new post

  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())

Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:

To highlight particular lines, prefix each line with @@


Remember me so that I can delete my post