-
Notifications
You must be signed in to change notification settings - Fork 1
/
solution.java
72 lines (65 loc) · 2.64 KB
/
solution.java
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package at.cnoize.codingame.defibrillators;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
/**
* CodinGame Classic Puzzle Easy - Defibrillators
* Copyright (C) 2016 Matthias 'Yolgie' Holzinger {@literal <[email protected]>}
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
class Solution {
private static class Location {
String name;
Double longitude;
Double latitude;
Double getDistance(Location otherLocation) {
Double x = (otherLocation.longitude - this.longitude) * Math.cos((this.latitude + otherLocation.latitude) / 2);
Double y = (otherLocation.latitude - this.latitude);
Double d = Math.sqrt(x * x + y * y) * 6371;
return d;
}
}
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
Location userLocation = new Location();
Double closestLocation = Double.MAX_VALUE;
userLocation.longitude = parseDouble(in.next());
userLocation.latitude = parseDouble(in.next());
int N = in.nextInt();
Map<Double, Location> locations = new HashMap<>(N);
in.nextLine();
for (int i = 0; i < N; i++) {
Location location = parseDefib(in.nextLine());
Double distance = userLocation.getDistance(location);
locations.put(distance, location);
if (distance < closestLocation) {
closestLocation = distance;
}
}
// To debug: System.err.println("Debug messages...");
System.out.println(locations.get(closestLocation).name);
}
private static Location parseDefib(String defib) {
Location location = new Location();
String[] defibValues = defib.split(";");
location.name = defibValues[1];
location.longitude = parseDouble(defibValues[4]);
location.latitude = parseDouble(defibValues[5]);
return location;
}
private static Double parseDouble(String string) {
return Double.parseDouble(string.replace(',', '.'));
}
}