This commit is contained in:
2019-12-07 17:31:23 +00:00
parent ea8f7914f9
commit 4f614c7727
5 changed files with 201 additions and 1 deletions

15
07/build.zig Normal file
View File

@@ -0,0 +1,15 @@
const Builder = @import("std").build.Builder;
pub fn build(b: *Builder) void {
const mode = b.standardReleaseOptions();
const exe = b.addExecutable("07", "src/main.zig");
exe.addPackagePath("intcode", "../lib/intcode/intcode.zig");
exe.setBuildMode(mode);
exe.install();
const run_cmd = exe.run();
run_cmd.step.dependOn(b.getInstallStep());
const run_step = b.step("run", "Run the app");
run_step.dependOn(&run_cmd.step);
}

1
07/input Normal file
View File

@@ -0,0 +1 @@
3,8,1001,8,10,8,105,1,0,0,21,34,55,68,85,106,187,268,349,430,99999,3,9,1001,9,5,9,1002,9,5,9,4,9,99,3,9,1002,9,2,9,1001,9,2,9,1002,9,5,9,1001,9,2,9,4,9,99,3,9,101,3,9,9,102,3,9,9,4,9,99,3,9,1002,9,5,9,101,3,9,9,102,5,9,9,4,9,99,3,9,1002,9,4,9,1001,9,2,9,102,3,9,9,101,3,9,9,4,9,99,3,9,1001,9,2,9,4,9,3,9,101,2,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,1002,9,2,9,4,9,3,9,1001,9,1,9,4,9,3,9,1001,9,2,9,4,9,3,9,1002,9,2,9,4,9,3,9,101,2,9,9,4,9,3,9,1001,9,2,9,4,9,3,9,102,2,9,9,4,9,99,3,9,1002,9,2,9,4,9,3,9,102,2,9,9,4,9,3,9,101,1,9,9,4,9,3,9,1001,9,1,9,4,9,3,9,1001,9,1,9,4,9,3,9,101,1,9,9,4,9,3,9,1001,9,1,9,4,9,3,9,101,2,9,9,4,9,3,9,1001,9,1,9,4,9,3,9,1001,9,1,9,4,9,99,3,9,1001,9,2,9,4,9,3,9,101,2,9,9,4,9,3,9,101,2,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,102,2,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,101,1,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,102,2,9,9,4,9,3,9,1002,9,2,9,4,9,99,3,9,102,2,9,9,4,9,3,9,1001,9,2,9,4,9,3,9,102,2,9,9,4,9,3,9,102,2,9,9,4,9,3,9,101,1,9,9,4,9,3,9,102,2,9,9,4,9,3,9,102,2,9,9,4,9,3,9,102,2,9,9,4,9,3,9,101,1,9,9,4,9,3,9,1002,9,2,9,4,9,99,3,9,102,2,9,9,4,9,3,9,1001,9,1,9,4,9,3,9,102,2,9,9,4,9,3,9,101,1,9,9,4,9,3,9,102,2,9,9,4,9,3,9,102,2,9,9,4,9,3,9,101,2,9,9,4,9,3,9,101,2,9,9,4,9,3,9,1001,9,2,9,4,9,3,9,102,2,9,9,4,9,99

178
07/src/main.zig Normal file
View File

@@ -0,0 +1,178 @@
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{
.{ 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, 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.{ 3, 1, 2, 0, 4 },
.{ 3, 1, 2, 4, 0 },
.{ 3, 1, 4, 0, 2 },
.{ 3, 1, 4, 2, 0 },
.{ 3, 2, 0, 1, 4 },
.{ 3, 2, 0, 4, 1 },
.{ 3, 2, 1, 0, 4 },
.{ 3, 2, 1, 4, 0 },
.{ 3, 2, 4, 0, 1 },
.{ 3, 2, 4, 1, 0 },
.{ 3, 4, 0, 1, 2 },
.{ 3, 4, 0, 2, 1 },
.{ 3, 4, 1, 0, 2 },
.{ 3, 4, 1, 2, 0 },
.{ 3, 4, 2, 0, 1 },
.{ 3, 4, 2, 1, 0 },
.{ 4, 0, 1, 2, 3 },
.{ 4, 0, 1, 3, 2 },
.{ 4, 0, 2, 1, 3 },
.{ 4, 0, 2, 3, 1 },
.{ 4, 0, 3, 1, 2 },
.{ 4, 0, 3, 2, 1 },
.{ 4, 1, 0, 2, 3 },
.{ 4, 1, 0, 3, 2 },
.{ 4, 1, 2, 0, 3 },
.{ 4, 1, 2, 3, 0 },
.{ 4, 1, 3, 0, 2 },
.{ 4, 1, 3, 2, 0 },
.{ 4, 2, 0, 1, 3 },
.{ 4, 2, 0, 3, 1 },
.{ 4, 2, 1, 0, 3 },
.{ 4, 2, 1, 3, 0 },
.{ 4, 2, 3, 0, 1 },
.{ 4, 2, 3, 1, 0 },
.{ 4, 3, 0, 1, 2 },
.{ 4, 3, 0, 2, 1 },
.{ 4, 3, 1, 0, 2 },
.{ 4, 3, 1, 2, 0 },
.{ 4, 3, 2, 0, 1 },
.{ 4, 3, 2, 1, 0 },
};
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;
// 120 phase permutations. Find the highest.
var max: i32 = 0;
for (permutations) |inputs| {
for (machines) |*machine| {
var copy = try alloc.alloc(i32, program.len);
std.mem.copy(i32, copy, program);
machine.* = try intcode.Machine.init(alloc, copy);
}
// Phase input
for (inputs) |input, i| {
try machines[i].input.append(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);
}
try machine.run();
}
const final = machines[4].output.at(0);
if (final > max)
max = final;
// Don't forget to free everything!
for (machines) |*machine, i| {
alloc.free(machine.memory);
machine.deinit();
}
}
std.debug.warn("Part 1: {}\n", max);
}

View File

@@ -13,7 +13,7 @@ pub fn loadFromStream(alloc: *std.mem.Allocator, stream: *std.fs.File.InStream.S
var trimmed = std.mem.trimRight(u8, num, "\r\n");
program[i] = try std.fmt.parseInt(i32, trimmed, 10);
std.debug.warn("{},", program[i]);
// std.debug.warn("{},", program[i]);
i += 1;
}
@@ -118,6 +118,11 @@ pub const Machine = struct {
while (try self.step()) {}
}
pub fn deinit(self: *Machine) void {
self.input.deinit();
self.output.deinit();
}
/// Read an immediate or position value from memory. Parameter determines
/// which field following the current instruction to read from
inline fn __read(self: *Machine, parameter: usize, mode: Mode) i32 {

View File

@@ -2,3 +2,4 @@
zig test lib/intcode/intcode.zig
zig build --build-file 02/build.zig run < 02/input
zig build --build-file 05/build.zig run < 05/input
zig build --build-file 07/build.zig run < 07/input