Browse Source

Solved remote_stars

Vsevolod Levitan 1 năm trước cách đây
mục cha
commit
83d9c6a85d
1 tập tin đã thay đổi với 40 bổ sung0 xóa
  1. 40 0
      remote_stars/main.py

+ 40 - 0
remote_stars/main.py

@@ -0,0 +1,40 @@
+import numpy as np
+import socket
+
+host = "84.237.21.36"
+port = 5152
+
+def rcvall(sock, n):
+    data = bytearray()
+    while len(data) < n:
+        pack = sock.recv(n - len(data))
+        data.extend(pack)
+    return data
+
+def square(b, y, x):
+    return b[y-1:y+2, x-1:x+2].flatten()
+
+def lmax(data):
+    pos1, pos2 = None, None
+    for y in range(1, data.shape[0] - 1):
+        for x in range(1, data.shape[1] - 1):
+            v = data[y, x]
+            if v < 3: continue
+            if any([n > v for n in square(data, y, x)]): continue
+            if pos1 is None: pos1 = (x, y)
+            elif pos2 is None: pos2 = (x, y)
+            else: break
+    return pos1, pos2
+
+with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
+    sock.connect((host, port))
+    for i in range(10):
+        sock.send(b"get")
+        rc = rcvall(sock, 40002)
+        img = np.frombuffer(rc[2:40002], dtype="uint8").reshape(200, 200)
+        pos1, pos2 = lmax(img)
+        res = np.sqrt((pos1[0] - pos2[0]) ** 2 + (pos1[1] - pos2[1]) ** 2)
+        sock.send(f"{res:.1f}".encode())
+        print(sock.recv(20).decode())
+    sock.send(b"beat")
+    print(sock.recv(20).decode())