Skip to content

Instantly share code, notes, and snippets.

@ZengetsuFR
Created June 3, 2015 07:33
Show Gist options
  • Save ZengetsuFR/6ae39e32ae3263f9d25a to your computer and use it in GitHub Desktop.
Save ZengetsuFR/6ae39e32ae3263f9d25a to your computer and use it in GitHub Desktop.
Résolution du jeu "Défibrillateur" sur le site codingame.com
import sys, math, re
# Auto-generated code below aims at helping you parse
# the standard input according to the problem statement.
def deg2Rad(deg):
return deg * (math.pi/180)
def transformToFloat(value):
result = 0
if len(value)>0:
if re.match("^[0-9,]*$",value):
result = float(str.replace(value,",","."))
return result
def distance(latitude1,longitude1,latitude2,longitude2):
R = 6371
dLat = deg2Rad(latitude2 - latitude1)
dLong = deg2Rad(longitude2 - longitude1)
a = math.sin(dLat/2) * math.sin(dLat/2) + math.cos(deg2Rad(latitude1)) * math.cos(deg2Rad(latitude2)) * math.sin(dLong/2) * math.sin(dLong/2)
c = 2 * math.atan2(math.sqrt(a), math.sqrt(1-a))
d = R * c #distance en km
#miles = d * .6214
return d
LON = raw_input()
LAT = raw_input()
N = int(raw_input())
Long1 = transformToFloat(LON)
Lat1 = transformToFloat(LAT)
lstDistance = {}
d=[]
dicoF = {}
defLat = 0
defLong = 0
for i in xrange(N):
DEFIB = raw_input()
d = DEFIB.split(';')
#dico avec le nom du défibrillateur comme cle
dicoF[d[0]] = d[1]
itemLat = transformToFloat(d[len(d)-1])
itemLong = transformToFloat(d[len(d)-2])
lstDistance[d[0]]= distance(Lat1, Long1, itemLat, itemLong)
#la partie key=lambda x:x[1] permet de specifier que nous allons
#trier sur la 2ieme colonne
sortedList = sorted(lstDistance.items(), key=lambda x: x[1])
#dans sortedList la 1ere occurence est la valeur la plus proche
#il suffit de faire matcher cette donnée avec le dicoF pour afficher
#le nom du défibrillateur
print dicoF[sortedList[0][0]]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment