WIP: Implement part 1
Use IntegerBitSet instead of slices
This commit is contained in:
parent
4a058df871
commit
9d5a5a22d6
1 changed files with 20 additions and 55 deletions
|
|
@ -28,63 +28,38 @@ pub fn run(allocator: std.mem.Allocator) !void {
|
|||
}
|
||||
|
||||
const Machine = struct {
|
||||
lights_goal: []bool,
|
||||
buttons: []Button,
|
||||
lights_goal: std.bit_set.IntegerBitSet(MaxLightCount),
|
||||
buttons: []std.bit_set.IntegerBitSet(MaxLightCount),
|
||||
|
||||
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();
|
||||
|
||||
const MaxLightCount = 16;
|
||||
|
||||
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;
|
||||
var lights_goal: std.bit_set.IntegerBitSet(MaxLightCount) = .initEmpty();
|
||||
var buttons: std.ArrayList(std.bit_set.IntegerBitSet(MaxLightCount)) = .empty;
|
||||
defer buttons.deinit(allocator);
|
||||
|
||||
var parts = std.mem.tokenizeScalar(u8, line, ' ');
|
||||
while (parts.next()) |part| {
|
||||
var line_iter = std.mem.tokenizeScalar(u8, line, ' ');
|
||||
while (line_iter.next()) |part| {
|
||||
const p = part[1..part.len - 1];
|
||||
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,
|
||||
};
|
||||
for (p, 0..) |c, i| {
|
||||
if (c == '#') lights_goal.set(i);
|
||||
}
|
||||
} else if (part[0] == '(' and part[part.len - 1] == ')') {
|
||||
var toggles: std.ArrayList(usize) = .empty;
|
||||
defer toggles.deinit(allocator);
|
||||
var button: std.bit_set.IntegerBitSet(MaxLightCount) = .initEmpty();
|
||||
|
||||
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);
|
||||
var values_iter = std.mem.tokenizeScalar(u8, p, ',');
|
||||
while (values_iter.next()) |value| {
|
||||
button.set(try std.fmt.parseUnsigned(usize, value, 10));
|
||||
}
|
||||
|
||||
const toggles_slice = try toggles.toOwnedSlice(allocator);
|
||||
errdefer allocator.free(toggles_slice);
|
||||
try buttons.append(allocator, .{
|
||||
.toggles = toggles_slice,
|
||||
});
|
||||
try buttons.append(allocator, button);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -99,24 +74,14 @@ const Machine = struct {
|
|||
}
|
||||
|
||||
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(' ');
|
||||
}
|
||||
try w.print("[{b}]", .{self.lights_goal.mask});
|
||||
try w.writeByte(' ');
|
||||
for (self.buttons) |button| {
|
||||
try w.print("({b})", .{button.mask});
|
||||
}
|
||||
}
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue