<- back

advent of code - day 10

2023-12-10

See the puzzle here.

See my full solution here.

Part 1

I think it would be easy to over-complicate part 1, but all I needed to do was look at the "pipe" at the current position and determine which direction to go. I kept track of the previous position so I could determine which way to go next (since we can't go backward).

if(pipe === 'F' && (previousPos === undefined || previousPos.row === currentPos.row)) {
 // Go down
 currentPos.row++;
} else if(pipe === 'F') {
 // Go right
 currentPos.col++;
} else if(pipe === '|' && (previousPos === undefined || previousPos.row === currentPos.row - 1)) {
 // Go down
 currentPos.row++;
} else if (pipe === '|') {
 // Go up
 currentPos.row--;
} else if(pipe === '7' && (previousPos === undefined || previousPos.row === currentPos.row)) {
 // Go down
 currentPos.row++;
} else if(pipe === '7') {
 // Go left
 currentPos.col--;
} else if(pipe === 'L' && (previousPos === undefined || previousPos.row === currentPos.row)) {
 // Go up
 currentPos.row--;
} else if(pipe === 'L') {
 // Go right
 currentPos.col++;
} else if(pipe === 'J' && (previousPos === undefined || previousPos.row === currentPos.row)) {
 // Go up
 currentPos.row--;
} else if(pipe === 'J') {
 // Go left
 currentPos.col--;
} else if(pipe === '-' && (previousPos === undefined || previousPos.col === currentPos.col - 1)) {
 // Go right
 currentPos.col++;
} else if (pipe === '-') {
 // Go left
 currentPos.col--;
}

Every time you take a step, you increment a counter, and then the answer at the end is this distance divided by 2, since halfway through the loop is always the farthest point.

Part 2

In short, part 2 was insane.

In this part of the challenge, we had to find how many squares are "inside" of the main loop of pipe. Even more challenging: spaces that are between adjacent/parallel pipes are considered "outside" the loop.

I feel like there might be a lot of creative solutions for this problem, but what I landed on was "blowing up" the pipe map so that the tight spaces between parallel pipes were actual squares that could be considered part of any traversing algorithm. Any square I added this way I marked with an X (unless it was an extension of a pipe) so that it wouldn't be counted at the end.

I surrounded the map with O's, indicating "outside" and then I alternated between starting from the top or starting from the bottom and turned .'s into O's as I found they were adjacent. I kept looping this way until the count of inside spaces stopped changing.