Skip to content

Instantly share code, notes, and snippets.

Created June 13, 2012 06:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/2922399 to your computer and use it in GitHub Desktop.
Save anonymous/2922399 to your computer and use it in GitHub Desktop.
ocaml variant
open ExtLib;;
let (|>) x f = f x
let (>>) f g x = g (f x)
let flip f x y = f y x
let mask = 1040384
let sq x = let t = Int64.of_int x in Int64.mul t t
let (++) = Int64.add ;;
let ic = open_in "stars.dat" in
let stars = Std.input_lines ic |> Enum.map (flip String.nsplit "\t" >> List.map int_of_string >> Array.of_list) in
let cells = Hashtbl.create 999 and sectors = Hashtbl.create 999 in
Enum.iter (fun a ->
let sector = ((a.(0) land mask) lsl 1) + ((a.(1) land mask) lsr 6) + ((a.(2) land mask) lsr 13) in
Hashtbl.add cells sector a;
Hashtbl.replace sectors sector ()) stars;
let nbr = [-1; 0; 1] in
let deltas = List.map (fun x -> List.map (fun y ->
List.map (fun z-> x * 16384 + y *128 + z) nbr ) nbr |> List.concat) nbr |> List.concat in
let (bestd, besta, bestb) = Hashtbl.fold (fun sector () best ->
let area_stars = List.map (fun delta -> Hashtbl.find_all cells (sector + delta)) deltas |> List.concat |> Array.of_list in
let (mind, _,_) = best in
let bst = ref best and n = Array.length area_stars and md = ref mind in
for i=0 to n-1 do
let a = area_stars.(i) in
for j=0 to i-1 do
let b = area_stars.(j) in
let d = sq (a.(0)-b.(0)) ++ sq (a.(1)-b.(1)) ++ sq (a.(2)-b.(2)) in
if d < !md then (md := d; bst := (d, a, b))
done
done;
!bst ) sectors (10000000000000L, [||], [||]) in
let show a = String.join " " (Array.map string_of_int a |> Array.to_list) in
Printf.printf "ok d=%f %s %s\n" (Int64.to_float bestd |> sqrt) (show besta) (show bestb);;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment