Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- import socket
- from copy import deepcopy as cp
- from os import getpid
- fout = open('edgy.out.%d' % getpid(), 'w+')
- def send(fout, conn, text):
- conn.send(text)
- fout.write(text)
- fout.flush()
- def listreplace(l, find, replace):
- for i in xrange(len(l)):
- if l[i] in find:
- l[i] = replace
- return l
- def solvepuzzle(puzzle, startpos, starttestcaseorig, layercounterorig):
- starttestcase = cp(starttestcaseorig)
- layercounter = cp(layercounterorig)
- if layercounter != 0:
- # print layercounter
- layercounter -= 1
- for i in xrange(4):
- result = solvepuzzle(puzzle, startpos, starttestcase + [i], layercounter)
- if result != []:
- return result
- return []
- else:
- x,y = startpos
- tc = starttestcase
- # print 'Testing %s' % str(tc)
- first = True
- while True:
- if not first and x == startpos[0] and y == startpos[1]:
- return []
- else:
- first = False
- for direction in tc:
- if direction == 0:
- # go up
- y -= 1
- if puzzle[y][x] == 'x':
- # print 'Fails %s' % str(tc)
- return []
- elif puzzle[y][x] in ['-','|']:
- # print 'Works %s' % str(tc)
- return tc
- elif direction == 1:
- # go left
- x -= 1
- if puzzle[y][x] == 'x':
- # print 'Fails %s' % str(tc)
- return []
- elif puzzle[y][x] in ['-','|']:
- # print 'Works %s' % str(tc)
- return tc
- elif direction == 2:
- # go down
- y += 1
- if puzzle[y][x] == 'x':
- # print 'Fails %s' % str(tc)
- return []
- elif puzzle[y][x] in ['-','|']:
- # print 'Works %s' % str(tc)
- return tc
- elif direction == 3:
- # go right
- x += 1
- if puzzle[y][x] == 'x':
- # print 'Fails %s' % str(tc)
- return []
- elif puzzle[y][x] in ['-','|']:
- # print 'Works %s' % str(tc)
- return tc
- conn = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
- conn.connect(('edgy.2015.ghostintheshellcode.com', 44440))
- print conn.recv(4096).strip()
- send(fout, conn, 'EdgesAreFun\n')
- print conn.recv(4096).strip()
- print conn.recv(4096).strip()
- send(fout, conn, 'sd\n')
- while (1):
- puzzle = conn.recv(65534)
- #print puzzle
- instructions = ''
- if puzzle.splitlines()[0][0] != '-':
- print puzzle
- puzzle = conn.recv(4096)
- instructions = puzzle.splitlines()[-1]
- puzzlesplit = puzzle.splitlines()[:-1]
- puzzle = ''
- for x in puzzlesplit:
- puzzle += '%s\n' % x
- while puzzle.splitlines()[-1][-1] != '-' or len(puzzle.splitlines()[-1]) != len(puzzle.splitlines()[-2]):
- puzzle += conn.recv(4096)
- puzzle = puzzle.strip()
- print puzzle
- if instructions == '':
- instructions = conn.recv(4096).strip()
- print instructions
- nummoves = int(instructions.split(' ')[5])
- puzzle = map(list, puzzle.splitlines())
- startpos = []
- for i in xrange(len(puzzle)):
- for j in xrange(len(puzzle)):
- if puzzle[i][j] == '@':
- startpos = [j,i]
- solution = []
- for i in xrange(1, nummoves + 1):
- #print 'Trying %d moves' % i
- cursol = solvepuzzle(puzzle, startpos, [], i)
- if cursol != []:
- solution = cursol
- break
- solutionstr = ''
- for x in solution:
- if x == 0:
- solutionstr += 'w'
- elif x == 1:
- solutionstr += 'a'
- elif x == 2:
- solutionstr += 's'
- elif x == 3:
- solutionstr += 'd'
- print solutionstr
- send(fout, conn, '%s\n' % solutionstr)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement