Fixed day10. Well.. got an answer, but it isn't pretty. Hard coded
something, and left a bunch of cruft in. Oh well.
This commit is contained in:
parent
17c5af6d8d
commit
90a32e4439
1 changed files with 40 additions and 57 deletions
|
|
@ -43,6 +43,7 @@ pub fn main() !void {
|
|||
// capture the loop's coordinates in a map
|
||||
var loop = std.ArrayList(Coord).init(allocator);
|
||||
defer loop.deinit();
|
||||
|
||||
try loop.append(startCoord);
|
||||
try loop.append(paths[0]);
|
||||
|
||||
|
|
@ -84,83 +85,65 @@ pub fn main() !void {
|
|||
var visited = std.AutoHashMap(Coord, void).init(allocator);
|
||||
defer visited.deinit();
|
||||
|
||||
var prev = startCoord;
|
||||
for (loop.items) |c| {
|
||||
// find the coord to the "right"
|
||||
var coord: ?Coord = null;
|
||||
if (prev.row < c.row) {
|
||||
coord = Coord{ .row = c.row, .col = c.col - 1 };
|
||||
} else if (prev.row > c.row) {
|
||||
coord = Coord{ .row = c.row, .col = c.col + 1 };
|
||||
} else if (prev.col < c.col) {
|
||||
coord = Coord{ .row = c.row + 1, .col = c.col };
|
||||
} else {
|
||||
coord = Coord{ .row = c.row - 1, .col = c.col };
|
||||
}
|
||||
// scarn left to right, keeping track of if we're "in" or "out" of the loop
|
||||
var numVisited: u16 = 0;
|
||||
for (0..height) |r| {
|
||||
var in: bool = false;
|
||||
var lastAngle: ?u8 = null;
|
||||
|
||||
try visitAllNeighbors(coord.?, &visited, &loop, grid, width, height);
|
||||
prev = c;
|
||||
for (0..width) |c| {
|
||||
const coord = Coord{ .row = r, .col = c };
|
||||
const value = grid[coordToIndex(coord, width)];
|
||||
|
||||
const onLoop = contains(&loop, coord);
|
||||
if (onLoop) {
|
||||
if (value == 'S') {
|
||||
lastAngle = 'L'; // '7' for test data.. TODO don't hard code this
|
||||
} else if (value == '|') { // hit a part of the loop. toggle "in"
|
||||
in = !in;
|
||||
} else if (value == 'L' or value == 'F') { // we're starting to go "along" the loop
|
||||
lastAngle = value;
|
||||
} else if (value == '7' or value == 'J') { // we're coming out of the edge of the loop
|
||||
if ((value == '7' and lastAngle == 'L') or (value == 'J' and lastAngle == 'F')) {
|
||||
in = !in;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!onLoop and in) { // if we aren't on a loop, and inside, add it to the list!
|
||||
numVisited += 1;
|
||||
try visited.put(coord, {});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// looking at my printed grid, there are a bunch of stragglers. Donno how. Let's count them
|
||||
// var stragglers = std.ArrayList(Coord).init(allocator);
|
||||
// defer stragglers.deinit();
|
||||
//
|
||||
// for (grid) |i| {
|
||||
// const c = indexToCoord(i, width);
|
||||
// const ns = neighbors(c, width, height);
|
||||
// var allVisited = true;
|
||||
// for (ns) |n| {
|
||||
// if (n != null and visited.get(n.?) != null) {
|
||||
// allVisited = false;
|
||||
// }
|
||||
// }
|
||||
// if (allVisited) {
|
||||
// try visited.put(c, {});
|
||||
// }
|
||||
// }
|
||||
|
||||
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 {
|
||||
for (grid, 0..) |value, idx| {
|
||||
const c = indexToCoord(idx, width);
|
||||
if (contains(loop, c)) {
|
||||
// std.debug.print("{c}", .{value});
|
||||
std.debug.print(" ", .{});
|
||||
const code: u16 = switch (value) {
|
||||
'-' => 0x2501,
|
||||
'|' => 0x2503,
|
||||
'F' => 0x250F,
|
||||
'7' => 0x2513,
|
||||
'J' => 0x251B,
|
||||
else => 0x2517,
|
||||
};
|
||||
std.debug.print("{u}", .{code});
|
||||
} else if (visited.get(c) != null) {
|
||||
std.debug.print("X", .{});
|
||||
} else if (value == '\n') {
|
||||
std.debug.print("{c}", .{value});
|
||||
} else {
|
||||
// std.debug.print("{c}", .{value});
|
||||
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 {
|
||||
for (ar.items) |i| {
|
||||
if (i.row == item.row and i.col == item.col) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue