WIP: implement part 1
This commit is contained in:
parent
9d5a5a22d6
commit
ec4a937062
1 changed files with 26 additions and 11 deletions
|
|
@ -11,19 +11,18 @@ pub fn run(allocator: std.mem.Allocator) !void {
|
|||
;
|
||||
|
||||
var lines = std.mem.tokenizeScalar(u8, input, '\n');
|
||||
|
||||
var machines: std.ArrayList(Machine) = .empty;
|
||||
defer {
|
||||
for (machines.items) |machine| {
|
||||
machine.deinit();
|
||||
}
|
||||
machines.deinit(allocator);
|
||||
}
|
||||
|
||||
var i: usize = 0;
|
||||
while (lines.next()) |line| {
|
||||
defer i += 1;
|
||||
|
||||
const machine = try Machine.init(allocator, line);
|
||||
defer machine.deinit();
|
||||
|
||||
std.debug.print("{f}\n", .{machine});
|
||||
try machines.append(allocator, machine);
|
||||
|
||||
const initial_lights_state: std.bit_set.IntegerBitSet(Machine.MaxLightCount) = .initEmpty();
|
||||
const button_presses = machine.pressButtonSequences(initial_lights_state);
|
||||
std.debug.print("machine {d} required {d} button presses", .{i, button_presses});
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -35,7 +34,7 @@ const Machine = struct {
|
|||
|
||||
const Self = @This();
|
||||
|
||||
const MaxLightCount = 16;
|
||||
pub const MaxLightCount = 16;
|
||||
|
||||
pub fn init(allocator: std.mem.Allocator, line: []const u8) !Self {
|
||||
std.debug.assert(line[0] == '[');
|
||||
|
|
@ -84,5 +83,21 @@ const Machine = struct {
|
|||
try w.print("({b})", .{button.mask});
|
||||
}
|
||||
}
|
||||
|
||||
pub fn pressButtonSequences(
|
||||
self: Self,
|
||||
initial_lights_state: std.bit_set.IntegerBitSet(MaxLightCount),
|
||||
buttons_pressed: *usize,
|
||||
) usize {
|
||||
var button_presses: usize = 0;
|
||||
for (self.buttons) |button| {
|
||||
const result = initial_lights_state.xorWith(button);
|
||||
if (result.mask == self.lights_goal.mask) {
|
||||
return 1;
|
||||
}
|
||||
button_presses += self.pressButtonSequences(result);
|
||||
}
|
||||
return button_presses;
|
||||
}
|
||||
};
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue