loading...

Daily Challenge #289 - Manhattan Distance

thepracticaldev profile image dev.to staff ・1 min read

The distance formula can be used to find the distance between two points. What if we were trying to walk from point A to point B, but there were buildings in the way? We would need some other formula, but which?

Manhattan Distance
Manhattan distance is the distance between two points in a grid (like the grid-like street geography of the New York borough of Manhattan) calculated by only taking a vertical and/or horizontal path.

Write a function manhattanDistance that accepts two points, pointA and pointB, and returns the Manhattan Distance between the two points.

pointA and pointB are arrays containing the x and y coordinate in the grid. You can think of x as the row in the grid, and y as the column.

Examples:
manhattanDistance( [1, 1], [1, 1] ) // => returns 0
manhattanDistance( [5, 4], [3, 2] ) // => returns 4
manhattanDistance( [1, 1], [0, 3] ) // => returns 3

Tests:
manhattanDistance( [1,2], [1,3] )
manhattanDistance( [5,2], [7,3] )
manhattanDistance( [1,2], [4,4] )

Good luck!


This challenge comes from xDranik on CodeWars. Thank you to CodeWars, who has licensed redistribution of this challenge under the 2-Clause BSD License!

Want to propose a challenge idea for a future post? Email yo+challenge@dev.to with your suggestions!

Posted on by:

thepracticaldev profile

dev.to staff

@thepracticaldev

The hardworking team behind dev.to ❤️

Discussion

markdown guide
 

JavaScript

const manhattan = ([x1, y1], [x2, y2]) => Math.abs(x2-x1) + Math.abs(y2-y1)
 

Progress 4GL (aka OpenEdge ABL)

FUNCTION manhattan RETURNS INTEGER
  ( x1 AS INTEGER, y1 AS INTEGER
  , x2 AS INTEGER, y2 AS INTEGER):

  RETURN ABS(x2 - x1) + ABS(y2 - y1).

END FUNCTION. 

MESSAGE manhattan(5,2,7,3)
  VIEW-AS ALERT-BOX INFORMATION BUTTONS OK.
 

Rust

lol generics

use core::ops::{Add, Sub};
fn abs_difference<T>(x: T, y: T) -> T
where
    T: Sub<Output = T> + Ord,
{
    if x < y {
        y - x
    } else {
        x - y
    }
}

struct Point<T>(T, T);

fn manhattan<T>(Point(x1, y1): Point<T>, Point(x2, y2): Point<T>) -> T
where
    T: Sub<Output = T> + Add<Output = T> + Ord,
{
    abs_difference(x2, x1) + abs_difference(y2, y1)
}

fn main() {
    assert_eq!(manhattan(Point(1, 1), Point(1, 1)), 0);
    assert_eq!(manhattan(Point(5, 4), Point(3, 2)), 4);
    assert_eq!(manhattan(Point(1, 1), Point(0, 3)), 3);

    dbg!(manhattan(Point(1, 2), Point(1, 3)));
    dbg!(manhattan(Point(5, 2), Point(7, 3)));
    dbg!(manhattan(Point(1, 2), Point(4, 4)));
}
 

Here is the simple solution with Python:

def manhattan_distance(pointA, pointB):
    return abs(pointA[0] - pointB[0]) + abs(pointA[1] - pointB[1])
 

I'm sure a most simpler solution exist but:

function calculateDistance(a, b) {
    return a.map((d1, index) => {
        return Math.abs(d1 - b[index])
    }).reduce((acc, curr) => acc + curr);
}
 

Python

def manhattan_distance(x1, y1, x2, y2):
      distance = abs(x2 - x1) + abs(y2 - y1)
      return distance

print(manhattan_distance(5,2,7,3))