Files
advent-of-code/2019/03/src/main.zig

161 lines
3.7 KiB
Zig
Raw Normal View History

2019-12-03 22:34:17 +00:00
const std = @import("std");
const Error = error{ParseError};
2019-12-03 23:11:58 +00:00
const Point = struct {
X: i32,
Y: i32,
2019-12-03 23:45:30 +00:00
Steps: i32, // number of steps taken to reach this point
2019-12-03 22:34:17 +00:00
};
2019-12-03 23:11:58 +00:00
const Points = std.ArrayList(Point);
2019-12-03 22:34:17 +00:00
const Direction = enum(u8) {
Up = 'U',
Right = 'R',
Down = 'D',
Left = 'L',
pub fn parse(in: u8) anyerror!Direction {
return switch (in) {
'U' => .Up,
'R' => .Right,
'D' => .Down,
'L' => .Left,
else => Error.ParseError,
};
}
};
const Step = struct {
d: Direction,
2019-12-03 23:11:58 +00:00
l: i32,
2019-12-03 22:34:17 +00:00
pub fn parse(in: []const u8) anyerror!Step {
if (in.len < 2) {
return error.ParseError;
}
return Step{
.d = try Direction.parse(in[0]),
2019-12-03 23:11:58 +00:00
.l = try std.fmt.parseInt(i32, in[1..in.len], 10),
2019-12-03 22:34:17 +00:00
};
}
};
const World = struct {
2019-12-03 23:11:58 +00:00
entries: Points,
2019-12-03 23:45:30 +00:00
ticks: i32,
x: i32,
y: i32,
2019-12-03 22:34:17 +00:00
2019-12-03 23:45:30 +00:00
pub fn step(self: *World, in: Step) void {
std.debug.warn("Step {}: {}\n", self.ticks, in);
2019-12-03 22:34:17 +00:00
2019-12-03 23:45:30 +00:00
var i: i32 = 0;
2019-12-03 22:34:17 +00:00
switch (in.d) {
.Up => {
2019-12-03 23:45:30 +00:00
while (i < in.l) : (i += 1) {
self.y -= 1;
self.fill();
}
2019-12-03 22:34:17 +00:00
},
.Down => {
2019-12-03 23:45:30 +00:00
while (i < in.l) : (i += 1) {
self.y += 1;
self.fill();
}
2019-12-03 22:34:17 +00:00
},
.Left => {
2019-12-03 23:45:30 +00:00
while (i < in.l) : (i += 1) {
self.x -= 1;
self.fill();
}
2019-12-03 22:34:17 +00:00
},
.Right => {
2019-12-03 23:45:30 +00:00
while (i < in.l) : (i += 1) {
self.x += 1;
self.fill();
}
2019-12-03 22:34:17 +00:00
},
}
}
2019-12-03 23:45:30 +00:00
fn fill(self: *World) void {
self.ticks += 1;
2019-12-03 22:34:17 +00:00
2019-12-03 23:45:30 +00:00
const point = Point{ .X = self.x, .Y = self.y, .Steps = self.ticks };
std.debug.warn("\t{}\n", point);
self.entries.appendAssumeCapacity(point);
2019-12-03 22:34:17 +00:00
}
};
fn getLine(stream: *std.fs.File.InStream.Stream) anyerror![]Step {
var buf: [10240]u8 = undefined;
var out: [1000]Step = undefined;
var i: u32 = 0;
var line = (try stream.readUntilDelimiterOrEof(&buf, '\n')) orelse return Error.ParseError;
var iter = std.mem.separate(line, ",");
while (iter.next()) |value| {
std.debug.assert(i < out.len);
out[i] = try Step.parse(value);
i += 1;
}
return out[0..i];
}
fn manhattan(x: i32, y: i32) u32 {
2019-12-03 23:11:58 +00:00
return std.math.absCast(x) + std.math.absCast(y);
2019-12-03 22:34:17 +00:00
}
2019-12-03 23:11:58 +00:00
const maxEntries = 1024 * 1024;
2019-12-03 22:34:17 +00:00
pub fn main() anyerror!void {
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
defer arena.deinit();
const alloc = &arena.allocator;
const file = std.io.getStdIn();
const stream = &file.inStream().stream;
var world: World = World{
2019-12-03 23:11:58 +00:00
.entries = try Points.initCapacity(alloc, maxEntries),
2019-12-03 23:45:30 +00:00
.ticks = 0,
.x = 0,
.y = 0,
2019-12-03 22:34:17 +00:00
};
// Line 1
for (try getLine(stream)) |item| {
2019-12-03 23:45:30 +00:00
world.step(item);
2019-12-03 22:34:17 +00:00
}
// reset to origin
2019-12-03 23:11:58 +00:00
const entriesA = world.entries;
world.entries = try Points.initCapacity(alloc, maxEntries);
2019-12-03 23:45:30 +00:00
world.ticks = 0;
world.x = 0;
world.y = 0;
2019-12-03 22:34:17 +00:00
// Line 2
for (try getLine(stream)) |item| {
2019-12-03 23:45:30 +00:00
world.step(item);
2019-12-03 22:34:17 +00:00
}
2019-12-03 23:11:58 +00:00
const entriesB = world.entries;
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) {
2019-12-03 23:45:30 +00:00
std.debug.warn("Match! {}, {} => {} : {}\n", a, b, manhattan(a.X, a.Y), a.Steps + b.Steps);
2019-12-03 22:34:17 +00:00
}
}
}
}