Shoelace, Pick, and Enumerators - Day 18 - Advent of Code 2023
Вставка
- Опубліковано 11 жов 2024
- In this Advent of Code 2023 video, you’ll learn how to solve the "Lava Duct Lagoon" puzzle from Day 18. We walk through calculating the area of a complex polygon shape based on a set of digging instructions.
The key techniques and concepts covered are:
Parsing direction, step count, and color instructions
Visualizing the polygon by drawing it on a grid
Applying the Shoelace Formula to calculate interior area
Using Pick's Theorem to count interior dots
Switching to an Enumerator/Generator for better performance
Yielding instructions instead of materializing a huge array
Keeping a wall-length counter to enable Pick's Theorem
We’ll start with the sample input, walking through the instructions visually, then extend it to handle the full input efficiently using enumerators.
Shoelace Formula en.wikipedia.o...
Pick's theorem en.wikipedia.o...
Advent of Code: adventofcode.com/
My Solutions: gist.github.co...
Playlist • Advent of Code
#adventofcode #ruby
Uhm, there is no need to build the wall one block a time. What you can do just jump directly to the next coordinate pair by just doing [x + dx * dist, y + dy * dist] where dy is the direction and dist is the lenght given in the instructions (what you call steps). You will only have about half a thousand of the coordinate pairs. The lenght of the wall you can get just by adding all the distances from the instructions. Runs in a fraction of a second.
Oh man! Thanks for pointing this out! Very cool.