Finish day07, part 1

This commit is contained in:
ktkk 2025-12-07 15:45:04 +00:00
parent fc3a53c68d
commit cc35a07724
2 changed files with 223 additions and 2 deletions

View file

@ -1,7 +1,86 @@
const std = @import("std");
pub const title = "Day 07";
pub const title = "Day 07: Laboratories";
pub fn run(_: std.mem.Allocator) !void {
pub fn run(allocator: std.mem.Allocator) !void {
const input = @embedFile("./input/day07.txt");
//const input =
// \\.......S.......
// \\...............
// \\.......^.......
// \\...............
// \\......^.^......
// \\...............
// \\.....^.^.^.....
// \\...............
// \\....^.^...^....
// \\...............
// \\...^.^...^.^...
// \\...............
// \\..^...^.....^..
// \\...............
// \\.^.^.^.^.^...^.
// \\...............
// ;
var lines = std.mem.tokenizeScalar(u8, input, '\n');
const first_line = lines.next() orelse unreachable;
const starting_position = for (first_line, 0..) |c, i| {
if (c == 'S') break i;
} else unreachable;
const line_len = first_line.len;
std.debug.print("starting_position = {d}\n", .{starting_position});
std.debug.print("{s}\n", .{first_line});
const previous_line = try allocator.alloc(u8, line_len);
defer allocator.free(previous_line);
var accumulator: usize = 0;
var i: usize = 0;
while (lines.next()) |line| {
defer i += 1;
const current_line = try allocator.alloc(u8, line_len);
defer allocator.free(current_line);
@memcpy(current_line, line);
defer @memcpy(previous_line, current_line);
defer std.debug.print("{s}\n", .{current_line});
if (i == 0) {
std.debug.assert(current_line[starting_position] == '.');
current_line[starting_position] = '|';
continue;
}
for (line, 0..) |c, ci| {
if (previous_line[ci] == '|') {
if (c == '.') {
current_line[ci] = '|';
} else if (c == '^') {
accumulator += 1;
if (ci != 0 and current_line[ci - 1] == '.') {
current_line[ci - 1] = '|';
}
if (ci != line_len - 1 and current_line[ci + 1] == '.') {
current_line[ci + 1] = '|';
}
}
}
}
}
var buffer: [8]u8 = undefined;
var stdout_writer = std.fs.File.stdout().writer(&buffer);
const stdout = &stdout_writer.interface;
try stdout.print("{d}\n", .{accumulator});
try stdout.flush();
}