Day 7.2 using threads
This commit is contained in:
@@ -12,9 +12,6 @@ pub fn loadFromStream(alloc: *std.mem.Allocator, stream: *std.fs.File.InStream.S
|
||||
while (try stream.readUntilDelimiterOrEof(&buf, ',')) |num| {
|
||||
var trimmed = std.mem.trimRight(u8, num, "\r\n");
|
||||
program[i] = try std.fmt.parseInt(i32, trimmed, 10);
|
||||
|
||||
// std.debug.warn("{},", program[i]);
|
||||
|
||||
i += 1;
|
||||
}
|
||||
|
||||
@@ -92,6 +89,7 @@ const Instruction = struct {
|
||||
|
||||
pub const Machine = struct {
|
||||
alloc: *std.mem.Allocator,
|
||||
idx: usize = 0, // handy for debugging
|
||||
|
||||
memory: []i32,
|
||||
|
||||
@@ -144,20 +142,22 @@ pub const Machine = struct {
|
||||
return self.__out(self.output, val);
|
||||
}
|
||||
|
||||
inline fn __in(self: *Machine, dev: CharDev) !i32 {
|
||||
fn __in(self: *Machine, dev: CharDev) !i32 {
|
||||
const fd = dev[0]; // Read from the pipe
|
||||
var store: [4]u8 = undefined;
|
||||
|
||||
// std.debug.warn("{}: __in({} <- {})\n", self.idx, dev[0], dev[1]);
|
||||
var n = try std.os.read(fd, &store);
|
||||
std.debug.assert(n == 4); // TODO: handle partial reads
|
||||
|
||||
return std.mem.readIntNative(i32, &store);
|
||||
}
|
||||
|
||||
inline fn __out(self: *Machine, dev: CharDev, val: i32) !void {
|
||||
fn __out(self: *Machine, dev: CharDev, val: i32) !void {
|
||||
const fd = dev[1]; // Write to the pipe
|
||||
const bytes = std.mem.asBytes(&val);
|
||||
|
||||
// std.debug.warn("{}: __out({} -> {}, {})\n", self.idx, dev[1], dev[0], val);
|
||||
try std.os.write(fd, bytes);
|
||||
}
|
||||
|
||||
@@ -236,7 +236,9 @@ pub const Machine = struct {
|
||||
}
|
||||
},
|
||||
|
||||
.END => {},
|
||||
.END => {
|
||||
// std.debug.warn("{}: ended\n", self.idx);
|
||||
},
|
||||
};
|
||||
|
||||
// Only modify IP if the instruction itself has not
|
||||
|
Reference in New Issue
Block a user