WIP: implement part 2
This commit is contained in:
parent
f85eda4e87
commit
76b70d3c6b
1 changed files with 31 additions and 18 deletions
|
|
@ -5,6 +5,18 @@ pub const title = "Day 11: Reactor";
|
|||
pub fn run(allocator: std.mem.Allocator) !void {
|
||||
const input = @embedFile("./input/day11.txt");
|
||||
//const input =
|
||||
// \\aaa: you hhh
|
||||
// \\you: bbb ccc
|
||||
// \\bbb: ddd eee
|
||||
// \\ccc: ddd eee fff
|
||||
// \\ddd: ggg
|
||||
// \\eee: out
|
||||
// \\fff: out
|
||||
// \\ggg: out
|
||||
// \\hhh: ccc fff iii
|
||||
// \\iii: out
|
||||
// ;
|
||||
//const input =
|
||||
// \\svr: aaa bbb
|
||||
// \\aaa: fft
|
||||
// \\fft: ccc
|
||||
|
|
@ -49,8 +61,11 @@ pub fn run(allocator: std.mem.Allocator) !void {
|
|||
try devices.put(device, outputs_slice);
|
||||
}
|
||||
|
||||
var cache: std.StringHashMap(usize) = .init(allocator);
|
||||
defer cache.deinit();
|
||||
|
||||
var paths_to_out: usize = 0;
|
||||
visitOutputs("svr", devices, &paths_to_out, false, false);
|
||||
try visitOutputs("svr", "fft", devices, &cache, &paths_to_out);
|
||||
|
||||
var buffer: [8]u8 = undefined;
|
||||
var stdout_writer = std.fs.File.stdout().writer(&buffer);
|
||||
|
|
@ -63,30 +78,28 @@ pub fn run(allocator: std.mem.Allocator) !void {
|
|||
|
||||
fn visitOutputs(
|
||||
start: []const u8,
|
||||
end: []const u8,
|
||||
devices: std.StringHashMap([][]const u8),
|
||||
paths_to_out: *usize,
|
||||
visited_fft: bool,
|
||||
visited_dac: bool,
|
||||
) void {
|
||||
cache: *std.StringHashMap(usize),
|
||||
paths_to_end: *usize,
|
||||
) !void {
|
||||
if (cache.get(start)) |result| {
|
||||
paths_to_end.* += result;
|
||||
return;
|
||||
}
|
||||
|
||||
var paths: usize = 0;
|
||||
const device = devices.get(start);
|
||||
if (device) |outputs| {
|
||||
for (outputs) |output| {
|
||||
if (std.mem.eql(u8, output, "out")) {
|
||||
std.debug.print("found out! (fft = {any}, dac = {any})\n", .{visited_fft, visited_dac});
|
||||
if (visited_fft and visited_dac) {
|
||||
std.debug.print("visited fft and dac\n", .{});
|
||||
paths_to_out.* += 1;
|
||||
}
|
||||
} else if (std.mem.eql(u8, output, "fft")) {
|
||||
std.debug.print("found fft!\n", .{});
|
||||
visitOutputs(output, devices, paths_to_out, true, visited_dac);
|
||||
} else if (std.mem.eql(u8, output, "dac")) {
|
||||
std.debug.print("found dac!\n", .{});
|
||||
visitOutputs(output, devices, paths_to_out, visited_fft, true);
|
||||
if (std.mem.eql(u8, output, end)) {
|
||||
paths += 1;
|
||||
} else {
|
||||
visitOutputs(output, devices, paths_to_out, visited_fft, visited_dac);
|
||||
try visitOutputs(output, end, devices, cache, &paths);
|
||||
}
|
||||
}
|
||||
try cache.put(start, paths);
|
||||
paths_to_end.* += paths;
|
||||
} else {
|
||||
std.debug.print("device {s} not found\n", .{start});
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue