Add character devices

This commit is contained in:
2019-12-05 22:31:18 +00:00
parent 032451ad74
commit 0ffe0410b4
2 changed files with 43 additions and 21 deletions

View File

@@ -1,24 +1,22 @@
const std = @import("std");
const intcode = @import("intcode");
fn run(program: []i32, noun: i32, verb: i32) i32 {
fn run(alloc: *std.mem.Allocator, program: []i32, noun: i32, verb: i32) !i32 {
program[1] = noun;
program[2] = verb;
var machine = &intcode.Machine{ .memory = program, .ip = 0 };
machine.run();
var machine = try intcode.Machine.Run(alloc, program);
return program[0];
}
fn runCopy(alloc: *std.mem.Allocator, program: []i32, noun: i32, verb: i32) anyerror!i32 {
fn runCopy(alloc: *std.mem.Allocator, program: []i32, noun: i32, verb: i32) !i32 {
var memory = try alloc.alloc(i32, program.len);
defer alloc.free(memory);
std.mem.copy(i32, memory, program);
return run(memory, noun, verb);
return run(alloc, memory, noun, verb);
}
pub fn main() anyerror!void {
@@ -41,7 +39,7 @@ pub fn main() anyerror!void {
std.debug.warn("Part 2: Searching...");
while (!done) {
std.mem.copy(i32, memory, program);
const result = run(memory, noun, verb);
const result = try run(alloc, memory, noun, verb);
// Too high: 250800 (noun=33, verb=76)
if (result == 19690720) {