Day 11, part 2
This commit is contained in:
@@ -1,8 +1,8 @@
|
||||
const std = @import("std");
|
||||
const intcode = @import("intcode");
|
||||
|
||||
const boardX = 1024;
|
||||
const boardY = 1024;
|
||||
const boardX = 155;
|
||||
const boardY = 100;
|
||||
|
||||
const Point = struct {
|
||||
x: usize,
|
||||
@@ -67,11 +67,8 @@ fn runMachine(m: *intcode.Machine) void {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn main() anyerror!void {
|
||||
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
|
||||
defer arena.deinit();
|
||||
|
||||
const alloc = &arena.allocator;
|
||||
fn run(alloc: *std.mem.Allocator, program: []intcode.Word, part1: bool) !void {
|
||||
const part2 = !part1;
|
||||
|
||||
var board: Board = undefined;
|
||||
for (board) |*row| {
|
||||
@@ -84,8 +81,11 @@ pub fn main() anyerror!void {
|
||||
var pos = Point{.x = boardX / 2, .y = boardY / 2};
|
||||
var direction = Compass.North;
|
||||
|
||||
var program = try intcode.loadFromStdIn(alloc);
|
||||
// Start on a white panel in part 2
|
||||
if (part2) board[pos.y][pos.x].colour = .White;
|
||||
|
||||
var machine = try intcode.Machine.init(alloc, program);
|
||||
defer machine.deinit();
|
||||
var robot = try std.Thread.spawn(&machine, runMachine);
|
||||
|
||||
// Robot event loop
|
||||
@@ -111,12 +111,36 @@ pub fn main() anyerror!void {
|
||||
|
||||
robot.wait();
|
||||
|
||||
var touched: usize = 0;
|
||||
for (board) |row| {
|
||||
for (row) |cell| {
|
||||
if (cell.touched) touched += 1;
|
||||
if (part1) {
|
||||
var touched: usize = 0;
|
||||
for (board) |row| {
|
||||
for (row) |cell| {
|
||||
if (cell.touched) touched += 1;
|
||||
}
|
||||
}
|
||||
|
||||
std.debug.warn("Day 11, Part 1: {}\n", touched);
|
||||
} else {
|
||||
for (board) |row| {
|
||||
for (row) |cell| {
|
||||
switch (cell.colour) {
|
||||
.Black => std.debug.warn(" "),
|
||||
.White => std.debug.warn("#"),
|
||||
}
|
||||
}
|
||||
std.debug.warn("\n");
|
||||
}
|
||||
}
|
||||
|
||||
std.debug.warn("Day 11, Part 1: {}\n", touched);
|
||||
}
|
||||
|
||||
pub fn main() anyerror!void {
|
||||
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
|
||||
defer arena.deinit();
|
||||
|
||||
const alloc = &arena.allocator;
|
||||
|
||||
var program = try intcode.loadFromStdIn(alloc);
|
||||
|
||||
try run(alloc, program, true);
|
||||
try run(alloc, program, false);
|
||||
}
|
||||
|
Reference in New Issue
Block a user