• Sign Up
• Login
• API
• FAQ
• Tools
• Archive
daily pastebin goal
45%
SHARE
TWEET

# The Game - PCTF - iPw - TeamTasteless

a guest Apr 30th, 2012 102 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!
15. previous_answer = []
16. relations = {}
17. previous_answer.append('Nope')
18. previous_answer.append('Nope')
19.
20. def opensocket():
21.         s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
22.         s.connect((HOST, PORT))
23.         return s
24.
25. def senddata(s, answer=1):
26.         s.sendall(str(answer) + "\n")
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]:
41.                 if(x == current_answer[1]):
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.
52. def maprelations(relations, current_answer):
53.
54.         continuebool = 1
55.         try:
56.                 relations[current_answer[0]]
57.         except KeyError:
58.                 #print "No firstlevel neighbours"
59.                 continuebool = 0
60.                 return 0
61.         if(continuebool == 1):
62.                 for x in relations[current_answer[0]]['greater']:
63.                         if(x == current_answer[1]):
64.                                 #print "FOUND CHAIN!"
65.                                 return 1
66.                                 break
67.                         else:
68.                                 if(getnext(relations, x, 'greater', current_answer, 1) == 1):
69.                                         relations[current_answer[0]]['greater'].insert(0,current_answer[1])
70.                                         relations[current_answer[1]]['smaller'].insert(0,current_answer[0])
71.                                         return 1
72.                 for x in relations[current_answer[0]]['smaller']:
73.                         if(x == current_answer[1]):
74.                                 #print "FOUND CHAIN!"
75.                                 return 2
76.                                 break
77.                         else:
78.                                 if(getnext(relations, x, 'smaller', current_answer, 1) == 1):
79.                                         relations[current_answer[0]]['smaller'].insert(0,current_answer[1])
80.                                         relations[current_answer[1]]['greater'].insert(0,current_answer[0])
81.                                         return 2
82.                 #print "No n-level neighbours"
83.                 return 0
84.
85. def parseresponse(s, response, first=0):
86.         global previous_answer
87.         global relations
88.         current_answer = []
89.         current_answer.append(0)
90.         current_answer.append(0)
91.
92.         #print response
93.         response = response.split("\n")
94.
95.         if(previous_answer[0] == 'Nope'):
96.
97.                 current_answer[0] = response[1][11:]
98.                 current_answer[1] = response[2][11:]
99.
100.         elif(previous_answer[0] == 'Known'):
101.
102.                 current_answer[0] = response[4][11:]
103.                 current_answer[1] = response[5][11:]
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.
109.                 current_answer[0] = response[4][11:]
110.                 current_answer[1] = response[5][11:]
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:
121.                         relations[previous_answer[0]]
122.                 except KeyError:
123.                         relations[previous_answer[0]] = {}
124.                         relations[previous_answer[0]]['greater'] = []
125.                         relations[previous_answer[0]]['smaller'] = []
126.
127.                 try:
128.                         relations[previous_answer[1]]
129.                 except KeyError:
130.                         relations[previous_answer[1]] = {}
131.                         relations[previous_answer[1]]['greater'] = []
132.                         relations[previous_answer[1]]['smaller'] = []
133.
134.                 #print "[DEBUG] Saving New Relations!"
135.
136.                 if (bigger == 1):
137.                         #print "[DEBUG] 1 is bigger than 2!"
138.                         relations[previous_answer[0]]['greater'].insert(0,previous_answer[1])
139.                         relations[previous_answer[1]]['smaller'].insert(0,previous_answer[0])
140.                 else:
141.                         #print "[DEBUG] 1 is smaller than 2!"
142.                         relations[previous_answer[0]]['smaller'].insert(0,previous_answer[1])
143.                         relations[previous_answer[1]]['greater'].insert(0,previous_answer[0])
144.
145.         guess = maprelations(relations, current_answer)
146.
147.         if(guess>0):
148.                 previous_answer[0] = 'Known'
149.                 return guess
150.         else:
151.                 previous_answer = current_answer
152.                 return 1
153.
154. socket = opensocket()
155. result = parseresponse(socket, recdata(socket),1)
156.
157. x = 1 #Starting iteration!
158.
159. #Load old relations!
160. pkl_file = open('relations.pkl', 'rb')
161. relations = pickle.load(pkl_file)
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
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy.

Top