-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathsolve.py
70 lines (47 loc) · 1.52 KB
/
solve.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/usr/bin/env python3
from pwn import log, remote, sys
movements = {
(0, 1): 'R',
(0, -1): 'L',
(1, 0): 'D',
}
final_path = ''
def dfs(root, maze, visited, path=''):
global final_path
if -1 in root or len(maze) <= root[0] or len(maze[0]) <= root[1]:
return
if maze[root[0]][root[1]] == '💎' and final_path == '':
final_path = path
visited.add(root)
for movement in [(1, 0), (0, -1), (0, 1)]:
node = (root[0] + movement[0], root[1] + movement[1])
if -1 in node or len(maze) <= node[0] or len(maze[0]) <= node[1]:
continue
if node not in visited and maze[node[0]][node[1]] != '☠️':
dfs(node, maze, visited.copy(), path + movements[movement])
def main():
global final_path
if len(sys.argv) != 2:
log.warning(f'Usage: python3 {sys.argv[0]} <host:port>')
exit(1)
host, port = sys.argv[1].split(':')
r = remote(host, int(port))
r.sendlineafter(b'> ', b'2')
prog = log.progress('Round')
for round in range(500):
prog.status(str(round + 1))
r.recvline()
maze = list(map(
lambda s: s.split()[1:-1],
r.recvuntil(b'\n\n').strip().decode().splitlines()
))[1:-1]
j = maze[0].index('🤖')
dfs((0, j), maze, {(0, j)})
r.sendlineafter(b'> ', final_path.encode())
final_path = ''
r.recvline()
prog.success(str(round + 1))
print(r.recv().decode())
r.close()
if __name__ == '__main__':
main()