Last active
August 29, 2015 14:16
-
-
Save shunkp/f036aa1169706c9d2b77 to your computer and use it in GitHub Desktop.
calculuswhyy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import socket | |
from fractions import gcd | |
from sympy import * | |
from sympy.parsing.sympy_parser import parse_expr | |
def gcd3(a,b,c): | |
return gcd(gcd(a,b),c) | |
def solve_x(eq1,eq2): | |
e1f = eq1.split(' ') | |
e2f = eq2.split(' ') | |
e1x = int(e1f[0][:-1],10) | |
e1y = int(e1f[2][:-1],10) | |
e1a = int(e1f[4],10) | |
e2x = int(e2f[0][:-1],10) | |
e2y = int(e2f[2][:-1],10) | |
e2a = int(e2f[4],10) | |
# solve first eq for y | |
e1g = gcd3(e1x,e1y,e1a) | |
e1x = e1x / e1g | |
e1y = e1y / e1g | |
e1a = e1a / e1g | |
e1x = ((e1x / e1y) * -1) | |
e1a = ((e1a / e1y)) | |
# sub into second eq | |
e2x2 = e2x + (e2y * e1x) | |
e2a2 = e2a + (-1 * (e2y * e1a)) | |
e2xf = e2a2 / e2x2 | |
e2yf = (e2a - (e2x * e2xf))/e2y | |
return [int(round(e2xf)), int(round(e2yf))] | |
def misc200(): | |
c = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
c.connect(('104.131.107.153', 12121)) | |
res = c.recv(1024).decode('utf-8') # first message | |
stage = 1 | |
i = 0 | |
while stage == 1: # addition | |
res = c.recv(1024).decode('utf-8').split(' ') | |
op1 = ''.join(res[-3:-2]) | |
op2 = ''.join(res[-1:])[:-1] | |
try: | |
ans = int(op1,10) + int(op2,10) | |
except ValueError: | |
stage = 2 | |
break | |
res = c.send(str(ans).encode('utf-8')) #answer | |
while stage == 2: # system of equations | |
res = c.recv(1024).decode('utf-8').split('\n') | |
if len(res) == 5: | |
eq1 = ''.join(res[1:2]) | |
eq2 = ''.join(res[2:3]) | |
else: | |
if res[0].split(' ')[0] == 'Thanks': | |
stage = 3 | |
break | |
res = c.recv(1024).decode('utf-8').split('\n') | |
eq1 = ''.join(res[0:1]) | |
eq2 = ''.join(res[1:2]) | |
ans = solve_x(eq1,eq2) | |
res = c.send(str(ans[0]).encode('utf-8')) | |
res = c.recv(1024).decode('utf-8').split('\n') | |
res = c.send(str(ans[1]).encode('utf-8')) | |
while stage == 3: # derivative function | |
res = c.recv(1024).decode('utf-8').split('\n') | |
if len(res) == 4: | |
xv = int(res[0].split('=')[1].replace(' ','').replace(':',''),10) | |
fx = res[1].split('=')[1].replace(' ','').replace('x^','*x**') | |
else: | |
try: | |
xv = int(res[0].split('=')[1].replace(' ','').replace(':',''),10) | |
except IndexError: | |
print('Flag: ', ''.join(res[0].split(' ')[-1:])) | |
break | |
res = c.recv(1024).decode('utf-8').split('\n') | |
fx = res[0].split('=')[1].replace(' ','').replace('x^','*x**') | |
x = Symbol('x') | |
y = parse_expr(fx) | |
yp = y.diff(x) | |
f = lambdify(x,yp,'numpy') | |
ans = f(xv) | |
res = c.send(str(ans).encode('utf-8')) | |
c.close() | |
misc200() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment