8 # check if board is all 0s
9 def winningboard(board):
15 # xors two boards, result is a new board (originals unchanged)
16 def xorboard(boardA, boardB):
18 for i in range(len(boardA)):
19 newBoard.append(boardA[i] != boardB[i])
22 # read a board from pipe, return representation with True/False
23 def readboard(pipe, dimension):
25 for _ in range(dimension):
26 line = pipe.recvline()
28 for field in line.split(b" "):
29 if field == b"0" or field == b"0\n":
31 elif field == b"1" or field == b"1\n":
34 print("assumption fail: ")
40 def bfs(currentBoard, moves, maxindex, depth):
41 for i in range(maxindex):
42 board = xorboard(currentBoard, moves[i][0])
44 if winningboard(board):
47 result = bfs(xorboard(currentBoard, moves[i][0]), moves, i, depth - 1)
54 fridge = pwnlib.tubes.remote.remote("ppc-fridge.ctfz.one", 31337)
56 # do all this once per stage
57 for stage in range(len(dimensions)):
58 dim = dimensions[stage]
60 before = readboard(fridge, dim)
61 # does all possible moves once, records the changes they make
64 move_str = "{0},{1}".format(x,y)
65 fridge.sendline(move_str)
66 after = readboard(fridge, dim)
67 moves.append((xorboard(before, after),move_str,))
69 # from the current board, find a solution
70 for depth in range(1, len(moves)):
71 print("SEARCH DEPTH: ", depth)
72 moves_list = bfs(before, moves, len(moves), depth)
73 if moves_list != None:
75 if moves_list == None:
76 print("NO MOVES FOUND")
77 print("BEFORE: ",before)
78 # apply moves, skipping the last one because it doesn't print a board
79 for i in moves_list[:-1]:
81 fridge.sendline(moves[i][1])
82 print(readboard(fridge, dim))
85 fridge.sendline(moves[-1][1])
86 print(fridge.recvline()) # this one is solved