Switch to using a queue for character devices

This commit is contained in:
2019-12-07 18:35:03 +00:00
parent 4f614c7727
commit 325ec9d591
5 changed files with 77 additions and 39 deletions

View File

@@ -3,7 +3,7 @@ const std = @import("std");
const intcode = @import("intcode");
// TODO: it would be awesome if we could make this a comptime function
const permutations = [120][5]i32{
const perm1 = [120][5]i32{
.{ 0, 1, 2, 3, 4 },
.{ 0, 1, 2, 4, 3 },
.{ 0, 1, 3, 2, 4 },
@@ -138,7 +138,7 @@ pub fn main() anyerror!void {
// 120 phase permutations. Find the highest.
var max: i32 = 0;
for (permutations) |inputs| {
for (perm1) |inputs| {
for (machines) |*machine| {
var copy = try alloc.alloc(i32, program.len);
std.mem.copy(i32, copy, program);
@@ -148,22 +148,19 @@ pub fn main() anyerror!void {
// Phase input
for (inputs) |input, i| {
try machines[i].input.append(input);
try machines[i].writeToInput(input);
}
// amplifier input
for (machines) |*machine, i| {
var ampInput: i32 = 0;
if (i > 0) {
try machine.input.append(machines[i - 1].output.at(0));
} else {
try machine.input.append(0);
}
if (i > 0) ampInput = try machines[i - 1].readFromOutput(false);
try machine.writeToInput(ampInput);
try machine.run();
}
const final = machines[4].output.at(0);
const final = try machines[4].readFromOutput(false);
if (final > max)
max = final;
@@ -174,5 +171,7 @@ pub fn main() anyerror!void {
}
}
std.debug.warn("Part 1: {}\n", max);
std.debug.warn("Day 7, Part 1: {}\n", max);
// In part 2, the machines need to be wired into a feedback loop
}