Day 3: Foggy Forest
Megathread guidelines
- Keep top level comments as only solutions, if you want to say something other than a solution put it in a new post. (replies to comments can be whatever)
- You can send code in code blocks by using three backticks, the code, and then three backticks or use something such as https://topaz.github.io/paste/ if you prefer sending it through a URL
FAQ
- What is this?: Here is a post with a large amount of details: https://programming.dev/post/6637268
- Where do I participate?: https://adventofcode.com/
- Is there a leaderboard for the community?: We have a programming.dev leaderboard with the info on how to join in this post: https://programming.dev/post/6631465


I like this approach much better than mine, I waste so much time walking around the grid to find the next corner, instead of checking the known trees.
Indeed; very nice. Saves wasting loads of time checking for ‘every possible grid and skew’ like I was doing, since it only checks possibilities, and would scale very well to truly enormous grids of trees.
I tried it and my code got slower :(
Mostly because the hashset lookup is just brutally slow for some reason.
You’re probably doing way too many hashing checks, my version prunes a lot of incorrect pairs (where second corner is above or to the right of the first) cheaply.
But you also don’t need hashset here at all, you can just index into the existing grid e.g.
grid[y][x] == '#'.Priming the hash checks didnt help, but now you mention it, just indexing into the grid is very obvious…
Edit: down to 22ms, completely dropped hashset. Still not quite happy, but getting there!