-
Notifications
You must be signed in to change notification settings - Fork 0
/
cc9_2ksum_closest.py
48 lines (39 loc) · 1.42 KB
/
cc9_2ksum_closest.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
import requests
import time
cc_number = 9
url_input = 'https://cc.the-morpheus.de/challenges/' + str(cc_number) + '/'
url_output = 'https://cc.the-morpheus.de/solutions/' + str(cc_number) + '/'
def solve():
response = requests.get(url_input).json()
print('------------ Response of Server --------')
print(response)
print('----------------------------------------')
# TODO: programm your solution here
k = response['k']
liste = response['list']
def find_first_pair(k, liste):
pairs = []
for i1, m in enumerate(liste):
for i2, n in enumerate(liste):
if m + n == k:
pairs.append([i1, i2])
distance_list = []
for i in range(len(pairs)):
# distance = (distance between indices, index in pairs)
distance = (pairs[i][0] - pairs[i][1]).__abs__(), i
distance_list.append(distance)
distance_list = sorted(distance_list)
return pairs[distance_list[0][1]]
result = find_first_pair(k, liste)
print(result)
response2 = requests.post(url_output, json={'token': result})
print('------------Response 2 of Server -------')
print(response2.text)
print('----------------------------------------')
# Success: TMT{5umm3d17upr16h7d1dn7y4}
def benchmark(n):
start = time.time()
for i in range(n):
solve()
print((time.time() - start) / n)
benchmark(100)