--- Day 5: Hydrothermal Vents ---

December 5

Listening to: My friends learning Rust


I can already see these puzzles getting more complexe. Hydrothermal vents!

It's really just a line intersection problem, and it's simplified much further by the fact I can just kind of put them all onto a grid and toss the original lines. Even so, just do the horizontal or vertical ones. Pretty simple.

I basically just gathered all of the pairs, used a populated board, and did

// Vertical case
if (x1 == x2) {
    auto pair = std::minmax({y1, y2});
    for (int i = pair.first; i <= pair.second; i++) board[x1][i]++;
}

Only hitch I ran into was the array size. With the example 10x10 grid, it was just fine, but it would always crash on the actual input for a 1000x1000 grid. I guess if it was done globally it's fine, but locally it can't allocate enough memory. Odd.


Big surprise, part two is exactly the same except using the diagonal lines as well. This is pretty good because I can just use the same exact code, but add an extra case where they are diagonal. Still pretty easy, and I can hyper-focus on the additon to the new problem instead.

It was pretty easy, I basically just went from the left to right point, and kept track of which direction things were going.

int len = std::abs(x1 - x2);
int xdir = x1 < x2 ? 1 : -1;
int ydir = y1 < y2 ? 1 : -1;

for (int x = x1, y = y1, i = 0;
        i <= len;
        x += xdir, y += ydir, i++)
        board[x][y]++;

I realize after finishing I could likely adapt the diagonal function to also handle the horizontal and vertical cases too, with xdir or ydir equaling zero. I'm pretty sure the compare function outputs -1, 0, or 1 based on the result, which is pretty much all I would need. Hmm. I'll try that.

int comp (int a, int b) {
    if (a > b) return -1;
    if (a < b) return 1;
    return 0;
}
int len = max(std::abs(x1 - x2), std::abs(y1 - y2));
int xdir = comp(x1, x2);
int ydir = comp(y1, y2);

Yep! Super small tweak, and that same code handles it all!


My solutions for today's puzzles