paizaラーニングレベルアップ問題集の「二点間の距離」をやってみた。

paizaラーニングレベルアップ問題集の「二点間の距離」をやってみました。
- - -
##### 問題
###### STEP: 1
https://paiza.jp/works/mondai/array_primer/array_primer__array_distance_step1
###### STEP: 2
https://paiza.jp/works/mondai/array_primer/array_primer__array_distance_step2
- - -
##### C
###### STEP: 1
```c
#include <stdio.h>
#include <stdlib.h>
const int O[] = { 2, 3 };

int main() {
	int n;
	scanf("%d", &n);
	int P[n][2];
	for (int i = 0; i < n; i++) scanf("%d %d", &P[i][0], &P[i][1]);
	for (int i = 0; i < n; i++) printf("%d\n", abs(P[i][0] - O[0]) + abs(P[i][1] - O[1]));
	return 0;
}
```
###### STEP: 2
```c
#include <stdio.h>
#include <stdlib.h>

int main() {
	int n, a, b;
	scanf("%d %d %d", &n, &a, &b);
	a -= 1;
	b -= 1;
	int P[n][2];
	for (int i = 0; i < n; i++) scanf("%d %d", &P[i][0], &P[i][1]);
	printf("%d\n", abs(P[b][0] - P[a][0]) + abs(P[b][1] - P[a][1]));
	return 0;
}
```
- - -
##### C++
###### STEP: 1
```cpp
#include <iostream>
#include <vector>
#include <utility>
using namespace std;

const pair<int, int> O = { 2, 3 };

int main() {
	int n;
	cin >> n;
	vector<pair<int, int>> P(n);
	for (int i = 0; i < n; i++) cin >> P[i].first >> P[i].second;
	for (int i = 0; i < n; i++) cout << abs(P[i].first - O.first) + abs(P[i].second - O.second) << endl;
	return 0;
}
```
###### STEP: 2
```cpp
#include <iostream>
#include <vector>
#include <utility>
using namespace std;

const pair<int, int> O = { 2, 3 };

int main() {
	int n, a, b;
	cin >> n >> a >> b;
	a -= 1;
	b -= 1;
	vector<pair<int, int>> P(n);
	for (int i = 0; i < n; i++) cin >> P[i].first >> P[i].second;
	cout << abs(P[b].first - P[a].first) + abs(P[b].second - P[a].second) << endl;
	return 0;
}
```
- - -
##### C#
###### STEP: 1
```cs
using System;
using System.Linq;

class Program
{
	static readonly Tuple<int, int> O = new Tuple<int, int>(2, 3);
	
	static void Main()
	{
		int n = int.Parse(Console.ReadLine());
		Tuple<int, int>[] P = Enumerable.Range(0, n).Select(_ => Console.ReadLine().Split().Select(int.Parse).ToArray()).Select(arr => new Tuple<int, int>(arr[0], arr[1])).ToArray();
		int[] D = P.Select(p => Math.Abs(p.Item1 - O.Item1) + Math.Abs(p.Item2 - O.Item2)).ToArray();
		foreach (int d in D) Console.WriteLine(d);
	}
}
```
###### STEP: 2
```cs
using System;
using System.Linq;

class Program
{
	static void Main()
	{
		int[] nab = Console.ReadLine().Split().Select(int.Parse).ToArray();
		int n = nab[0], a = nab[1] - 1, b = nab[2] - 1;
		Tuple<int, int>[] P = Enumerable.Range(0, n).Select(_ => Console.ReadLine().Split().Select(int.Parse).ToArray()).Select(arr => new Tuple<int, int>(arr[0], arr[1])).ToArray();
		Console.WriteLine(Math.Abs(P[b].Item1 - P[a].Item1) + Math.Abs(P[b].Item2 - P[a].Item2));
	}
}
```
- - -
##### Go
###### STEP: 1
```go
package main
import (
	"fmt"
	"math"
)

var O = []int{ 2, 3 }

func main() {
	var n int
	fmt.Scan(&n)
	P := make([][]int, n)
	for i := 0; i < n; i++ {
		P[i] = make([]int, 2)
		fmt.Scan(&P[i][0], &P[i][1])
	}
	for i := 0; i < n; i++ {
		fmt.Println(int(math.Abs(float64(P[i][0] - O[0])) + math.Abs(float64(P[i][1] - O[1]))))
	}
}
```
###### STEP: 2
```go
package main
import (
	"fmt"
	"math"
)

func main() {
	var n, a, b int
	fmt.Scan(&n, &a, &b)
	a -= 1
	b -= 1
	P := make([][]int, n)
	for i := 0; i < n; i++ {
		P[i] = make([]int, 2)
		fmt.Scan(&P[i][0], &P[i][1])
	}
	fmt.Println(int(math.Abs(float64(P[b][0] - P[a][0])) + math.Abs(float64(P[b][1] - P[a][1]))))
}
```
- - -
##### Java
###### STEP: 1
```java
import java.util.*;
import java.util.stream.*;

public class Main {

	static final int[] O = { 2, 3 };
	
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		sc.nextLine();
		int[][] P = IntStream.range(0, n).mapToObj(i -> Arrays.stream(sc.nextLine().split(" ")).mapToInt(Integer::parseInt).toArray()).toArray(int[][]::new);
		sc.close();
		int[] D = Arrays.stream(P).mapToInt(p -> Math.abs(p[0] - O[0]) + Math.abs(p[1] - O[1])).toArray();
		for (int d : D) System.out.println(d);
	}
}
```
###### STEP: 2
```java
import java.util.*;
import java.util.stream.*;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int a = sc.nextInt() - 1;
		int b = sc.nextInt() - 1;
		sc.nextLine();
		int[][] P = IntStream.range(0, n).mapToObj(i -> Arrays.stream(sc.nextLine().split(" ")).mapToInt(Integer::parseInt).toArray()).toArray(int[][]::new);
		sc.close();
		System.out.println(Math.abs(P[b][0] - P[a][0]) + Math.abs(P[b][1] - P[a][1]));
	}
}
```
- - -
##### JavaScript
###### STEP: 1
```js
const O = [ 2, 3 ];
const [n, ...P] = require("fs").readFileSync("/dev/stdin", "utf8").trim().split('\n').map((s, i) => i ? s.split(' ').map(Number) : Number(s));
const D = P.map(p => Math.abs(p[0] - O[0]) + Math.abs(p[1] - O[1]));
for (var i = 0; i < n; i++) console.log(D[i]);
```
###### STEP: 2
```js
let [[n, a, b], ...P] = require("fs").readFileSync("/dev/stdin", "utf8").trim().split('\n').map((s, i) => s.split(' ').map(Number));
a -= 1;
b -= 1;
console.log(Math.abs(P[b][0] - P[a][0]) + Math.abs(P[b][1] - P[a][1]));
```
- - -
##### Kotlin
###### STEP: 1
```kotlin
fun main() {
	val O = arrayOf(2, 3)
	val n = readLine()!!.toInt()
	val P = Array(n) { readLine()!!.split(' ').map { it.toInt() } }
	val D = P.map { Math.abs(it[0] - O[0]) + Math.abs(it[1] - O[1]) }
	for (d in D) println(d)
}
```
###### STEP: 2
```kotlin
fun main() {
	var (n, a, b) = readLine()!!.split(' ').map { it.toInt() }
	a -= 1
	b -= 1
	val P = Array(n) { readLine()!!.split(' ').map { it.toInt() } }
	println(Math.abs(P[b][0] - P[a][0]) + Math.abs(P[b][1] - P[a][1]))
}
```
- - -
##### PHP
###### STEP: 1
```php
<?php
	$O = [ 2, 3 ];
	$n = intval(fgets(STDIN));
	$P = [];
	for ($i = 0; $i < $n; $i++) $P[] = array_map("intval", explode(' ', trim(fgets(STDIN))));
	$D = array_map(fn($p) => abs($p[0] - $O[0]) + abs($p[1] - $O[1]), $P);
	foreach ($D as $d) echo $d, PHP_EOL;
?>
```
###### STEP: 2
```php
<?php
	[$n, $a, $b] = array_map("intval", explode(' ', trim(fgets(STDIN))));
	$a -= 1;
	$b -= 1;
	$P = [];
	for ($i = 0; $i < $n; $i++) $P[] = array_map("intval", explode(' ', trim(fgets(STDIN))));
	echo abs($P[$b][0] - $P[$a][0]) + abs($P[$b][1] - $P[$a][1]), PHP_EOL;
?>
```
- - -
##### Perl
###### STEP: 1
```pl
my @O = (2, 3);
my $n = int(<STDIN>);
my @P;
for (my $i = 0; $i < $n; $i++) {
	push @P, [map { int($_) } split ' ', <STDIN>];
}
my @D = map { abs($_->[0] - $O[0]) + abs($_->[1] - $O[1]) } @P;
foreach (@D) {
	print "$_$/";
}
```
###### STEP: 2
```pl
my ($n, $a, $b) = map { int($_) } split ' ', <STDIN>;
$a -= 1;
$b -= 1;
my @P;
for (my $i = 0; $i < $n; $i++) {
	push @P, [map { int($_) } split ' ', <STDIN>];
}
print abs($P[$b][0] - $P[$a][0]) + abs($P[$b][1] - $P[$a][1]), $/;
```
- - -
##### Python3
###### STEP: 1
```py
O = (2, 3)
n = int(input())
P = [tuple(map(int, input().split())) for _ in range(n)]
D = list(map(lambda p: abs(p[0] - O[0]) + abs(p[1] - O[1]), P))
for d in D:
	print(d)
```
```py
print(*list(map(lambda p: abs(p[0] - 2) + abs(p[1] - 3), [tuple(map(int, input().split())) for _ in range(int(input()))])), sep='\n')
```
###### STEP: 2
```py
n, a, b = map(int, input().split())
a -= 1
b -= 1
P = [tuple(map(int, input().split())) for _ in range(n)]
print(abs(P[b][0] - P[a][0]) + abs(P[b][1] - P[a][1]))
```
- - -
##### Ruby
###### STEP: 1
```rb
O = [ 2, 3 ]
N = gets.to_i
P = N.times.map { gets.split.map(&:to_i) }
D = P.map { |p| (p[0] - O[0]).abs + (p[1] - O[1]).abs }
D.each do |d|
	p d
end
```
###### STEP: 2
```rb
N, a, b = gets.split.map(&:to_i)
a -= 1
b -= 1
P = N.times.map { gets.split.map(&:to_i) }
p (P[b][0] - P[a][0]).abs + (P[b][1] - P[a][1]).abs
```
- - -
##### Scala
###### STEP: 1
```scala
import scala.io.StdIn._

object Main extends App{
	val O = Array(2, 3)
	val n = readInt()
	val P = (0 until n).map { _ => readLine().split(' ').map { _.toInt } }
	val D = P.map { p => Math.abs(p(0) - O(0)) + Math.abs(p(1) - O(1)) }
	for (d <- D) println(d)
}
```
###### STEP: 2
```scala
import scala.io.StdIn._

object Main extends App{
	var Array(n, a, b) = readLine().split(' ').map { _.toInt }
	a -= 1
	b -= 1
	val P = (0 until n).map { _ => readLine().split(' ').map { _.toInt } }
	println(Math.abs(P(b)(0) - P(a)(0)) + Math.abs(P(b)(1) - P(a)(1)))
}
```
- - -
##### Swift
###### STEP: 1
```swift
let O = (2, 3)
let n = Int(readLine()!)!
var P: [(Int, Int)] = []
for _ in 0..<n {
	let p = readLine()!.split(separator: " ").compactMap { Int($0) }
	P.append((p[0], p[1]))
}
let D = P.map { abs($0.0 - O.0) + abs($0.1 - O.1) }
for d in D {
	print(d)
}
```
###### STEP: 2
```swift
let nab = readLine()!.split(separator: " ").compactMap { Int($0) }
let (n, a, b) = (nab[0], nab[1] - 1, nab[2] - 1)
var P: [(Int, Int)] = []
for _ in 0..<n {
	let p = readLine()!.split(separator: " ").compactMap { Int($0) }
	P.append((p[0], p[1]))
}
print(abs(P[b].0 - P[a].0) + abs(P[b].1 - P[a].1))
```