public
anonymous / stars.d
Created

FP(FP) June entry for thedeemon, v1

  • Download Gist
stars.d
D
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 34 35 36 37 38 39 40 41 42 43
 
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);
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.