Finish day05, part 1

This commit is contained in:
ktkk 2025-12-05 08:52:11 +00:00
parent 9ad1671195
commit 9dd5b41fe5
2 changed files with 1242 additions and 2 deletions

View file

@ -1,7 +1,59 @@
const std = @import("std");
pub const title = "Day 05";
pub const title = "Day 05: Cafeteria";
pub fn run(_: std.mem.Allocator) !void {
pub fn run(allocator: std.mem.Allocator) !void {
const input = @embedFile("./input/day05.txt");
//const input =
// \\3-5
// \\10-14
// \\16-20
// \\12-18
// \\
// \\1
// \\5
// \\8
// \\11
// \\17
// \\32
// ;
var lines = std.mem.tokenizeScalar(u8, input, '\n');
var ranges: std.ArrayList(Range) = .empty;
defer ranges.deinit(allocator);
var accumulator: u32 = 0;
outer: while (lines.next()) |line| {
var components = std.mem.tokenizeScalar(u8, line, '-');
const id_or_start_s = components.next() orelse continue;
const id_or_start = try std.fmt.parseUnsigned(u64, id_or_start_s, 10);
if (components.next()) |end_s| {
const end = try std.fmt.parseUnsigned(u64, end_s, 10);
try ranges.append(allocator, Range{ .start = id_or_start, .end = end });
} else {
for (ranges.items) |range| {
if (id_or_start >= range.start and id_or_start <= range.end) {
accumulator += 1;
continue :outer;
}
}
}
}
var buffer: [64]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();
}
const Range = struct {
start: u64,
end: u64,
};

1188
src/days/input/day05.txt Executable file

File diff suppressed because it is too large Load diff