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
Error List00F8 ↓ · ⇧F8 ↑
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.)