Want more features on Pastebin? Sign Up, it's FREE!
Guest

Programming Praxis Tracking Santa Revised

By: a guest on Dec 26th, 2010  |  syntax: Ruby  |  size: 1.46 KB  |  views: 49  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #!/usr/bin/env ruby
  2. require 'net/http'
  3.  
  4. def law_of_cosine(lat1, long1, lat2, long2)
  5.   Math.acos( (Math.sin(lat1)*Math.sin(lat2)) + (Math.cos(lat1)*Math.cos(lat2)) * Math.cos((long1-long2).abs))
  6. end
  7.  
  8. def great_circle_distance(rads)
  9.   6371.01 * rads
  10. end
  11.  
  12. santas_distance = 0
  13. d = { "firstlat" => 0, "firstlng" => 0, "secondlat" => 0, "secondlng" => 0}
  14. url = URI.parse('http://www.noradsanta.org/js/data.js')
  15. req = Net::HTTP::Get.new(url.path)
  16. resp = Net::HTTP.start(url.host, url.port) {|http|
  17.   http.request(req)
  18. }
  19.  
  20. resp.body.each do |line|
  21.   line.chomp
  22.   puts line
  23.   if line.include? "lat" and d['firstlat'] == 0
  24.     arr = line.map {|x| x[/[0-9]*\.[0-9]+|[0-9]+/]}
  25.     d['firstlat'] = arr[0].to_i
  26.   elsif line.include? "lat"
  27.     arr = line.map {|x| x[/[0-9]*\.[0-9]+|[0-9]+/]}
  28.     d['secondlat'] = arr[0].to_i
  29.   end
  30.   if line.include? "lng" and d['firstlng'] == 0
  31.     arr = line.map {|x| x[/[0-9]*\.[0-9]+|[0-9]+/]}
  32.     d['firstlng'] = arr[0].to_i
  33.   elsif line.include? "lng"
  34.     arr = line.map {|x| x[/[0-9]*\.[0-9]+|[0-9]+/]}
  35.     d['secondlng'] = arr[0].to_i
  36.   end
  37.   if d['firstlat'] != 0 and d['secondlat'] != 0 and d['firstlng'] != 0 and d['secondlng'] != 0
  38.     rads = law_of_cosine(d['firstlat'], d['firstlng'], d['secondlat'], d['secondlng'])
  39.     santas_distance += great_circle_distance(rads)
  40.     d['firstlat'] = d['secondlat']
  41.     d['firstlng'] = d['secondlng']
  42.     d['secondlat'] = 0
  43.     d['secondlng'] = 0
  44.   end
  45. end
  46. puts santas_distance
clone this paste RAW Paste Data