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 {
|
const Machine = struct {
|
||||||
lights_goal: []bool,
|
lights_goal: std.bit_set.IntegerBitSet(MaxLightCount),
|
||||||
buttons: []Button,
|
buttons: []std.bit_set.IntegerBitSet(MaxLightCount),
|
||||||
|
|
||||||
allocator: std.mem.Allocator,
|
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 Self = @This();
|
||||||
|
|
||||||
|
const MaxLightCount = 16;
|
||||||
|
|
||||||
pub fn init(allocator: std.mem.Allocator, line: []const u8) !Self {
|
pub fn init(allocator: std.mem.Allocator, line: []const u8) !Self {
|
||||||
std.debug.assert(line[0] == '[');
|
std.debug.assert(line[0] == '[');
|
||||||
|
|
||||||
var lights_goal: []bool = undefined;
|
var lights_goal: std.bit_set.IntegerBitSet(MaxLightCount) = .initEmpty();
|
||||||
var buttons: std.ArrayList(Button) = .empty;
|
var buttons: std.ArrayList(std.bit_set.IntegerBitSet(MaxLightCount)) = .empty;
|
||||||
defer buttons.deinit(allocator);
|
defer buttons.deinit(allocator);
|
||||||
|
|
||||||
var parts = std.mem.tokenizeScalar(u8, line, ' ');
|
var line_iter = std.mem.tokenizeScalar(u8, line, ' ');
|
||||||
while (parts.next()) |part| {
|
while (line_iter.next()) |part| {
|
||||||
|
const p = part[1..part.len - 1];
|
||||||
if (part[0] == '[' and part[part.len - 1] == ']') {
|
if (part[0] == '[' and part[part.len - 1] == ']') {
|
||||||
const lights_count = part.len - 2;
|
for (p, 0..) |c, i| {
|
||||||
lights_goal = try allocator.alloc(bool, lights_count);
|
if (c == '#') lights_goal.set(i);
|
||||||
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] == ')') {
|
} else if (part[0] == '(' and part[part.len - 1] == ')') {
|
||||||
var toggles: std.ArrayList(usize) = .empty;
|
var button: std.bit_set.IntegerBitSet(MaxLightCount) = .initEmpty();
|
||||||
defer toggles.deinit(allocator);
|
|
||||||
|
|
||||||
var indexes = std.mem.tokenizeScalar(u8, part[1..part.len - 1], ',');
|
var values_iter = std.mem.tokenizeScalar(u8, p, ',');
|
||||||
while (indexes.next()) |index| {
|
while (values_iter.next()) |value| {
|
||||||
const toggle = try std.fmt.parseUnsigned(usize, index, 10);
|
button.set(try std.fmt.parseUnsigned(usize, value, 10));
|
||||||
try toggles.append(allocator, toggle);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const toggles_slice = try toggles.toOwnedSlice(allocator);
|
try buttons.append(allocator, button);
|
||||||
errdefer allocator.free(toggles_slice);
|
|
||||||
try buttons.append(allocator, .{
|
|
||||||
.toggles = toggles_slice,
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -99,24 +74,14 @@ const Machine = struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn deinit(self: Self) void {
|
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);
|
self.allocator.free(self.buttons);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn format(self: Self, w: *std.Io.Writer) std.Io.Writer.Error!void {
|
pub fn format(self: Self, w: *std.Io.Writer) std.Io.Writer.Error!void {
|
||||||
try w.writeByte('[');
|
try w.print("[{b}]", .{self.lights_goal.mask});
|
||||||
for (self.lights_goal) |light| {
|
try w.writeByte(' ');
|
||||||
try w.writeByte(if (light) '#' else '.');
|
for (self.buttons) |button| {
|
||||||
}
|
try w.print("({b})", .{button.mask});
|
||||||
_ = try w.write("] ");
|
|
||||||
for (self.buttons, 0..) |button, i| {
|
|
||||||
try w.print("{f}", .{button});
|
|
||||||
if (i < self.buttons.len - 1) {
|
|
||||||
try w.writeByte(' ');
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue