Skip to content

Instantly share code, notes, and snippets.

@hiromu

hiromu/zombie.py Secret

Created May 18, 2014 22:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hiromu/6d1bf682dcaf2eb871e7 to your computer and use it in GitHub Desktop.
Save hiromu/6d1bf682dcaf2eb871e7 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
import re
import math
import time
import socket
G = 9.8
THRE = 50
RIFLE = 2
V_RIFLE = [853, 975, 800]
PISTOL = 3
V_PISTOL = [251, 350, 375]
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect(('zombies_8977dda19ee030d0ea35e97ad2439319.2014.shallweplayaga.me', 20689))
print sock.recv(1024)
sock.sendall('%s\n%s\n' % (RIFLE, PISTOL))
print sock.recv(1024)
while True:
for i in range(9):
res = sock.recv(1024)
print res
match = re.search('puppy ([0-9]+)m from your van and ([0-9]+)m above your van', res)
x = float(match.group(1))
y = float(match.group(2))
a = y / x
if x < THRE:
b = x * G / (2 * V_PISTOL[PISTOL - 1] ** 2)
else:
b = x * G / (2 * V_RIFLE[RIFLE - 1] ** 2)
v1 = - math.sqrt(1 / ((a + b) ** 2) - (4 * b) / (a + b)) - 1 / (a + b)
v2 = math.sqrt(4 + v1 ** 2)
v3 = (v1 + v2) / 2
ans = math.atan(v3) * 360 / math.pi
if x < THRE:
sock.sendall('p,%f,%f,%f\n' % (ans, x, y))
else:
sock.sendall('r,%f,%f,%f\n' % (ans, x, y))
while True:
T = 2
res = sock.recv(1024)
print res
match = re.search('The zombie is stalking a puppy across a hillside, starting at a distance of ([0-9]+)m from your van and ([0-9]+)m above it. The puppy is frozen in terror ([0-9]+)m from your van and ([0-9]+)m above your van.', res)
xz = float(match.group(1))
yz = float(match.group(2))
xp = float(match.group(3))
yp = float(match.group(4))
match = re.search('You have ([0-9]+) seconds before the zombie eats the puppy!', res)
tr = float(match.group(1))
x = xp + (xz - xp) * (tr - T) / tr
y = yp + (yz - yp) * (tr - T) / tr
a = y / x
if x < THRE:
b = x * G / (2 * V_PISTOL[PISTOL - 1] ** 2)
else:
b = x * G / (2 * V_RIFLE[RIFLE - 1] ** 2)
v1 = - math.sqrt(1 / ((a + b) ** 2) - (4 * b) / (a + b)) - 1 / (a + b)
v2 = math.sqrt(4 + v1 ** 2)
v3 = (v1 + v2) / 2
ans = math.atan(v3) * 360 / math.pi
while True:
res = sock.recv(1024)
print res
if str(T) in res:
if x < THRE:
sock.sendall('p,%f,%f,%f\n' % (ans, x, y))
else:
sock.sendall('r,%f,%f,%f\n' % (ans, x, y))
break
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment