diff --git a/src/days/day10.zig b/src/days/day10.zig index e70ce94..ce47597 100644 --- a/src/days/day10.zig +++ b/src/days/day10.zig @@ -3,12 +3,12 @@ const std = @import("std"); pub const title = "Day 10: Factory"; 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} - // ; + //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} + ; //const input = // \\[#...#] (1,3) (2,3,4) (0,2,3) (0,1,2) (2,3) {37,24,60,50,16} // \\[##...#..] (0,1,3,5,6) (0,1,5) (4) (5,6) (0,4,7) (1,2,5) (3) {23,18,2,26,14,36,25,7} @@ -18,11 +18,11 @@ pub fn run(allocator: std.mem.Allocator) !void { var accumulator: usize = 0; - var light_states: std.ArrayList(std.bit_set.IntegerBitSet(Machine.MaxLightCount)) = .empty; - defer light_states.deinit(allocator); + var jolt_states: std.ArrayList([Machine.MaxLightCount]u9) = .empty; + defer jolt_states.deinit(allocator); - var next_light_states: std.ArrayList(std.bit_set.IntegerBitSet(Machine.MaxLightCount)) = .empty; - defer next_light_states.deinit(allocator); + var next_jolt_states: std.ArrayList([Machine.MaxLightCount]u9) = .empty; + defer next_jolt_states.deinit(allocator); var i: usize = 0; while (lines.next()) |line| { @@ -33,27 +33,32 @@ pub fn run(allocator: std.mem.Allocator) !void { std.debug.print("{f}\n", .{machine}); - light_states.clearRetainingCapacity(); - try light_states.append(allocator, .initEmpty()); + jolt_states.clearRetainingCapacity(); + try jolt_states.append(allocator, [_]u9{0} ** Machine.MaxLightCount); var buttons_pressed: usize = 0; outer: while (true) { - next_light_states.clearRetainingCapacity(); + next_jolt_states.clearRetainingCapacity(); buttons_pressed += 1; - for (light_states.items) |state| { + for (jolt_states.items) |state| { for (machine.buttons) |button| { - const next_state = state.xorWith(button); - if (next_state.eql(machine.lights_goal)) { + var next_state: [Machine.MaxLightCount]u9 = state; + for (0..Machine.MaxLightCount) |index| { + if (button.isSet(index)) { + next_state[index] += 1; + } + } + if (std.mem.eql(u9, &next_state, machine.jolts_goal)) { break :outer; } - try next_light_states.append(allocator, next_state); + try next_jolt_states.append(allocator, next_state); } } - light_states.clearRetainingCapacity(); - try light_states.appendSlice(allocator, next_light_states.items); + jolt_states.clearRetainingCapacity(); + try jolt_states.appendSlice(allocator, next_jolt_states.items); } std.debug.print("machine {d} requires {d} button presses\n", .{i, buttons_pressed}); @@ -73,6 +78,7 @@ pub fn run(allocator: std.mem.Allocator) !void { const Machine = struct { lights_goal: std.bit_set.IntegerBitSet(MaxLightCount), buttons: []std.bit_set.IntegerBitSet(MaxLightCount), + jolts_goal: []u9, allocator: std.mem.Allocator, @@ -86,6 +92,8 @@ const Machine = struct { var lights_goal: std.bit_set.IntegerBitSet(MaxLightCount) = .initEmpty(); var buttons: std.ArrayList(std.bit_set.IntegerBitSet(MaxLightCount)) = .empty; defer buttons.deinit(allocator); + var jolts_goal: std.ArrayList(u9) = .empty; + defer jolts_goal.deinit(allocator); var line_iter = std.mem.tokenizeScalar(u8, line, ' '); while (line_iter.next()) |part| { @@ -103,29 +111,51 @@ const Machine = struct { } try buttons.append(allocator, button); + } else if (part[0] == '{' and part[part.len - 1] == '}') { + var values_iter = std.mem.tokenizeScalar(u8, p, ','); + while (values_iter.next()) |value| { + try jolts_goal.append(allocator, try std.fmt.parseUnsigned(u9, value, 10)); + } } } const buttons_slice = try buttons.toOwnedSlice(allocator); errdefer allocator.free(buttons_slice); + const jolts_goal_slice = try jolts_goal.toOwnedSlice(allocator); + errdefer allocator.free(jolts_goal_slice); + return .{ .lights_goal = lights_goal, .buttons = buttons_slice, + .jolts_goal = jolts_goal_slice, .allocator = allocator, }; } pub fn deinit(self: Self) void { self.allocator.free(self.buttons); + self.allocator.free(self.jolts_goal); } pub fn format(self: Self, w: *std.Io.Writer) std.Io.Writer.Error!void { try w.print("[{b}]", .{self.lights_goal.mask}); try w.writeByte(' '); - for (self.buttons) |button| { + for (self.buttons, 0..) |button, i| { try w.print("({b})", .{button.mask}); + if (i < self.buttons.len - 1) { + try w.writeByte(' '); + } } + try w.writeByte(' '); + try w.writeByte('{'); + for (self.jolts_goal, 0..) |jolt, i| { + try w.print("{d}", .{jolt}); + if (i < self.jolts_goal.len - 1) { + try w.writeByte(','); + } + } + try w.writeByte('}'); } };