{ }codelper
absolute valueP0017

Grid distance

On a grid of streets where you can only move horizontally or vertically, find the distance between two points.
The first line gives the first point x1 y1, and the second line gives the second point x2 y2, all integers.
Print the distance |x1 − x2| + |y1 − y2|.
If the points are the same, it is 0.

Input
-100,000,000 ≤ x1, y1, x2, y2 ≤ 100,000,000
Output
the distance on one line (0 or more)
Example 1 · stdin
1 5
4 1
stdout
7
Example 2 · stdin
-2 -3
-7 -9
stdout
11
main.py
Input (stdin)

When you run, these lines are fed to input()/stdin in order (the program doesn't stop to ask).

$ Ready

Open this on a computer to write and run code. On mobile you can read the problem and the solutions.

In other languagesShow solutions by language · 8 languages

Absolute value by language: Python abs() (built in), JS/TS Math.abs, Java Math.abs, C# Math.Abs, C++ std::abs (overloaded for integers and floating point), Craykon Mathf.Abs.
C has a different function per type — abs from <stdlib.h> for int, llabs for long long, fabs from <math.h> for double; using abs on a long long in C truncates it to int.
The most common trap is adding the differences first and taking one absolute value: in |(x2 − x1) + (y2 − y1)| differences in opposite directions cancel, so the first example gives 1 instead of 7.
Take the absolute value of each difference separately.
(Java's Math.abs(Integer.MIN_VALUE) overflows and stays negative, and C#'s Math.Abs(int.MinValue) throws OverflowException, but in this problem each difference is at most 200 million, so it is fine.)

Pythonmain.py · 3 lines
x1, y1 = map(int, input().split())x2, y2 = map(int, input().split())print(abs(x1 - x2) + abs(y1 - y2))
JavaScriptmain.js · 2 lines
const [x1, y1, x2, y2] = require("fs").readFileSync(0, "utf8").trim().split(/\s+/).map(Number);console.log(Math.abs(x1 - x2) + Math.abs(y1 - y2));
TypeScriptmain.ts · 3 lines
import { readFileSync } from "node:fs";const [x1, y1, x2, y2]: number[] = readFileSync(0, "utf8").trim().split(/\s+/).map(Number);console.log(Math.abs(x1 - x2) + Math.abs(y1 - y2));
JavaMain.java · 10 lines
import java.util.Scanner; public class Main {    public static void main(String[] args) {        Scanner sc = new Scanner(System.in);        int x1 = sc.nextInt(), y1 = sc.nextInt();        int x2 = sc.nextInt(), y2 = sc.nextInt();        System.out.println(Math.abs(x1 - x2) + Math.abs(y1 - y2));    }}
C#Main.cs · 13 lines
using System; class Program{    static void Main()    {        string[] a = Console.ReadLine().Split(' ', StringSplitOptions.RemoveEmptyEntries);        string[] b = Console.ReadLine().Split(' ', StringSplitOptions.RemoveEmptyEntries);        int x1 = int.Parse(a[0]), y1 = int.Parse(a[1]);        int x2 = int.Parse(b[0]), y2 = int.Parse(b[1]);        Console.WriteLine(Math.Abs(x1 - x2) + Math.Abs(y1 - y2));    }}
C++main.cpp · 8 lines
#include <cstdlib>#include <iostream> int main() {    int x1, y1, x2, y2;    std::cin >> x1 >> y1 >> x2 >> y2;    std::cout << std::abs(x1 - x2) + std::abs(y1 - y2) << '\n';}
Cmain.c · 9 lines
#include <stdio.h>#include <stdlib.h> /* 정수 abs()는 stdlib.h, 실수 fabs()는 math.h */ int main(void) {    int x1, y1, x2, y2;    scanf("%d %d %d %d", &x1, &y1, &x2, &y2);    printf("%d\n", abs(x1 - x2) + abs(y1 - y2));    return 0;}
Craykonmain.cray · 3 lines
a = Console.ReadNumbers();b = Console.ReadNumbers();Console.WriteLine(Mathf.Abs(a[0] - b[0]) + Mathf.Abs(a[1] - b[1]));