grid traversal with pattern matching - Day 16 - Advent of Code 2023
Вставка
- Опубліковано 27 сер 2024
- In this episode of the Advent of Code 2023 solutions series, we tackle day 16, The Floor Is Lava, using Ruby. We start by examining the puzzle input, representing a contraption through which a beam of light travels. The beam interacts with different components like mirrors, splitters, empty spaces, etc. Our goal is to follow the path of the beam and count the number of "energized" tiles it travels over.
First, we set up the input grid and beams data structures to track the position and direction of beam segments. We use a while loop to traverse the grid, using pattern matching to check the tile type ahead and turn, split beams, or continue on accordingly. We skip over seen tiles and handle edge cases.
For part 1, we print the number of energized tiles for the given input. For part 2, we maximize energy by checking all possible starting points along the edges. This results in a brute-force approach that takes several minutes to run. We make an optimization to use just 1 set instead of 2 to improve performance.
Overall, this solution leverages pattern matching, recursion, and data structures like arrays and sets. The video explains the code step-by-step and suggests further improvements as well. It's aimed at intermediate Ruby developers looking to strengthen their skills.
Advent of Code: adventofcode.com/
My Solutions: gist.github.co...
Playlist • Advent of Code
#adventofcode #ruby
I ended up using 'sed' to convert every backslash to a 'x' in my input file. I couldn't figure out how to stop Ruby from escaping my backslashes and I think it was because my filename was "input.txt". I just tried it without the extension (just 'input') and it worked fine just like yours!
My part 2 ended up being nearly instant, but I think it's because I used recursion and only sets/hashes. If I didn't hit a stack overflow, I probably would have taken forever to figure out the infinite loop problem! Your methods for handling the turns and special characters was much more elegant than mine, I ended up using nested cases lol
I always manage to learn a lot from watching you work through the problem, thanks for sharing!
The backslash thing is confusing!!
Would love to see your recursive solution if you’re down to share.