Created
June 29, 2016 18:19
-
-
Save gamikun/d1a8c918a187e8ca0156b6adea26f834 to your computer and use it in GitHub Desktop.
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
# 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