WIP: implement part 1

This commit is contained in:
ktkk 2025-12-10 12:53:18 +00:00
parent 5a8bf21386
commit 4a058df871
2 changed files with 316 additions and 2 deletions

View file

@ -1,7 +1,123 @@
const std = @import("std");
pub const title = "Day 10";
pub const title = "Day 10: Factory";
pub fn run(_: std.mem.Allocator) !void {
pub fn run(allocator: std.mem.Allocator) !void {
//const input = @embedFile("./input/day10.txt");
const input =
\\[.##.] (3) (1,3) (2) (2,3) (0,2) (0,1) {3,5,4,7}
\\[...#.] (0,2,3,4) (2,3) (0,4) (0,1,2) (1,2,3,4) {7,5,12,7,2}
\\[.###.#] (0,1,2,3,4) (0,3,4) (0,1,2,4,5) (1,2) {10,11,11,5,10,5}
;
var lines = std.mem.tokenizeScalar(u8, input, '\n');
var machines: std.ArrayList(Machine) = .empty;
defer {
for (machines.items) |machine| {
machine.deinit();
}
machines.deinit(allocator);
}
while (lines.next()) |line| {
const machine = try Machine.init(allocator, line);
std.debug.print("{f}\n", .{machine});
try machines.append(allocator, machine);
}
}
const Machine = struct {
lights_goal: []bool,
buttons: []Button,
allocator: std.mem.Allocator,
const Button = struct {
toggles: []usize,
pub fn format(self: Button, w: *std.Io.Writer) std.Io.Writer.Error!void {
try w.writeByte('(');
for (self.toggles, 0..) |toggle, i| {
try w.print("{d}", .{toggle});
if (i < self.toggles.len - 1) {
try w.writeByte(',');
}
}
try w.writeByte(')');
}
};
const Self = @This();
pub fn init(allocator: std.mem.Allocator, line: []const u8) !Self {
std.debug.assert(line[0] == '[');
var lights_goal: []bool = undefined;
var buttons: std.ArrayList(Button) = .empty;
defer buttons.deinit(allocator);
var parts = std.mem.tokenizeScalar(u8, line, ' ');
while (parts.next()) |part| {
if (part[0] == '[' and part[part.len - 1] == ']') {
const lights_count = part.len - 2;
lights_goal = try allocator.alloc(bool, lights_count);
errdefer allocator.free(lights_count);
for (part[1..part.len - 1], 0..) |c, i| {
lights_goal[i] = switch (c) {
'.' => false,
'#' => true,
else => unreachable,
};
}
} else if (part[0] == '(' and part[part.len - 1] == ')') {
var toggles: std.ArrayList(usize) = .empty;
defer toggles.deinit(allocator);
var indexes = std.mem.tokenizeScalar(u8, part[1..part.len - 1], ',');
while (indexes.next()) |index| {
const toggle = try std.fmt.parseUnsigned(usize, index, 10);
try toggles.append(allocator, toggle);
}
const toggles_slice = try toggles.toOwnedSlice(allocator);
errdefer allocator.free(toggles_slice);
try buttons.append(allocator, .{
.toggles = toggles_slice,
});
}
}
const buttons_slice = try buttons.toOwnedSlice(allocator);
errdefer allocator.free(buttons_slice);
return .{
.lights_goal = lights_goal,
.buttons = buttons_slice,
.allocator = allocator,
};
}
pub fn deinit(self: Self) void {
self.allocator.free(self.lights_goal);
for (self.buttons) |button| {
self.allocator.free(button.toggles);
}
self.allocator.free(self.buttons);
}
pub fn format(self: Self, w: *std.Io.Writer) std.Io.Writer.Error!void {
try w.writeByte('[');
for (self.lights_goal) |light| {
try w.writeByte(if (light) '#' else '.');
}
_ = try w.write("] ");
for (self.buttons, 0..) |button, i| {
try w.print("{f}", .{button});
if (i < self.buttons.len - 1) {
try w.writeByte(' ');
}
}
}
};