Day 11, part 1, but horrible
This commit is contained in:
16
11/build.zig
Normal file
16
11/build.zig
Normal file
@@ -0,0 +1,16 @@
|
||||
const Builder = @import("std").build.Builder;
|
||||
|
||||
pub fn build(b: *Builder) void {
|
||||
const mode = b.standardReleaseOptions();
|
||||
const exe = b.addExecutable("11", "src/main.zig");
|
||||
exe.setBuildMode(mode);
|
||||
exe.addPackagePath("intcode", "../lib/intcode/intcode.zig");
|
||||
|
||||
exe.install();
|
||||
|
||||
const run_cmd = exe.run();
|
||||
run_cmd.step.dependOn(b.getInstallStep());
|
||||
|
||||
const run_step = b.step("run", "Run the app");
|
||||
run_step.dependOn(&run_cmd.step);
|
||||
}
|
1
11/input
Normal file
1
11/input
Normal file
@@ -0,0 +1 @@
|
||||
3,8,1005,8,304,1106,0,11,0,0,0,104,1,104,0,3,8,102,-1,8,10,101,1,10,10,4,10,1008,8,1,10,4,10,1002,8,1,29,2,103,1,10,1,106,18,10,3,8,102,-1,8,10,1001,10,1,10,4,10,1008,8,1,10,4,10,102,1,8,59,2,102,3,10,2,1101,12,10,3,8,102,-1,8,10,1001,10,1,10,4,10,108,0,8,10,4,10,101,0,8,88,3,8,102,-1,8,10,1001,10,1,10,4,10,108,1,8,10,4,10,101,0,8,110,2,108,9,10,1006,0,56,3,8,102,-1,8,10,1001,10,1,10,4,10,108,0,8,10,4,10,101,0,8,139,1,108,20,10,3,8,102,-1,8,10,101,1,10,10,4,10,108,0,8,10,4,10,102,1,8,165,1,104,9,10,3,8,102,-1,8,10,101,1,10,10,4,10,1008,8,0,10,4,10,1001,8,0,192,2,9,14,10,2,1103,5,10,1,1108,5,10,3,8,1002,8,-1,10,101,1,10,10,4,10,1008,8,1,10,4,10,102,1,8,226,1006,0,73,1006,0,20,1,1106,11,10,1,1105,7,10,3,8,102,-1,8,10,1001,10,1,10,4,10,108,0,8,10,4,10,1001,8,0,261,3,8,102,-1,8,10,101,1,10,10,4,10,108,1,8,10,4,10,1002,8,1,283,101,1,9,9,1007,9,1052,10,1005,10,15,99,109,626,104,0,104,1,21101,48062899092,0,1,21101,0,321,0,1105,1,425,21101,936995300108,0,1,21101,0,332,0,1106,0,425,3,10,104,0,104,1,3,10,104,0,104,0,3,10,104,0,104,1,3,10,104,0,104,1,3,10,104,0,104,0,3,10,104,0,104,1,21102,209382902951,1,1,21101,379,0,0,1106,0,425,21102,179544747200,1,1,21102,390,1,0,1106,0,425,3,10,104,0,104,0,3,10,104,0,104,0,21102,1,709488292628,1,21102,1,413,0,1106,0,425,21101,0,983929868648,1,21101,424,0,0,1105,1,425,99,109,2,22101,0,-1,1,21102,40,1,2,21102,456,1,3,21101,446,0,0,1106,0,489,109,-2,2106,0,0,0,1,0,0,1,109,2,3,10,204,-1,1001,451,452,467,4,0,1001,451,1,451,108,4,451,10,1006,10,483,1102,0,1,451,109,-2,2105,1,0,0,109,4,1201,-1,0,488,1207,-3,0,10,1006,10,506,21102,1,0,-3,21202,-3,1,1,21201,-2,0,2,21101,0,1,3,21101,525,0,0,1105,1,530,109,-4,2105,1,0,109,5,1207,-3,1,10,1006,10,553,2207,-4,-2,10,1006,10,553,21202,-4,1,-4,1105,1,621,21201,-4,0,1,21201,-3,-1,2,21202,-2,2,3,21102,1,572,0,1106,0,530,21201,1,0,-4,21101,0,1,-1,2207,-4,-2,10,1006,10,591,21102,0,1,-1,22202,-2,-1,-2,2107,0,-3,10,1006,10,613,22101,0,-1,1,21101,0,613,0,106,0,488,21202,-2,-1,-2,22201,-4,-2,-4,109,-5,2106,0,0
|
122
11/src/main.zig
Normal file
122
11/src/main.zig
Normal file
@@ -0,0 +1,122 @@
|
||||
const std = @import("std");
|
||||
const intcode = @import("intcode");
|
||||
|
||||
const boardX = 1024;
|
||||
const boardY = 1024;
|
||||
|
||||
const Point = struct {
|
||||
x: usize,
|
||||
y: usize,
|
||||
|
||||
fn go(self: Point, d: Compass) Point {
|
||||
var out = Point{.x = self.x, .y = self.y};
|
||||
|
||||
switch (d) {
|
||||
.North => out.y -= 1,
|
||||
.East => out.x += 1,
|
||||
.South => out.y += 1,
|
||||
.West => out.x -= 1,
|
||||
}
|
||||
|
||||
return out;
|
||||
}
|
||||
};
|
||||
|
||||
const Compass = enum(u8) {
|
||||
North = 0,
|
||||
East = 1,
|
||||
South = 2,
|
||||
West = 3,
|
||||
|
||||
fn turn(self: Compass, direction: intcode.Word) Compass {
|
||||
return switch (direction) {
|
||||
0 => self.widdershins(),
|
||||
1 => self.deasil(),
|
||||
else => unreachable,
|
||||
};
|
||||
}
|
||||
|
||||
fn deasil(self: Compass) Compass {
|
||||
if (self == .West) return .North;
|
||||
|
||||
return @intToEnum(Compass, @enumToInt(self)+1);
|
||||
}
|
||||
|
||||
fn widdershins(self: Compass) Compass {
|
||||
if (self == .North) return .West;
|
||||
|
||||
return @intToEnum(Compass, @enumToInt(self)-1);
|
||||
}
|
||||
};
|
||||
|
||||
const Colour = enum(u8) {
|
||||
Black = 0,
|
||||
White = 1,
|
||||
};
|
||||
|
||||
const Cell = struct {
|
||||
colour: Colour,
|
||||
touched: bool = false,
|
||||
};
|
||||
|
||||
const Board = [boardY][boardX]Cell;
|
||||
|
||||
fn runMachine(m: *intcode.Machine) void {
|
||||
if (m.run()) {} else |err| {
|
||||
std.debug.warn("Machine failed to run successfully: {}\n", err);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn main() anyerror!void {
|
||||
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
|
||||
defer arena.deinit();
|
||||
|
||||
const alloc = &arena.allocator;
|
||||
|
||||
var board: Board = undefined;
|
||||
for (board) |*row| {
|
||||
for (row) |*cell| {
|
||||
cell.* = Cell{.colour = .Black, .touched = false};
|
||||
}
|
||||
}
|
||||
|
||||
// Current state of the robot
|
||||
var pos = Point{.x = boardX / 2, .y = boardY / 2};
|
||||
var direction = Compass.North;
|
||||
|
||||
var program = try intcode.loadFromStdIn(alloc);
|
||||
var machine = try intcode.Machine.init(alloc, program);
|
||||
var robot = try std.Thread.spawn(&machine, runMachine);
|
||||
|
||||
// Robot event loop
|
||||
while (!machine.halted) {
|
||||
// Feed it the colour at its current position
|
||||
try machine.writeToInput(@enumToInt(board[pos.y][pos.x].colour));
|
||||
|
||||
// Read back instructions
|
||||
var paintColour = try machine.readFromOutput();
|
||||
var turnDirection = try machine.readFromOutput();
|
||||
|
||||
// Paint
|
||||
board[pos.y][pos.x].colour = @intToEnum(Colour, @intCast(u8, paintColour));
|
||||
board[pos.y][pos.x].touched = true;
|
||||
|
||||
// Turn in the specified direction and move forward one square
|
||||
direction = direction.turn(turnDirection);
|
||||
pos = pos.go(direction);
|
||||
|
||||
// Ugh. Give the other thread some time to decide to halt or not.
|
||||
std.time.sleep(1000);
|
||||
}
|
||||
|
||||
robot.wait();
|
||||
|
||||
var touched: usize = 0;
|
||||
for (board) |row| {
|
||||
for (row) |cell| {
|
||||
if (cell.touched) touched += 1;
|
||||
}
|
||||
}
|
||||
|
||||
std.debug.warn("Day 11, Part 1: {}\n", touched);
|
||||
}
|
@@ -101,6 +101,8 @@ pub const Machine = struct {
|
||||
|
||||
ip: usize = 0,
|
||||
|
||||
halted: bool = false,
|
||||
|
||||
input: CharDev,
|
||||
output: CharDev,
|
||||
|
||||
@@ -257,9 +259,7 @@ pub const Machine = struct {
|
||||
self.rb += self.__read(0, insn.modes[0]);
|
||||
},
|
||||
|
||||
.END => {
|
||||
// std.debug.warn("{}: ended\n", self.idx);
|
||||
},
|
||||
.END => self.halted = true,
|
||||
};
|
||||
|
||||
// Only modify IP if the instruction itself has not
|
||||
|
Reference in New Issue
Block a user