WIP: implement part 1
This commit is contained in:
parent
862f25d6d8
commit
fa796b5331
2 changed files with 1137 additions and 2 deletions
|
|
@ -1,7 +1,142 @@
|
||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
|
|
||||||
pub const title = "Day 08";
|
pub const title = "Day 08: Playground";
|
||||||
|
|
||||||
pub fn run(_: std.mem.Allocator) !void {
|
pub fn run(allocator: std.mem.Allocator) !void {
|
||||||
|
//const input = @embedFile("./input/day08.txt");
|
||||||
|
const input =
|
||||||
|
\\162,817,812
|
||||||
|
\\57,618,57
|
||||||
|
\\906,360,560
|
||||||
|
\\592,479,940
|
||||||
|
\\352,342,300
|
||||||
|
\\466,668,158
|
||||||
|
\\542,29,236
|
||||||
|
\\431,825,988
|
||||||
|
\\739,650,466
|
||||||
|
\\52,470,668
|
||||||
|
\\216,146,977
|
||||||
|
\\819,987,18
|
||||||
|
\\117,168,530
|
||||||
|
\\805,96,715
|
||||||
|
\\346,949,466
|
||||||
|
\\970,615,88
|
||||||
|
\\941,993,340
|
||||||
|
\\862,61,35
|
||||||
|
\\984,92,344
|
||||||
|
\\425,690,689
|
||||||
|
;
|
||||||
|
|
||||||
|
var lines = std.mem.tokenizeScalar(u8, input, '\n');
|
||||||
|
|
||||||
|
var junction_boxes: std.ArrayList(JunctionBox) = .empty;
|
||||||
|
defer junction_boxes.deinit(allocator);
|
||||||
|
|
||||||
|
var junction_box_index: usize = 0;
|
||||||
|
while (lines.next()) |line| {
|
||||||
|
defer junction_box_index += 1;
|
||||||
|
|
||||||
|
var coordinates = std.mem.tokenizeScalar(u8, line, ',');
|
||||||
|
const x = blk: {
|
||||||
|
const s = coordinates.next() orelse continue;
|
||||||
|
break :blk try std.fmt.parseUnsigned(u32, s, 10);
|
||||||
|
};
|
||||||
|
const y = blk: {
|
||||||
|
const s = coordinates.next() orelse continue;
|
||||||
|
break :blk try std.fmt.parseUnsigned(u32, s, 10);
|
||||||
|
};
|
||||||
|
const z = blk: {
|
||||||
|
const s = coordinates.next() orelse continue;
|
||||||
|
break :blk try std.fmt.parseUnsigned(u32, s, 10);
|
||||||
|
};
|
||||||
|
const junction_box = JunctionBox{
|
||||||
|
.index = junction_box_index,
|
||||||
|
.position = .{ .x = x, .y = y, .z = 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (junction_boxes.items) |junction_box| {
|
||||||
|
std.debug.print("{f}\n", .{junction_box});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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});
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const Position = struct {
|
||||||
|
x: u32,
|
||||||
|
y: u32,
|
||||||
|
z: 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});
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
|
|
||||||
1000
src/days/input/day08.txt
Executable file
1000
src/days/input/day08.txt
Executable file
File diff suppressed because it is too large
Load diff
Loading…
Add table
Add a link
Reference in a new issue