Skip to content

Instantly share code, notes, and snippets.

@Atreyagaurav
Created January 30, 2020 12:03
Show Gist options
  • Save Atreyagaurav/f5322add30ef4c49da46269478f4d99e to your computer and use it in GitHub Desktop.
Save Atreyagaurav/f5322add30ef4c49da46269478f4d99e to your computer and use it in GitHub Desktop.
visualization for 2 rope problem
import math
import random
import time
def fun(x,a1=100,a2=50,a3=0):
y= math.fabs(x*(a2-x)*(a1-x)/(x+1) +random.random()*x*x*(100-x)/(x*x+2*x+1))
return y
def get_xy(num,a,b,c):
x=list(range(num))
y=[fun(i,a,b,c) for i in x]
y=[sum(y[:i]) for i in range(num)]
m=max(y)
y=[i/m for i in y]
return x,y
def get_rope_str(l,a,b):
if a+b >=l:
return '\u001b[30;1m'+'_'*(l+2)+'\u001b[0m'
else:
start= '\u001b[30;1m'+'_'*a+'\u001b[0m'
end='\u001b[30;1m'+'_'*b+'\u001b[0m'
mid= '\u001b[31m*\u001b[0m' + '='*(l-a-b) + '\u001b[31m*\u001b[0m'
return start+mid+end
def print_ropes(time,r1,r2):
print("TIME:",time)
print(r1)
print(r2)
def clean_ropes():
print('\u001b[3A\r',end="")
x,y= get_xy(1000,900,500,800)
x,z= get_xy(1000,200,700,900)
graph2={i:int(j*100) for i,j in zip(x,y)}
graph1={i:int(j*100) for i,j in zip(x,z)}
i=0
j=0
while True:
rope2=get_rope_str(100,graph2[i],100-graph2[999-i])
if '*' in rope2:
back= 100
else:
back=graph1[999-j]
j+=1
rope1=get_rope_str(100,graph1[i],100-back)
print_ropes(i,rope1,rope2)
clean_ropes()
time.sleep(0.05)
i+=1
if '*' not in rope1 and '*' not in rope2:
print_ropes(i,rope1,rope2)
break
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment