# The Game - PCTF - iPw - TeamTasteless

a guest
Apr 30th, 2012
144
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. '''
2.
3.     PCTF - The Game solution by iPw for Tasteless!
4.
5. '''
6.
7.
8. import socket
9. import pickle
10.
11. HOST = '23.22.16.34'    # The remote host
12. PORT = 6969              # The same port as used by the server
13.
14. # Too lazy to be consistent, just gave some variables their default values here!
16. relations = {}
19.
20. def opensocket():
21.     s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
22.     s.connect((HOST, PORT))
23.     return s
24.
27.
28. def recdata(s):
29.     data = []
30.     data.append(s.recv(256))
31.     data.append(s.recv(256))
32.     data = ''.join(data)
33.     #print data
34.     return data
35.
36. def getnext(relations, curr, type, current_answer, level):
37.     if not relations[curr][type]:
38.         #print "No level " + str(level) + " neighbours!"
39.         return 0
40.     for x in relations[curr][type]:
42.             #print "FOUND CHAIN!"
43.             return 1
44.             break
45.         else:
46.             if(level < 6)#Make the longest chains 5!
47.                 if(getnext(relations, x, type, current_answer, (level+1)) == 1):
48.                     return 1
49.                     break
50.     return 0
51.
53.
54.     continuebool = 1
55.     try:
57.     except KeyError:
58.         #print "No firstlevel neighbours"
59.         continuebool = 0
60.         return 0
61.     if(continuebool == 1):
64.                 #print "FOUND CHAIN!"
65.                 return 1
66.                 break
67.             else:
68.                 if(getnext(relations, x, 'greater', current_answer, 1) == 1):
71.                     return 1
74.                 #print "FOUND CHAIN!"
75.                 return 2
76.                 break
77.             else:
78.                 if(getnext(relations, x, 'smaller', current_answer, 1) == 1):
81.                     return 2
82.         #print "No n-level neighbours"
83.         return 0
84.
85. def parseresponse(s, response, first=0):
87.     global relations
91.
92.     #print response
93.     response = response.split("\n")
94.
96.
99.
101.
104.         print "\n+++++++++++++++++\nSanity check: We were - '" + str(response[0]) + ":" + response[1] + "'\n+++++++++++++++++\n"
105.         #Sanity check, we should not have to use this to get info!
106.
107.     else:
108.
111.         #Now we put the answer to memory!
112.         #print "Current record is: '" + response[3] + "'!"
113.         #print "Previous guess was - '" + response[1] + "'"
114.         print  "\n+++++++++++++++++\nHad to guess, and I guessed: " + response[1] + "\n+++++++++++++++++\n"
115.         if(response[1] == 'Wrong :('):
116.             bigger = 0
117.         else:
118.             bigger = 1
119.
120.         try:
122.         except KeyError:
126.
127.         try:
129.         except KeyError:
133.
134.         #print "[DEBUG] Saving New Relations!"
135.
136.         if (bigger == 1):
137.             #print "[DEBUG] 1 is bigger than 2!"
140.         else:
141.             #print "[DEBUG] 1 is smaller than 2!"
144.
146.
147.     if(guess>0):
149.         return guess
150.     else:
152.         return 1
153.
154. socket = opensocket()
155. result = parseresponse(socket, recdata(socket),1)
156.
157. x = 1 #Starting iteration!
158.
160. pkl_file = open('relations.pkl', 'rb')
162. pkl_file.close()
163.
164. #print relations
165.
166. try:
167.     while 1:
168.         print "[DEBUG] Iteration:" + str(x)
169.         senddata(socket, result)
170.         result = parseresponse(socket, recdata(socket))
171.         x=x+1
172.
173. except KeyboardInterrupt:
174.     #Save relations!
175.     output = open('relations.pkl', 'wb')
176.     pickle.dump(relations, output)
177.     output.close()
178.     socket.close()
RAW Paste Data