-
Notifications
You must be signed in to change notification settings - Fork 0
/
d1p1.go
49 lines (41 loc) · 807 Bytes
/
d1p1.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package main
import (
"bufio"
"fmt"
"os"
"slices"
)
func main() {
scan := bufio.NewScanner(os.Stdin)
var idx int
var locations1 []int
var locations2 []int
var distances []int
var distanceSum int
for scan.Scan() {
var (
location1 int
location2 int
)
_, err := fmt.Sscanf(scan.Text(), "%d %d", &location1, &location2)
if err != nil {
fmt.Println("Error: ", err)
}
locations1 = append(locations1, location1)
locations2 = append(locations2, location2)
idx++
}
slices.Sort(locations1)
slices.Sort(locations2)
for i := 0; i < idx; i++ {
distance := locations2[i] - locations1[i]
if distance < 0 {
distance = -distance
}
distances = append(distances, distance)
}
for i := 0; i < idx; i++ {
distanceSum += distances[i]
}
fmt.Println(distanceSum)
}