Finish day07, part 2
This commit is contained in:
parent
cc35a07724
commit
beca41e511
1 changed files with 65 additions and 18 deletions
|
|
@ -35,44 +35,73 @@ pub fn run(allocator: std.mem.Allocator) !void {
|
||||||
|
|
||||||
std.debug.print("{s}\n", .{first_line});
|
std.debug.print("{s}\n", .{first_line});
|
||||||
|
|
||||||
const previous_line = try allocator.alloc(u8, line_len);
|
const previous_line = try allocator.alloc(Cell, line_len);
|
||||||
defer allocator.free(previous_line);
|
defer allocator.free(previous_line);
|
||||||
|
|
||||||
var accumulator: usize = 0;
|
|
||||||
|
|
||||||
var i: usize = 0;
|
var i: usize = 0;
|
||||||
while (lines.next()) |line| {
|
while (lines.next()) |line| {
|
||||||
defer i += 1;
|
defer i += 1;
|
||||||
|
|
||||||
const current_line = try allocator.alloc(u8, line_len);
|
const current_line = try allocator.alloc(Cell, line_len);
|
||||||
defer allocator.free(current_line);
|
defer allocator.free(current_line);
|
||||||
@memcpy(current_line, line);
|
for (line, 0..) |c, ci| {
|
||||||
|
switch (c) {
|
||||||
|
'.' => current_line[ci] = .empty,
|
||||||
|
'^' => current_line[ci] = .splitter,
|
||||||
|
else => unreachable, // There shouldn't be beams on this line yet!
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
defer @memcpy(previous_line, current_line);
|
defer @memcpy(previous_line, current_line);
|
||||||
|
|
||||||
defer std.debug.print("{s}\n", .{current_line});
|
defer {
|
||||||
|
for (current_line) |cell| {
|
||||||
|
std.debug.print("{f}", .{cell});
|
||||||
|
}
|
||||||
|
std.debug.print("\n", .{});
|
||||||
|
}
|
||||||
|
|
||||||
if (i == 0) {
|
if (i == 0) {
|
||||||
std.debug.assert(current_line[starting_position] == '.');
|
std.debug.assert(current_line[starting_position] == .empty);
|
||||||
current_line[starting_position] = '|';
|
current_line[starting_position] = .{ .beam = .{ .timelines = 1 } };
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (line, 0..) |c, ci| {
|
for (current_line, 0..) |cell, celli| {
|
||||||
if (previous_line[ci] == '|') {
|
const previous_cell = previous_line[celli];
|
||||||
if (c == '.') {
|
if (previous_cell == .beam) {
|
||||||
current_line[ci] = '|';
|
if (cell == .empty) {
|
||||||
} else if (c == '^') {
|
current_line[celli] = .{ .beam = .{ .timelines = previous_cell.beam.timelines } };
|
||||||
accumulator += 1;
|
} else if (cell == .splitter) {
|
||||||
if (ci != 0 and current_line[ci - 1] == '.') {
|
if (celli != 0) {
|
||||||
current_line[ci - 1] = '|';
|
const left = current_line[celli - 1];
|
||||||
|
if (left == .empty) {
|
||||||
|
current_line[celli - 1] = .{ .beam = .{ .timelines = previous_cell.beam.timelines } };
|
||||||
|
} else if (left == .beam) {
|
||||||
|
current_line[celli - 1] = .{ .beam = .{ .timelines = left.beam.timelines + previous_cell.beam.timelines } };
|
||||||
}
|
}
|
||||||
if (ci != line_len - 1 and current_line[ci + 1] == '.') {
|
}
|
||||||
current_line[ci + 1] = '|';
|
if (celli != line_len - 1) {
|
||||||
|
const right = current_line[celli + 1];
|
||||||
|
if (right == .empty) {
|
||||||
|
current_line[celli + 1] = .{ .beam = .{ .timelines = previous_cell.beam.timelines } };
|
||||||
|
} else if (right == .beam) {
|
||||||
|
current_line[celli + 1] = .{ .beam = .{ .timelines = right.beam.timelines + previous_cell.beam.timelines } };
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else if (cell == .beam and previous_cell == .beam) {
|
||||||
|
current_line[celli] = .{ .beam = .{ .timelines = cell.beam.timelines + previous_cell.beam.timelines } };
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var accumulator: usize = 0;
|
||||||
|
for (previous_line) |cell| {
|
||||||
|
accumulator += switch (cell) {
|
||||||
|
.empty, .splitter => 0,
|
||||||
|
.beam => |beam| beam.timelines,
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
var buffer: [8]u8 = undefined;
|
var buffer: [8]u8 = undefined;
|
||||||
|
|
@ -84,3 +113,21 @@ pub fn run(allocator: std.mem.Allocator) !void {
|
||||||
try stdout.flush();
|
try stdout.flush();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const Cell = union(enum) {
|
||||||
|
empty: void,
|
||||||
|
splitter: void,
|
||||||
|
beam: struct {
|
||||||
|
timelines: usize,
|
||||||
|
},
|
||||||
|
|
||||||
|
const Self = @This();
|
||||||
|
|
||||||
|
pub fn format(self: Cell, w: *std.io.Writer) std.io.Writer.Error!void {
|
||||||
|
switch (self) {
|
||||||
|
.empty => try w.print(".", .{}),
|
||||||
|
.splitter => try w.print("^", .{}),
|
||||||
|
.beam => |beam| try w.print("{d}", .{beam.timelines}),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue