## Tracking Santa

### December 24, 2010

Each year since 1955, Norad has tracked Santa Claus on his annual journey to deliver toys to good little girls and boys around the world. Since Santa must file his flight plan in advance, we already know where his journey will take him: the route at http://www.noradsanta.org/js/data.js is reproduced on the next page.

Your task is to calculate the number of miles that Santa will travel during his journey; you might find Wikipedia’s Great-circle distance page helpful. When you are finished, you are welcome to read or run a suggested solution, or to post your own solution or discuss the exercise in the comments below.

Advertisements

[…] today’s Programming Praxis, our task is to calculate the total distance traveled by Santa based on data […]

My Haskell solution (see http://bonsaicode.wordpress.com/2010/12/24/programming-praxis-tracking-santa/ for a version with comments):

Hey guys,

I’m new to Ruby so I did my best to throw this together in about 30 min…

My solution

Any help and comments are welcome!

With the file data.js saved in the same directory, having deleted the ‘var locations = ‘ bit at the beginning:

that’s 320,627 km to the metric-inclined.

A few days late, but here’s my Python

version. I went with the arctangent formula given by Wikiepdia, which it calls

“a more complicated formula that is accurate for all distances.” I also followed

Jebb’s lead of saving “data.js” with the

`var locations =`

portionremoved. This can be run (at least on my system) with

`./santa.py data.js`

.Actually I screwed up my first solution, this is correct I believe: http://pastebin.com/6maXGn80

Another Python version.

Download the data.js and save to santaflightpath.py. Edit the first line to remove “var “, so it starts with “location =”.

Remove the “;” from the last line. This creates a python compatible statement defining ‘locations’ to be a list of dictionaries. Importing ‘santaflightpath’ executes the code in the file, so there are clearly security implications.

pairwise is from the recipies in the itertools documentation.

Of interest, the flight plan is 28 hours long; stays within 600 miles of the north pole for the first 4 hours; doesn’t appear to visit Antarctica; and doesn’t return to the starting point.

from santaflightplan import locations

from math import asin, sin, cos, radians, sqrt

from itertools import starmap

from utils import pairwise

earth_radius = 3958.76 #average

def distance(p1, p2):

lat1, lng1 = map(radians, p1)

lat2, lng2 = map(radians, p2)

dlat = lat1 – lat2

dlng = lng1 – lng2

ang = 2*asin(sqrt(sin(dlat/2)**2 + cos(lat1)*cos(lat2)*sin(dlng/2)**2))

return ang * earth_radius

locs = ((float(p[‘lat’]),float(p[‘lng’])) for p in locations)

print sum(starmap(distance, pairwise(locs)))

[/sourcecode/

With a little delay, my commented Python version (looks like the one above http://www.gleocadie.net/?p=572&lang=en)