Add '2019/' from commit 'fc21396bc86bc0f706225f4f6e1d8294d344ca53'
git-subtree-dir: 2019 git-subtree-mainline:f1be11fca8
git-subtree-split:fc21396bc8
This commit is contained in:
15
2019/02/build.zig
Normal file
15
2019/02/build.zig
Normal 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("part-02", "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
2019/02/input
Normal file
1
2019/02/input
Normal file
@@ -0,0 +1 @@
|
||||
1,0,0,3,1,1,2,3,1,3,4,3,1,5,0,3,2,1,13,19,2,9,19,23,1,23,6,27,1,13,27,31,1,31,10,35,1,9,35,39,1,39,9,43,2,6,43,47,1,47,5,51,2,10,51,55,1,6,55,59,2,13,59,63,2,13,63,67,1,6,67,71,1,71,5,75,2,75,6,79,1,5,79,83,1,83,6,87,2,10,87,91,1,9,91,95,1,6,95,99,1,99,6,103,2,103,9,107,2,107,10,111,1,5,111,115,1,115,6,119,2,6,119,123,1,10,123,127,1,127,5,131,1,131,2,135,1,135,5,0,99,2,0,14,0
|
56
2019/02/src/main.zig
Normal file
56
2019/02/src/main.zig
Normal file
@@ -0,0 +1,56 @@
|
||||
const std = @import("std");
|
||||
const intcode = @import("intcode");
|
||||
|
||||
fn run(alloc: *std.mem.Allocator, program: []intcode.Word, noun: intcode.Word, verb: intcode.Word) !intcode.Word {
|
||||
program[1] = noun;
|
||||
program[2] = verb;
|
||||
|
||||
var machine = try intcode.Machine.Run(alloc, program);
|
||||
defer machine.deinit();
|
||||
|
||||
return machine.memory[0];
|
||||
}
|
||||
|
||||
fn runCopy(alloc: *std.mem.Allocator, program: []intcode.Word, noun: intcode.Word, verb: intcode.Word) !intcode.Word {
|
||||
return run(alloc, program, noun, verb);
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
// Part 1
|
||||
std.debug.warn("Day 2, Part 1: {}\n", try runCopy(alloc, program, 12, 2));
|
||||
|
||||
// Part 2: 100*100 = 10,000 combinations to try.
|
||||
var memory: []intcode.Word = try alloc.alloc(intcode.Word, program.len);
|
||||
var done: bool = false;
|
||||
var noun: intcode.Word = 0;
|
||||
var verb: intcode.Word = 0;
|
||||
|
||||
std.debug.warn("Day 2, Part 2: ");
|
||||
while (!done) {
|
||||
std.mem.copy(intcode.Word, memory, program);
|
||||
const result = try run(alloc, memory, noun, verb);
|
||||
|
||||
// Too high: 250800 (noun=33, verb=76)
|
||||
if (result == 19690720) {
|
||||
std.debug.warn("noun={} verb={}\n", noun, verb);
|
||||
done = true;
|
||||
}
|
||||
|
||||
noun += 1;
|
||||
if (noun > 100) {
|
||||
noun = 0;
|
||||
verb += 1;
|
||||
|
||||
if (verb > 100) {
|
||||
std.debug.warn("failed!\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user