Teleportation

February 2018 Bronze - Problem 1: teleport

Algorithm

We can move manure in two ways: a->b or a->x->y->b (where a<=b and x<=y). We calculate the distance by each path and return the smaller distance.

Python

def run(a,b,x,y):
    a,b=sorted([a,b])
    x,y=sorted([x,y])
    path1 = b-a
    path2 = abs(a-x)+abs(b-y)
    return min(path1,path2)

def main():
    with open('teleport.in','r') as fIn:
        a,b,x,y = map(int,fIn.read().split())
    res = run(a,b,x,y)
    with open('teleport.out','w') as fOut:
        fOut.write(str(res)+'\n')

if __name__ == '__main__': main()