Skip to content

Instantly share code, notes, and snippets.

@gamikun
Created June 29, 2016 18:19
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 gamikun/d1a8c918a187e8ca0156b6adea26f834 to your computer and use it in GitHub Desktop.
Save gamikun/d1a8c918a187e8ca0156b6adea26f834 to your computer and use it in GitHub Desktop.
# CodeFights perfectCity
def perfectCity(departure, destination):
import math
sx, sy = departure
tx, ty = destination
distances = []
for i in [math.floor, math.ceil]:
x = sx
y = sy
distance = 0
while 1:
dx, dy = 0, 0
if x == tx:
dy = ty - y
elif y == ty:
dx = tx - x
elif x % 1 != 0:
dx = i(x) - sx
elif y % 1 != 0:
dy = i(y) - sy
elif tx % 1 != 0:
dy = ty - y
elif ty % 1 != 0:
dx = tx - x
distance += abs(dx + dy)
x += dx
y += dy
if x == tx and y == ty:
break
distances.append(distance)
return min(distances)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment