Refactor intcode in preparation for day 5
This commit is contained in:
@@ -1,6 +1,24 @@
|
||||
const std = @import("std");
|
||||
const intcode = @import("intcode");
|
||||
|
||||
fn run(program: []i32, noun: i32, verb: i32) i32 {
|
||||
program[1] = noun;
|
||||
program[2] = verb;
|
||||
|
||||
intcode.run(program);
|
||||
|
||||
return program[0];
|
||||
}
|
||||
|
||||
fn runCopy(alloc: *std.mem.Allocator, program: []i32, noun: i32, verb: i32) anyerror!i32 {
|
||||
var memory = try alloc.alloc(i32, program.len);
|
||||
defer alloc.free(memory);
|
||||
|
||||
std.mem.copy(i32, memory, program);
|
||||
|
||||
return run(memory, noun, verb);
|
||||
}
|
||||
|
||||
pub fn main() anyerror!void {
|
||||
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
|
||||
defer arena.deinit();
|
||||
@@ -10,18 +28,18 @@ pub fn main() anyerror!void {
|
||||
var program = try intcode.loadFromStdIn(alloc);
|
||||
|
||||
// Part 1
|
||||
std.debug.warn("Part 1: {}\n", try intcode.runCopy(alloc, program, 12, 2));
|
||||
std.debug.warn("Part 1: {}\n", try runCopy(alloc, program, 12, 2));
|
||||
|
||||
// Part 2: 100*100 = 10,000 combinations to try.
|
||||
var memory: []u32 = try alloc.alloc(u32, program.len);
|
||||
var memory: []i32 = try alloc.alloc(i32, program.len);
|
||||
var done: bool = false;
|
||||
var noun: u32 = 0;
|
||||
var verb: u32 = 0;
|
||||
var noun: i32 = 0;
|
||||
var verb: i32 = 0;
|
||||
|
||||
std.debug.warn("Part 2: Searching...");
|
||||
while (!done) {
|
||||
std.mem.copy(u32, memory, program);
|
||||
const result = try intcode.run(memory, noun, verb);
|
||||
std.mem.copy(i32, memory, program);
|
||||
const result = run(memory, noun, verb);
|
||||
|
||||
// Too high: 250800 (noun=33, verb=76)
|
||||
if (result == 19690720) {
|
||||
|
Reference in New Issue
Block a user