Compare commits
3 commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 3eb2e2d4c1 | |||
| 47a8eaa38c | |||
| fa796b5331 |
2 changed files with 1160 additions and 2 deletions
|
|
@ -1,7 +1,165 @@
|
||||||
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 = .{ .v = .{ x, y, z } },
|
||||||
|
};
|
||||||
|
std.debug.print("{f}\n", .{junction_box});
|
||||||
|
|
||||||
|
try junction_boxes.append(allocator, junction_box);
|
||||||
|
}
|
||||||
|
|
||||||
|
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 length = junction_box.position.distance(other_junction_box.position);
|
||||||
|
const connection = Connection{
|
||||||
|
.index = connection_index,
|
||||||
|
.from = junction_box,
|
||||||
|
.to = other_junction_box,
|
||||||
|
.length = length,
|
||||||
|
};
|
||||||
|
std.debug.print("{f}\n", .{connection});
|
||||||
|
|
||||||
|
try possible_connections.append(allocator, connection);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
std.mem.sort(Connection, possible_connections.items, {}, struct {
|
||||||
|
fn f(_: void, lhs: Connection, rhs: Connection) bool {
|
||||||
|
return lhs.length < rhs.length;
|
||||||
|
}
|
||||||
|
}.f);
|
||||||
|
|
||||||
|
var network_id: usize = 0;
|
||||||
|
for (possible_connections.items, 0..) |*connection, i| {
|
||||||
|
var connection_network_id = network_id;
|
||||||
|
for (possible_connections.items) |other_connection| {
|
||||||
|
if (connection.index == other_connection.index) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (other_connection.network == null) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (connection.connected(other_connection)) {
|
||||||
|
connection_network_id = other_connection.network.?;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
connection.network = connection_network_id;
|
||||||
|
if (connection_network_id == network_id) {
|
||||||
|
network_id += 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
std.debug.print("{f}\n", .{connection});
|
||||||
|
|
||||||
|
if (i >= 10) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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});
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn connected(self: Self, other: Self) bool {
|
||||||
|
if (self.from.index == other.from.index) return true;
|
||||||
|
if (self.to.index == other.from.index) return true;
|
||||||
|
if (self.from.index == other.to.index) return true;
|
||||||
|
if (self.to.index == other.to.index) return true;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const JunctionBox = struct {
|
||||||
|
index: usize,
|
||||||
|
position: Position,
|
||||||
|
|
||||||
|
const Self = @This();
|
||||||
|
|
||||||
|
pub fn format(self: Self, w: *std.io.Writer) std.io.Writer.Error!void {
|
||||||
|
try w.print("({d}) {f}", .{self.index, self.position});
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const Position = struct {
|
||||||
|
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.v[0], self.v[1], self.v[2]});
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn distance(self: Self, other: Self) u32 {
|
||||||
|
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));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
|
|
||||||
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