From 4f614c7727016a9328b0558ed9f0387ca96daf35 Mon Sep 17 00:00:00 2001 From: Nick Thomas Date: Sat, 7 Dec 2019 17:31:23 +0000 Subject: [PATCH] Day 7.1 --- 07/build.zig | 15 ++++ 07/input | 1 + 07/src/main.zig | 178 ++++++++++++++++++++++++++++++++++++++++ lib/intcode/intcode.zig | 7 +- run-intcodes | 1 + 5 files changed, 201 insertions(+), 1 deletion(-) create mode 100644 07/build.zig create mode 100644 07/input create mode 100644 07/src/main.zig diff --git a/07/build.zig b/07/build.zig new file mode 100644 index 0000000..16cb95e --- /dev/null +++ b/07/build.zig @@ -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); +} diff --git a/07/input b/07/input new file mode 100644 index 0000000..5828532 --- /dev/null +++ b/07/input @@ -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 diff --git a/07/src/main.zig b/07/src/main.zig new file mode 100644 index 0000000..630266d --- /dev/null +++ b/07/src/main.zig @@ -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 }, + .{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, 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); +} diff --git a/lib/intcode/intcode.zig b/lib/intcode/intcode.zig index 039fc20..9897b71 100644 --- a/lib/intcode/intcode.zig +++ b/lib/intcode/intcode.zig @@ -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 { diff --git a/run-intcodes b/run-intcodes index dcf2052..0b27af2 100755 --- a/run-intcodes +++ b/run-intcodes @@ -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