public
anonymous / stars.ml
Created

ocaml variant

  • Download Gist
stars.ml
OCaml
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
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);;

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.