Advertisement
chaosagent

GITS edgy

Jan 18th, 2015
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.29 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. import socket
  4. from copy import deepcopy as cp
  5. from os import getpid
  6.  
  7. fout = open('edgy.out.%d' % getpid(), 'w+')
  8.  
  9. def send(fout, conn, text):
  10. conn.send(text)
  11. fout.write(text)
  12. fout.flush()
  13.  
  14. def listreplace(l, find, replace):
  15. for i in xrange(len(l)):
  16. if l[i] in find:
  17. l[i] = replace
  18. return l
  19.  
  20. def solvepuzzle(puzzle, startpos, starttestcaseorig, layercounterorig):
  21. starttestcase = cp(starttestcaseorig)
  22. layercounter = cp(layercounterorig)
  23. if layercounter != 0:
  24. # print layercounter
  25. layercounter -= 1
  26. for i in xrange(4):
  27. result = solvepuzzle(puzzle, startpos, starttestcase + [i], layercounter)
  28. if result != []:
  29. return result
  30. return []
  31. else:
  32. x,y = startpos
  33. tc = starttestcase
  34. # print 'Testing %s' % str(tc)
  35. first = True
  36. while True:
  37. if not first and x == startpos[0] and y == startpos[1]:
  38. return []
  39. else:
  40. first = False
  41. for direction in tc:
  42. if direction == 0:
  43. # go up
  44. y -= 1
  45. if puzzle[y][x] == 'x':
  46. # print 'Fails %s' % str(tc)
  47. return []
  48. elif puzzle[y][x] in ['-','|']:
  49. # print 'Works %s' % str(tc)
  50. return tc
  51. elif direction == 1:
  52. # go left
  53. x -= 1
  54. if puzzle[y][x] == 'x':
  55. # print 'Fails %s' % str(tc)
  56. return []
  57. elif puzzle[y][x] in ['-','|']:
  58. # print 'Works %s' % str(tc)
  59. return tc
  60. elif direction == 2:
  61. # go down
  62. y += 1
  63. if puzzle[y][x] == 'x':
  64. # print 'Fails %s' % str(tc)
  65. return []
  66. elif puzzle[y][x] in ['-','|']:
  67. # print 'Works %s' % str(tc)
  68. return tc
  69. elif direction == 3:
  70. # go right
  71. x += 1
  72. if puzzle[y][x] == 'x':
  73. # print 'Fails %s' % str(tc)
  74. return []
  75. elif puzzle[y][x] in ['-','|']:
  76. # print 'Works %s' % str(tc)
  77. return tc
  78.  
  79. conn = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  80. conn.connect(('edgy.2015.ghostintheshellcode.com', 44440))
  81. print conn.recv(4096).strip()
  82. send(fout, conn, 'EdgesAreFun\n')
  83. print conn.recv(4096).strip()
  84. print conn.recv(4096).strip()
  85. send(fout, conn, 'sd\n')
  86. while (1):
  87. puzzle = conn.recv(65534)
  88. #print puzzle
  89. instructions = ''
  90. if puzzle.splitlines()[0][0] != '-':
  91. print puzzle
  92. puzzle = conn.recv(4096)
  93. instructions = puzzle.splitlines()[-1]
  94. puzzlesplit = puzzle.splitlines()[:-1]
  95. puzzle = ''
  96. for x in puzzlesplit:
  97. puzzle += '%s\n' % x
  98. while puzzle.splitlines()[-1][-1] != '-' or len(puzzle.splitlines()[-1]) != len(puzzle.splitlines()[-2]):
  99. puzzle += conn.recv(4096)
  100. puzzle = puzzle.strip()
  101. print puzzle
  102. if instructions == '':
  103. instructions = conn.recv(4096).strip()
  104. print instructions
  105. nummoves = int(instructions.split(' ')[5])
  106. puzzle = map(list, puzzle.splitlines())
  107. startpos = []
  108. for i in xrange(len(puzzle)):
  109. for j in xrange(len(puzzle)):
  110. if puzzle[i][j] == '@':
  111. startpos = [j,i]
  112. solution = []
  113. for i in xrange(1, nummoves + 1):
  114. #print 'Trying %d moves' % i
  115. cursol = solvepuzzle(puzzle, startpos, [], i)
  116. if cursol != []:
  117. solution = cursol
  118. break
  119. solutionstr = ''
  120. for x in solution:
  121. if x == 0:
  122. solutionstr += 'w'
  123. elif x == 1:
  124. solutionstr += 'a'
  125. elif x == 2:
  126. solutionstr += 's'
  127. elif x == 3:
  128. solutionstr += 'd'
  129. print solutionstr
  130. send(fout, conn, '%s\n' % solutionstr)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement