Day 2.1, horrible but working

This commit is contained in:
2019-12-03 23:11:58 +00:00
parent 7fb5201a11
commit 67f3c61d57

View File

@@ -1,18 +1,14 @@
const std = @import("std");
const max = 512;
const xOrigin = max / 2;
const yOrigin = max / 2;
const Error = error{ParseError};
const State = enum(u8) {
Empty,
Origin,
Line,
Intersect,
const Point = struct {
X: i32,
Y: i32,
};
const Points = std.ArrayList(Point);
const Direction = enum(u8) {
Up = 'U',
Right = 'R',
@@ -30,11 +26,9 @@ const Direction = enum(u8) {
}
};
const Grid = [max][max]State;
const Step = struct {
d: Direction,
l: u32,
l: i32,
pub fn parse(in: []const u8) anyerror!Step {
if (in.len < 2) {
@@ -43,86 +37,55 @@ const Step = struct {
return Step{
.d = try Direction.parse(in[0]),
.l = try std.fmt.parseInt(u32, in[1..in.len], 10),
.l = try std.fmt.parseInt(i32, in[1..in.len], 10),
};
}
};
const World = struct {
x: u32,
y: u32,
grid: *Grid,
entries: Points,
pos: Point,
pub fn init(self: *World) void {
for (self.grid) |y, y_offset| {
for (y) |x, x_offset| {
self.grid[y_offset][x_offset] = State.Empty;
}
}
}
pub fn display(self: *World) void {
for (self.grid) |y, y_offset| {
for (y) |x, x_offset| {
var chr = switch (x) {
State.Empty => " ",
State.Line => ".",
State.Origin => "o",
State.Intersect => "*",
};
std.debug.warn("{}", chr);
}
std.debug.warn("\n");
}
}
pub fn step(self: *World, in: Step, compare: *Grid) void {
pub fn step(self: *World, in: Step) anyerror!void {
std.debug.warn("Step: {}\n", in);
switch (in.d) {
.Up => {
self.lineY(self.y - in.l, self.y - 1, compare);
self.y -= in.l;
try self.lineY(self.pos.Y - in.l, self.pos.Y - 1);
self.pos.Y -= in.l;
},
.Down => {
self.lineY(self.y + 1, self.y + in.l, compare);
self.y += in.l;
try self.lineY(self.pos.Y + 1, self.pos.Y + in.l);
self.pos.Y += in.l;
},
.Left => {
self.lineX(self.x - in.l, self.x - 1, compare);
self.x -= in.l;
try self.lineX(self.pos.X - in.l, self.pos.X - 1);
self.pos.X -= in.l;
},
.Right => {
self.lineX(self.x + 1, self.x + in.l, compare);
self.x += in.l;
try self.lineX(self.pos.X + 1, self.pos.X + in.l);
self.pos.X += in.l;
},
}
}
fn lineX(self: *World, start: u32, end: u32, compare: *Grid) void {
var x: u32 = start;
fn lineX(self: *World, start: i32, end: i32) anyerror!void {
var x: i32 = start;
while (x <= end) : (x += 1) {
self.fill(x, self.y, compare);
try self.fill(x, self.pos.Y);
}
}
fn lineY(self: *World, start: u32, end: u32, compare: *Grid) void {
var y: u32 = start;
fn lineY(self: *World, start: i32, end: i32) anyerror!void {
var y: i32 = start;
while (y <= end) : (y += 1) {
self.fill(self.x, y, compare);
try self.fill(self.pos.X, y);
}
}
fn fill(self: *World, x: u32, y: u32, compare: *Grid) void {
std.debug.warn("\tFilling {},{}\n", x, y);
var newVal: State = switch (compare[y][x]) {
.Line => .Intersect,
else => .Line,
};
self.grid[y][x] = newVal;
fn fill(self: *World, x: i32, y: i32) anyerror!void {
//self.entries = try self.alloc.realloc(self.entries, self.entries.len+1);
self.entries.appendAssumeCapacity(Point{ .X = x, .Y = y });
}
};
@@ -145,9 +108,11 @@ fn getLine(stream: *std.fs.File.InStream.Stream) anyerror![]Step {
}
fn manhattan(x: i32, y: i32) u32 {
return std.math.absCast(x - xOrigin) + std.math.absCast(y - yOrigin);
return std.math.absCast(x) + std.math.absCast(y);
}
const maxEntries = 1024 * 1024;
pub fn main() anyerror!void {
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
defer arena.deinit();
@@ -156,58 +121,35 @@ pub fn main() anyerror!void {
const file = std.io.getStdIn();
const stream = &file.inStream().stream;
var grid = try alloc.create(Grid);
var world: World = World{
.x = xOrigin,
.y = yOrigin,
.grid = grid,
.entries = try Points.initCapacity(alloc, maxEntries),
.pos = Point{ .X = 0, .Y = 0 },
};
world.init();
var compare = std.mem.dupe(alloc, Grid, grid);
// Line 1
for (try getLine(stream)) |item| {
world.step(item, compare);
try world.step(item);
}
// reset to origin
//std.mem.copy(compare, world.grid);
world.x = xOrigin;
world.y = yOrigin;
const entriesA = world.entries;
world.entries = try Points.initCapacity(alloc, maxEntries);
world.pos = Point{ .X = 0, .Y = 0 };
// Line 2
for (try getLine(stream)) |item| {
world.step(item, compare);
try world.step(item);
}
// Ignore the origin
world.grid[yOrigin][xOrigin] = State.Origin;
const entriesB = world.entries;
// detect collisions
var minMH: u32 = undefined;
var found: bool = false;
for (world.grid) |y, yOff| {
for (y) |x, xOff| {
if (x == State.Intersect) {
const mh = manhattan(@intCast(i32, xOff), @intCast(i32, yOff));
std.debug.warn("Intersect! {},{} : {}\n", xOff, yOff, mh);
if (found) {
if (minMH > mh) {
minMH = mh;
}
} else {
found = true;
minMH = mh;
}
std.debug.warn("A: {} entries\nB: {} entries\n", entriesA.len, entriesB.len);
for (entriesA.toSlice()) |a| {
for (entriesB.toSlice()) |b| {
if (a.X == b.X and a.Y == b.Y) {
std.debug.warn("Match! {} => {}\n", a, manhattan(a.X, a.Y));
}
}
}
if (found) {
std.debug.warn("Smallest manhattan: {}\n", minMH);
}
// world.display();
}