Day 5.2
This commit is contained in:
@@ -7,14 +7,26 @@ pub fn main() anyerror!void {
|
|||||||
|
|
||||||
const alloc = &arena.allocator;
|
const alloc = &arena.allocator;
|
||||||
|
|
||||||
var program = try intcode.loadFromStdIn(alloc);
|
var p1 = try intcode.loadFromStdIn(alloc);
|
||||||
var machine = try intcode.Machine.init(alloc, program);
|
var p2 = try alloc.alloc(i32, p1.len);
|
||||||
|
std.mem.copy(i32, p2, p1);
|
||||||
|
|
||||||
try machine.input.append(1); // Air conditioner unit
|
var m1 = try intcode.Machine.init(alloc, p1);
|
||||||
try machine.run();
|
var m2 = try intcode.Machine.init(alloc, p2);
|
||||||
|
|
||||||
std.debug.warn("Part 1\n");
|
std.debug.warn("Part 1\n");
|
||||||
for (machine.output.toSlice()) |value, idx| {
|
try m1.input.append(1); // Air conditioner unit
|
||||||
|
try m1.run();
|
||||||
|
|
||||||
|
for (m1.output.toSlice()) |value, idx| {
|
||||||
|
std.debug.warn("Test {}: {}\n", idx, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
std.debug.warn("\nPart 2\n");
|
||||||
|
try m2.input.append(5); // Thermal radiator controller
|
||||||
|
try m2.run();
|
||||||
|
|
||||||
|
for (m2.output.toSlice()) |value, idx| {
|
||||||
std.debug.warn("Test {}: {}\n", idx, value);
|
std.debug.warn("Test {}: {}\n", idx, value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -35,6 +35,10 @@ const Opcode = enum(u8) {
|
|||||||
MULT = 2,
|
MULT = 2,
|
||||||
IN = 3,
|
IN = 3,
|
||||||
OUT = 4,
|
OUT = 4,
|
||||||
|
JNZ = 5,
|
||||||
|
JZ = 6,
|
||||||
|
LT = 7,
|
||||||
|
EQ = 8,
|
||||||
END = 99,
|
END = 99,
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -73,6 +77,10 @@ const Instruction = struct {
|
|||||||
.MULT => 4,
|
.MULT => 4,
|
||||||
.IN => 2,
|
.IN => 2,
|
||||||
.OUT => 2,
|
.OUT => 2,
|
||||||
|
.JNZ => 3,
|
||||||
|
.JZ => 3,
|
||||||
|
.LT => 4,
|
||||||
|
.EQ => 4,
|
||||||
.END => 1,
|
.END => 1,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@@ -127,8 +135,8 @@ pub const Machine = struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn step(self: *Machine) anyerror!bool {
|
pub fn step(self: *Machine) anyerror!bool {
|
||||||
var program = self.memory;
|
const insn = try Instruction.decode(self.memory[self.ip]);
|
||||||
const insn = try Instruction.decode(program[self.ip]);
|
const start_ip = self.ip;
|
||||||
|
|
||||||
const opcode = switch (insn.op) {
|
const opcode = switch (insn.op) {
|
||||||
.ADD => {
|
.ADD => {
|
||||||
@@ -148,13 +156,49 @@ pub const Machine = struct {
|
|||||||
.IN => {
|
.IN => {
|
||||||
self.__write(0, self.input.orderedRemove(0));
|
self.__write(0, self.input.orderedRemove(0));
|
||||||
},
|
},
|
||||||
|
|
||||||
.OUT => {
|
.OUT => {
|
||||||
try self.output.append(self.__read(0, insn.modes[0]));
|
try self.output.append(self.__read(0, insn.modes[0]));
|
||||||
},
|
},
|
||||||
|
|
||||||
|
.JNZ => {
|
||||||
|
if (self.__read(0, insn.modes[0]) != 0)
|
||||||
|
self.ip = @intCast(usize, self.__read(1, insn.modes[1]));
|
||||||
|
},
|
||||||
|
|
||||||
|
.JZ => {
|
||||||
|
if (self.__read(0, insn.modes[0]) == 0)
|
||||||
|
self.ip = @intCast(usize, self.__read(1, insn.modes[1]));
|
||||||
|
},
|
||||||
|
|
||||||
|
.LT => {
|
||||||
|
const a = self.__read(0, insn.modes[0]);
|
||||||
|
const b = self.__read(1, insn.modes[1]);
|
||||||
|
|
||||||
|
if (a < b) {
|
||||||
|
self.__write(2, 1);
|
||||||
|
} else {
|
||||||
|
self.__write(2, 0);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
.EQ => {
|
||||||
|
const a = self.__read(0, insn.modes[0]);
|
||||||
|
const b = self.__read(1, insn.modes[1]);
|
||||||
|
|
||||||
|
if (a == b) {
|
||||||
|
self.__write(2, 1);
|
||||||
|
} else {
|
||||||
|
self.__write(2, 0);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
.END => {},
|
.END => {},
|
||||||
};
|
};
|
||||||
|
|
||||||
self.ip += insn.size();
|
// Only modify IP if the instruction itself has not
|
||||||
|
if (self.ip == start_ip) self.ip += insn.size();
|
||||||
|
|
||||||
return !insn.halt();
|
return !insn.halt();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
Reference in New Issue
Block a user