Skip to content

Instantly share code, notes, and snippets.

Created June 9, 2012 19:26
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/2902247 to your computer and use it in GitHub Desktop.
Save anonymous/2902247 to your computer and use it in GitHub Desktop.
FP(FP) June entry for thedeemon, v1
module main;
import std.stdio, std.algorithm, std.array, std.conv;
immutable mask = 0b11111110000000000000;
alias int[] star;
void main(string[] argv)
{
star[][int] cells;
foreach(s; File("stars.dat", "rt").byLine()) {
int[] coords = s.split().map!(to!int).array();
int sector = ((coords[0] & mask) << 1) + ((coords[1] & mask) >> 6) + ((coords[2] & mask) >> 13);
auto a = cells.get(sector, []);
a ~= coords;
cells[sector] = a;
}
auto nbr = [-1, 0, 1];
int[] deltas = [];
foreach(x; nbr) foreach(y; nbr) foreach(z; nbr) deltas ~= (x << 14) + (y << 7) + z;
long mind = 10^^13;
star besta=[], bestb=[];
foreach(cell; cells.byKey()) {
star[] area = [];
foreach(delta; deltas) {
int sector = cell + delta;
if (sector in cells)
area ~= cells[sector];
}
if (area.length > 1) {
foreach(a; area)
foreach(b; area)
if (a !is b) {
long d = (a[0]-b[0])^^2 + (a[1]-b[1])^^2 + (a[2]-b[2])^^2;
if (d < mind) {
mind = d;
besta = a; bestb = b;
}
}
}
}
writeln(std.math.sqrt(to!real(mind)), besta, bestb);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment