Add '2019/' from commit 'fc21396bc86bc0f706225f4f6e1d8294d344ca53'
git-subtree-dir: 2019 git-subtree-mainline:f1be11fca8
git-subtree-split:fc21396bc8
This commit is contained in:
344
2019/07/src/main.zig
Normal file
344
2019/07/src/main.zig
Normal file
@@ -0,0 +1,344 @@
|
||||
const std = @import("std");
|
||||
|
||||
const intcode = @import("intcode");
|
||||
|
||||
// TODO: it would be awesome if we could make this a comptime function
|
||||
const perm1 = [120][5]intcode.Word{
|
||||
.{ 0, 1, 2, 3, 4 },
|
||||
.{ 0, 1, 2, 4, 3 },
|
||||
.{ 0, 1, 3, 2, 4 },
|
||||
.{ 0, 1, 3, 4, 2 },
|
||||
.{ 0, 1, 4, 2, 3 },
|
||||
.{ 0, 1, 4, 3, 2 },
|
||||
.{ 0, 2, 1, 3, 4 },
|
||||
.{ 0, 2, 1, 4, 3 },
|
||||
.{ 0, 2, 3, 1, 4 },
|
||||
.{ 0, 2, 3, 4, 1 },
|
||||
.{ 0, 2, 4, 1, 3 },
|
||||
.{ 0, 2, 4, 3, 1 },
|
||||
.{ 0, 3, 1, 2, 4 },
|
||||
.{ 0, 3, 1, 4, 2 },
|
||||
.{ 0, 3, 2, 1, 4 },
|
||||
.{ 0, 3, 2, 4, 1 },
|
||||
.{ 0, 3, 4, 1, 2 },
|
||||
.{ 0, 3, 4, 2, 1 },
|
||||
.{ 0, 4, 1, 2, 3 },
|
||||
.{ 0, 4, 1, 3, 2 },
|
||||
.{ 0, 4, 2, 1, 3 },
|
||||
.{ 0, 4, 2, 3, 1 },
|
||||
.{ 0, 4, 3, 1, 2 },
|
||||
.{ 0, 4, 3, 2, 1 },
|
||||
.{ 1, 0, 2, 3, 4 },
|
||||
.{ 1, 0, 2, 4, 3 },
|
||||
.{ 1, 0, 3, 2, 4 },
|
||||
.{ 1, 0, 3, 4, 2 },
|
||||
.{ 1, 0, 4, 2, 3 },
|
||||
.{ 1, 0, 4, 3, 2 },
|
||||
.{ 1, 2, 0, 3, 4 },
|
||||
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 5, 6, 7 },
|
||||
.{ 9, 8, 5, 7, 6 },
|
||||
.{ 9, 8, 6, 5, 7 },
|
||||
.{ 9, 8, 6, 7, 5 },
|
||||
.{ 9, 8, 7, 5, 6 },
|
||||
.{ 9, 8, 7, 6, 5 },
|
||||
};
|
||||
|
||||
fn doIt(m: *intcode.Machine) void {
|
||||
if (m.run()) {} else |err| {
|
||||
std.debug.warn("Machine {} failed to run successfully: {}\n", m.idx, err);
|
||||
}
|
||||
}
|
||||
|
||||
fn run(program: []intcode.Word, machines: *[5]intcode.Machine, permutations: [120][5]intcode.Word) !intcode.Word {
|
||||
var max: intcode.Word = 0;
|
||||
|
||||
for (permutations) |inputs| {
|
||||
// Reset program state each time
|
||||
for (machines) |*machine, i| {
|
||||
std.mem.copy(intcode.Word, machine.memory, program);
|
||||
machine.ip = 0;
|
||||
machine.idx = i;
|
||||
}
|
||||
|
||||
// Phase inputs
|
||||
for (inputs) |input, i| {
|
||||
try machines[i].writeToInput(input);
|
||||
}
|
||||
|
||||
// Provide starting value of 0
|
||||
try machines[0].writeToInput(0);
|
||||
|
||||
// run the machines asychronously
|
||||
var f0 = try std.Thread.spawn(&machines[0], doIt);
|
||||
var f1 = try std.Thread.spawn(&machines[1], doIt);
|
||||
var f2 = try std.Thread.spawn(&machines[2], doIt);
|
||||
var f3 = try std.Thread.spawn(&machines[3], doIt);
|
||||
var f4 = try std.Thread.spawn(&machines[4], doIt);
|
||||
|
||||
f0.wait();
|
||||
f1.wait();
|
||||
f2.wait();
|
||||
f3.wait();
|
||||
f4.wait();
|
||||
|
||||
const final = try machines[4].readFromOutput();
|
||||
if (final > max)
|
||||
max = final;
|
||||
}
|
||||
|
||||
return max;
|
||||
}
|
||||
|
||||
pub fn main() anyerror!void {
|
||||
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
|
||||
defer arena.deinit();
|
||||
|
||||
const alloc = &arena.allocator;
|
||||
var program = try intcode.loadFromStdIn(alloc);
|
||||
|
||||
// 5 intcode computers act as the amps
|
||||
var machines: [5]intcode.Machine = undefined;
|
||||
|
||||
// Allocate memory once only.
|
||||
for (machines) |*machine| {
|
||||
machine.alloc = alloc;
|
||||
machine.memory = try alloc.alloc(intcode.Word, program.len);
|
||||
}
|
||||
|
||||
// Now wire up the machines correctly. The output of each machine should be
|
||||
// the input to the next.
|
||||
const pipes: [6]intcode.CharDev = .{
|
||||
try std.os.pipe(), try std.os.pipe(),
|
||||
try std.os.pipe(), try std.os.pipe(),
|
||||
try std.os.pipe(), try std.os.pipe(),
|
||||
};
|
||||
|
||||
machines[0].input = pipes[0];
|
||||
machines[0].output = pipes[1];
|
||||
|
||||
machines[1].input = machines[0].output;
|
||||
machines[1].output = pipes[2];
|
||||
|
||||
machines[2].input = machines[1].output;
|
||||
machines[2].output = pipes[3];
|
||||
|
||||
machines[3].input = machines[2].output;
|
||||
machines[3].output = pipes[4];
|
||||
|
||||
machines[4].input = machines[3].output;
|
||||
machines[4].output = pipes[5];
|
||||
|
||||
std.debug.warn("Day 7, Part 1: {}\n", try run(program, &machines, perm1));
|
||||
|
||||
// In part 2, the machines need to be wired into a feedback loop.
|
||||
// To do this, drop pipe 6 and replace it with pipe 0
|
||||
machines[0].input = machines[4].output;
|
||||
|
||||
std.debug.warn("Day 7, Part 2: {}\n", try run(program, &machines, perm2));
|
||||
}
|
Reference in New Issue
Block a user