Finish day10, part 1
This commit is contained in:
parent
ec4a937062
commit
d68ac82e5d
1 changed files with 53 additions and 25 deletions
|
|
@ -3,14 +3,27 @@ const std = @import("std");
|
||||||
pub const title = "Day 10: Factory";
|
pub const title = "Day 10: Factory";
|
||||||
|
|
||||||
pub fn run(allocator: std.mem.Allocator) !void {
|
pub fn run(allocator: std.mem.Allocator) !void {
|
||||||
//const input = @embedFile("./input/day10.txt");
|
const input = @embedFile("./input/day10.txt");
|
||||||
const input =
|
//const input =
|
||||||
\\[.##.] (3) (1,3) (2) (2,3) (0,2) (0,1) {3,5,4,7}
|
// \\[.##.] (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,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}
|
// \\[.###.#] (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}
|
||||||
|
// ;
|
||||||
|
|
||||||
var lines = std.mem.tokenizeScalar(u8, input, '\n');
|
var lines = std.mem.tokenizeScalar(u8, input, '\n');
|
||||||
|
|
||||||
|
var accumulator: usize = 0;
|
||||||
|
|
||||||
|
var light_states: std.ArrayList(std.bit_set.IntegerBitSet(Machine.MaxLightCount)) = .empty;
|
||||||
|
defer light_states.deinit(allocator);
|
||||||
|
|
||||||
|
var next_light_states: std.ArrayList(std.bit_set.IntegerBitSet(Machine.MaxLightCount)) = .empty;
|
||||||
|
defer next_light_states.deinit(allocator);
|
||||||
|
|
||||||
var i: usize = 0;
|
var i: usize = 0;
|
||||||
while (lines.next()) |line| {
|
while (lines.next()) |line| {
|
||||||
defer i += 1;
|
defer i += 1;
|
||||||
|
|
@ -20,10 +33,41 @@ pub fn run(allocator: std.mem.Allocator) !void {
|
||||||
|
|
||||||
std.debug.print("{f}\n", .{machine});
|
std.debug.print("{f}\n", .{machine});
|
||||||
|
|
||||||
const initial_lights_state: std.bit_set.IntegerBitSet(Machine.MaxLightCount) = .initEmpty();
|
light_states.clearRetainingCapacity();
|
||||||
const button_presses = machine.pressButtonSequences(initial_lights_state);
|
try light_states.append(allocator, .initEmpty());
|
||||||
std.debug.print("machine {d} required {d} button presses", .{i, button_presses});
|
|
||||||
|
var buttons_pressed: usize = 0;
|
||||||
|
|
||||||
|
outer: while (true) {
|
||||||
|
next_light_states.clearRetainingCapacity();
|
||||||
|
|
||||||
|
buttons_pressed += 1;
|
||||||
|
for (light_states.items) |state| {
|
||||||
|
for (machine.buttons) |button| {
|
||||||
|
const next_state = state.xorWith(button);
|
||||||
|
if (next_state.eql(machine.lights_goal)) {
|
||||||
|
break :outer;
|
||||||
|
}
|
||||||
|
try next_light_states.append(allocator, next_state);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
light_states.clearRetainingCapacity();
|
||||||
|
try light_states.appendSlice(allocator, next_light_states.items);
|
||||||
|
}
|
||||||
|
|
||||||
|
std.debug.print("machine {d} requires {d} button presses\n", .{i, buttons_pressed});
|
||||||
|
|
||||||
|
accumulator += buttons_pressed;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var buffer: [64]u8 = undefined;
|
||||||
|
var stdout_writer = std.fs.File.stdout().writer(&buffer);
|
||||||
|
const stdout = &stdout_writer.interface;
|
||||||
|
|
||||||
|
try stdout.print("{d}\n", .{accumulator});
|
||||||
|
|
||||||
|
try stdout.flush();
|
||||||
}
|
}
|
||||||
|
|
||||||
const Machine = struct {
|
const Machine = struct {
|
||||||
|
|
@ -83,21 +127,5 @@ const Machine = struct {
|
||||||
try w.print("({b})", .{button.mask});
|
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