-- CodingAP's Advent of Code Hub --

[Back to Hub]

Advent of Code 2023, Day 16

-- The Floor Will Be Lava --

Leaderboard Positions - Part 1: 5198, Part 2: 4905

I also started this one late as I was playing Mario Party. The local leaderboards are settling any way, and there is a unlikely chance that I will make anything global-wise, so I will accept doing the puzzles an hour later (it still only tryhards at that time, so it shouldn't affect much).

In this puzzle, we are simulating a laser beam that travels through a room of mirrors. The beam has a direction, and each character in the input can affect it. Taken from the prose...

This means that we had multiple beam heads to deal with, which meant I was using an array. I took a queue approach that just held all current beams, then used .shift() to get all the beams until they either hit the edge or repeated a beam path. The reason I had to check for repeating beam paths is that the beams would keep hitting infinite loops, which would cause the program to not end. I just used a set to keep track of x,y,direction. Then, when counting, I removed duplicated positions.

For part 1, we need to see how many tiles the beam starting at the top left corner (0,0) facing right will 'energize'. This was very simple as I just made a movement function for each tile of the grid, which adds either the same beam back into the queue or adds two beams going in different directions (depending on the current tile). This ran fast enough, so part 2 dealt with finding the highest energize count for all the beams coming from the edge. So not only the top left, but the entire left edge (facing right), the right edge (facing left), the top edge (facing down), and the bottom edge (facing up). Also, I had to check the corners twice for each direction (ex: top left facing right AND down).

If it wasn't for the infinite loop and me starting an hour later, I probably would have done better, but alas, I had to beat others in Mario Party (Yoshi ftw).