forked from charlesfranciscodev/codingame
-
Notifications
You must be signed in to change notification settings - Fork 0
/
defibrillators.go
54 lines (45 loc) · 1.08 KB
/
defibrillators.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
50
51
52
53
54
package main
import (
"fmt"
"os"
"bufio"
"strings"
"strconv"
"math"
)
func main() {
scanner := bufio.NewScanner(os.Stdin)
scanner.Buffer(make([]byte, 1000000), 1000000)
var longitude string
scanner.Scan()
fmt.Sscan(scanner.Text(), &longitude)
var lon = toRadians(longitude)
var latitude string
scanner.Scan()
fmt.Sscan(scanner.Text(), &latitude)
var lat = toRadians(latitude)
var n int
scanner.Scan()
fmt.Sscan(scanner.Text(), &n)
var min = math.Inf(1)
var name = ""
for i := 0; i < n; i++ {
scanner.Scan()
defib := strings.Split(scanner.Text(), ";")
var defibLon = toRadians(defib[4])
var defibLat = toRadians(defib[5])
var x = (defibLon - lon) * math.Cos((lat + defibLat) / 2)
var y = defibLat - lat
var distance = math.Hypot(x, y) * 6371
if distance < min {
min = distance
name = defib[1]
}
}
fmt.Println(name)
}
// Convert degrees to radians
func toRadians(angle string) float64 {
var degrees, _ = strconv.ParseFloat(strings.Replace(angle, ",", ".", -1), 64)
return degrees * math.Pi / 180
}