WIP: implement part 1

This commit is contained in:
ktkk 2025-12-08 21:29:47 +00:00
parent fa796b5331
commit 47a8eaa38c

View file

@ -51,92 +51,78 @@ pub fn run(allocator: std.mem.Allocator) !void {
}; };
const junction_box = JunctionBox{ const junction_box = JunctionBox{
.index = junction_box_index, .index = junction_box_index,
.position = .{ .x = x, .y = y, .z = z }, .position = .{ .v = .{ x, y, z } },
}; };
try junction_boxes.append(allocator, junction_box); try junction_boxes.append(allocator, junction_box);
} }
var network_id: usize = 0; var possible_connections: std.ArrayList(Connection) = .empty;
while (true) { defer possible_connections.deinit(allocator);
var shortest_distance: ?u32 = null; var connection_index: usize = 0;
var a: ?*JunctionBox = null; for (junction_boxes.items, 0..) |junction_box, i| {
var b: ?*JunctionBox = null; for (junction_boxes.items[i + 1..]) |other_junction_box| {
for (junction_boxes.items, 0..) |*junction_box, i| { defer connection_index += 1;
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;
}
const distance = junction_box.position.distance(other_junction_box.position); const length = junction_box.position.distance(other_junction_box.position);
if (shortest_distance == null or distance < shortest_distance.?) { const connection = Connection{
shortest_distance = distance; .index = connection_index,
a = junction_box; .from = junction_box,
b = other_junction_box; .to = other_junction_box,
} .length = length,
} };
} try possible_connections.append(allocator, connection);
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;
} }
} }
for (junction_boxes.items) |junction_box| { for (junction_boxes.items) |junction_box| {
std.debug.print("{f}\n", .{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 { const JunctionBox = struct {
index: usize, index: usize,
network: ?usize = null,
position: Position, position: Position,
const Self = @This(); const Self = @This();
pub fn format(self: Self, w: *std.io.Writer) std.io.Writer.Error!void { 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 { const Position = struct {
x: u32, v: @Vector(3, u32),
y: u32,
z: u32,
const Self = @This(); const Self = @This();
pub fn format(self: Self, w: *std.io.Writer) std.io.Writer.Error!void { 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 { 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 comp = self.v < other.v;
const yds = std.math.pow(u32, @max(self.y, other.y) - @min(self.y, other.y), 2); const low = @select(u32, comp, self.v, other.v);
const zds = std.math.pow(u32, @max(self.z, other.z) - @min(self.z, other.z), 2); const high = @select(u32, !comp, self.v, other.v);
return std.math.sqrt(xds + yds + zds); const diff = high - low;
return std.math.sqrt(@reduce(.Add, diff * diff));
} }
}; };