WIP: implement part 1
This commit is contained in:
parent
fa796b5331
commit
47a8eaa38c
1 changed files with 41 additions and 55 deletions
|
|
@ -51,92 +51,78 @@ pub fn run(allocator: std.mem.Allocator) !void {
|
|||
};
|
||||
const junction_box = JunctionBox{
|
||||
.index = junction_box_index,
|
||||
.position = .{ .x = x, .y = y, .z = z },
|
||||
.position = .{ .v = .{ x, y, z } },
|
||||
};
|
||||
try junction_boxes.append(allocator, junction_box);
|
||||
}
|
||||
|
||||
var network_id: usize = 0;
|
||||
while (true) {
|
||||
var shortest_distance: ?u32 = null;
|
||||
var a: ?*JunctionBox = null;
|
||||
var b: ?*JunctionBox = null;
|
||||
for (junction_boxes.items, 0..) |*junction_box, i| {
|
||||
for (junction_boxes.items[i + 1..]) |*other_junction_box| {
|
||||
if (junction_box.network != null and other_junction_box.network != null) {
|
||||
if (junction_box.network == other_junction_box.network) continue;
|
||||
}
|
||||
var possible_connections: std.ArrayList(Connection) = .empty;
|
||||
defer possible_connections.deinit(allocator);
|
||||
var connection_index: usize = 0;
|
||||
for (junction_boxes.items, 0..) |junction_box, i| {
|
||||
for (junction_boxes.items[i + 1..]) |other_junction_box| {
|
||||
defer connection_index += 1;
|
||||
|
||||
const distance = junction_box.position.distance(other_junction_box.position);
|
||||
if (shortest_distance == null or distance < shortest_distance.?) {
|
||||
shortest_distance = distance;
|
||||
a = junction_box;
|
||||
b = other_junction_box;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
std.debug.print("shortest distance = {?d} between {?f} and {?f}\n", .{shortest_distance, a, b});
|
||||
|
||||
if (a != null and b != null) {
|
||||
if (a.?.network != null and b.?.network == null) {
|
||||
std.debug.print("assigning network {?d} to {?f}\n", .{a.?.network, b});
|
||||
b.?.network = a.?.network;
|
||||
} else if (b.?.network != null and a.?.network == null) {
|
||||
std.debug.print("assigning network {?d} to {?f}\n", .{b.?.network, a});
|
||||
a.?.network = b.?.network;
|
||||
} else {
|
||||
std.debug.print("assigning network {d} to {?f} and {?f}\n", .{network_id, a, b});
|
||||
a.?.network = network_id;
|
||||
b.?.network = network_id;
|
||||
network_id += 1;
|
||||
}
|
||||
} else if (a) |junction_box| {
|
||||
junction_box.network = network_id;
|
||||
network_id += 1;
|
||||
} else if (b) |junction_box| {
|
||||
junction_box.network = network_id;
|
||||
network_id += 1;
|
||||
}
|
||||
|
||||
if (network_id >= 20) {
|
||||
break;
|
||||
const length = junction_box.position.distance(other_junction_box.position);
|
||||
const connection = Connection{
|
||||
.index = connection_index,
|
||||
.from = junction_box,
|
||||
.to = other_junction_box,
|
||||
.length = length,
|
||||
};
|
||||
try possible_connections.append(allocator, connection);
|
||||
}
|
||||
}
|
||||
|
||||
for (junction_boxes.items) |junction_box| {
|
||||
std.debug.print("{f}\n", .{junction_box});
|
||||
}
|
||||
|
||||
for (possible_connections.items) |connection| {
|
||||
std.debug.print("{f}\n", .{connection});
|
||||
}
|
||||
}
|
||||
|
||||
const Connection = struct {
|
||||
index: usize,
|
||||
from: JunctionBox,
|
||||
to: JunctionBox,
|
||||
length: u32,
|
||||
network: ?usize = null,
|
||||
|
||||
const Self = @This();
|
||||
|
||||
pub fn format(self: Self, w: *std.io.Writer) std.io.Writer.Error!void {
|
||||
try w.print("({d}) {d} to {d}, length = {d}, network = {?d}", .{self.index, self.from.index, self.to.index, self.length, self.network});
|
||||
}
|
||||
};
|
||||
|
||||
const JunctionBox = struct {
|
||||
index: usize,
|
||||
network: ?usize = null,
|
||||
position: Position,
|
||||
|
||||
const Self = @This();
|
||||
|
||||
pub fn format(self: Self, w: *std.io.Writer) std.io.Writer.Error!void {
|
||||
try w.print("{d} in network {?d} at {f}", .{self.index, self.network, self.position});
|
||||
try w.print("({d}) {f}", .{self.index, self.position});
|
||||
}
|
||||
};
|
||||
|
||||
const Position = struct {
|
||||
x: u32,
|
||||
y: u32,
|
||||
z: u32,
|
||||
v: @Vector(3, u32),
|
||||
|
||||
const Self = @This();
|
||||
|
||||
pub fn format(self: Self, w: *std.io.Writer) std.io.Writer.Error!void {
|
||||
try w.print("{d}, {d}, {d}", .{self.x, self.y, self.z});
|
||||
try w.print("{d}, {d}, {d}", .{self.v[0], self.v[1], self.v[2]});
|
||||
}
|
||||
|
||||
pub fn distance(self: Self, other: Self) u32 {
|
||||
const xds = std.math.pow(u32, @max(self.x, other.x) - @min(self.x, other.x), 2);
|
||||
const yds = std.math.pow(u32, @max(self.y, other.y) - @min(self.y, other.y), 2);
|
||||
const zds = std.math.pow(u32, @max(self.z, other.z) - @min(self.z, other.z), 2);
|
||||
return std.math.sqrt(xds + yds + zds);
|
||||
const comp = self.v < other.v;
|
||||
const low = @select(u32, comp, self.v, other.v);
|
||||
const high = @select(u32, !comp, self.v, other.v);
|
||||
const diff = high - low;
|
||||
return std.math.sqrt(@reduce(.Add, diff * diff));
|
||||
}
|
||||
};
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue