|
|
@ -43,6 +43,7 @@ pub fn main() !void {
|
|
|
|
// capture the loop's coordinates in a map
|
|
|
|
// capture the loop's coordinates in a map
|
|
|
|
var loop = std.ArrayList(Coord).init(allocator);
|
|
|
|
var loop = std.ArrayList(Coord).init(allocator);
|
|
|
|
defer loop.deinit();
|
|
|
|
defer loop.deinit();
|
|
|
|
|
|
|
|
|
|
|
|
try loop.append(startCoord);
|
|
|
|
try loop.append(startCoord);
|
|
|
|
try loop.append(paths[0]);
|
|
|
|
try loop.append(paths[0]);
|
|
|
|
|
|
|
|
|
|
|
@ -84,83 +85,65 @@ pub fn main() !void {
|
|
|
|
var visited = std.AutoHashMap(Coord, void).init(allocator);
|
|
|
|
var visited = std.AutoHashMap(Coord, void).init(allocator);
|
|
|
|
defer visited.deinit();
|
|
|
|
defer visited.deinit();
|
|
|
|
|
|
|
|
|
|
|
|
var prev = startCoord;
|
|
|
|
// scarn left to right, keeping track of if we're "in" or "out" of the loop
|
|
|
|
for (loop.items) |c| {
|
|
|
|
var numVisited: u16 = 0;
|
|
|
|
// find the coord to the "right"
|
|
|
|
for (0..height) |r| {
|
|
|
|
var coord: ?Coord = null;
|
|
|
|
var in: bool = false;
|
|
|
|
if (prev.row < c.row) {
|
|
|
|
var lastAngle: ?u8 = null;
|
|
|
|
coord = Coord{ .row = c.row, .col = c.col - 1 };
|
|
|
|
|
|
|
|
} else if (prev.row > c.row) {
|
|
|
|
for (0..width) |c| {
|
|
|
|
coord = Coord{ .row = c.row, .col = c.col + 1 };
|
|
|
|
const coord = Coord{ .row = r, .col = c };
|
|
|
|
} else if (prev.col < c.col) {
|
|
|
|
const value = grid[coordToIndex(coord, width)];
|
|
|
|
coord = Coord{ .row = c.row + 1, .col = c.col };
|
|
|
|
|
|
|
|
} else {
|
|
|
|
const onLoop = contains(&loop, coord);
|
|
|
|
coord = Coord{ .row = c.row - 1, .col = c.col };
|
|
|
|
if (onLoop) {
|
|
|
|
}
|
|
|
|
if (value == 'S') {
|
|
|
|
|
|
|
|
lastAngle = 'L'; // '7' for test data.. TODO don't hard code this
|
|
|
|
try visitAllNeighbors(coord.?, &visited, &loop, grid, width, height);
|
|
|
|
} else if (value == '|') { // hit a part of the loop. toggle "in"
|
|
|
|
prev = c;
|
|
|
|
in = !in;
|
|
|
|
}
|
|
|
|
} else if (value == 'L' or value == 'F') { // we're starting to go "along" the loop
|
|
|
|
|
|
|
|
lastAngle = value;
|
|
|
|
// looking at my printed grid, there are a bunch of stragglers. Donno how. Let's count them
|
|
|
|
} else if (value == '7' or value == 'J') { // we're coming out of the edge of the loop
|
|
|
|
// var stragglers = std.ArrayList(Coord).init(allocator);
|
|
|
|
if ((value == '7' and lastAngle == 'L') or (value == 'J' and lastAngle == 'F')) {
|
|
|
|
// defer stragglers.deinit();
|
|
|
|
in = !in;
|
|
|
|
//
|
|
|
|
}
|
|
|
|
// for (grid) |i| {
|
|
|
|
}
|
|
|
|
// const c = indexToCoord(i, width);
|
|
|
|
}
|
|
|
|
// const ns = neighbors(c, width, height);
|
|
|
|
|
|
|
|
// var allVisited = true;
|
|
|
|
if (!onLoop and in) { // if we aren't on a loop, and inside, add it to the list!
|
|
|
|
// for (ns) |n| {
|
|
|
|
numVisited += 1;
|
|
|
|
// if (n != null and visited.get(n.?) != null) {
|
|
|
|
try visited.put(coord, {});
|
|
|
|
// allVisited = false;
|
|
|
|
}
|
|
|
|
// }
|
|
|
|
}
|
|
|
|
// }
|
|
|
|
}
|
|
|
|
// if (allVisited) {
|
|
|
|
|
|
|
|
// try visited.put(c, {});
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
printGrid(grid, width, visited, &loop);
|
|
|
|
printGrid(grid, width, visited, &loop);
|
|
|
|
std.debug.print("Part 2: {any}\n", .{visited.count()});
|
|
|
|
std.debug.print("Part 2: {any}\n", .{numVisited});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fn printGrid(grid: []const u8, width: usize, visited: anytype, loop: anytype) void {
|
|
|
|
fn printGrid(grid: []const u8, width: usize, visited: anytype, loop: anytype) void {
|
|
|
|
for (grid, 0..) |value, idx| {
|
|
|
|
for (grid, 0..) |value, idx| {
|
|
|
|
const c = indexToCoord(idx, width);
|
|
|
|
const c = indexToCoord(idx, width);
|
|
|
|
if (contains(loop, c)) {
|
|
|
|
if (contains(loop, c)) {
|
|
|
|
// std.debug.print("{c}", .{value});
|
|
|
|
const code: u16 = switch (value) {
|
|
|
|
std.debug.print(" ", .{});
|
|
|
|
'-' => 0x2501,
|
|
|
|
|
|
|
|
'|' => 0x2503,
|
|
|
|
|
|
|
|
'F' => 0x250F,
|
|
|
|
|
|
|
|
'7' => 0x2513,
|
|
|
|
|
|
|
|
'J' => 0x251B,
|
|
|
|
|
|
|
|
else => 0x2517,
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
std.debug.print("{u}", .{code});
|
|
|
|
} else if (visited.get(c) != null) {
|
|
|
|
} else if (visited.get(c) != null) {
|
|
|
|
std.debug.print("X", .{});
|
|
|
|
std.debug.print("X", .{});
|
|
|
|
} else if (value == '\n') {
|
|
|
|
} else if (value == '\n') {
|
|
|
|
std.debug.print("{c}", .{value});
|
|
|
|
std.debug.print("{c}", .{value});
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
// std.debug.print("{c}", .{value});
|
|
|
|
|
|
|
|
std.debug.print(".", .{});
|
|
|
|
std.debug.print(".", .{});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fn visitAllNeighbors(c: Coord, visited: anytype, loop: anytype, grid: []const u8, width: usize, height: usize) !void {
|
|
|
|
|
|
|
|
// if it's been visited, get out
|
|
|
|
|
|
|
|
if (visited.get(c) != null) {
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (contains(loop, c)) {
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try visited.put(c, {});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const ns = neighbors(c, width, height);
|
|
|
|
|
|
|
|
for (ns) |neighbor| {
|
|
|
|
|
|
|
|
if (neighbor) |n| {
|
|
|
|
|
|
|
|
try visitAllNeighbors(n, visited, loop, grid, width, height);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fn contains(ar: *std.ArrayList(Coord), item: Coord) bool {
|
|
|
|
fn contains(ar: *std.ArrayList(Coord), item: Coord) bool {
|
|
|
|
for (ar.items) |i| {
|
|
|
|
for (ar.items) |i| {
|
|
|
|
if (i.row == item.row and i.col == item.col) {
|
|
|
|
if (i.row == item.row and i.col == item.col) {
|
|
|
|