Add '2019/' from commit 'fc21396bc86bc0f706225f4f6e1d8294d344ca53'

git-subtree-dir: 2019
git-subtree-mainline: f1be11fca8
git-subtree-split: fc21396bc8
This commit is contained in:
2022-01-09 17:07:24 +00:00
36 changed files with 2978 additions and 0 deletions

56
2019/02/src/main.zig Normal file
View 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");
}
}
}
}