23
๐ฌ - 2026 DAY 3 SOLUTIONS - ๐ฌ
(programming.dev)
An unofficial home for the advent of code community on programming.dev! Other challenges are also welcome!
Advent of Code is an annual Advent calendar of small programming puzzles for a variety of skill sets and skill levels that can be solved in any programming language you like.
Everybody Codes is another collection of programming puzzles with seasonal events.
Solution Threads
| M | T | W | T | F | S | S |
|---|---|---|---|---|---|---|
| 1 | 2 | 3 | 4 | 5 | 6 | |
| 7 | 8 | 9 | 10 | 11 | 12 |
Icon base by Lorc under CC BY 3.0 with modifications to add a gradient
console.log('Hello World')
Nim
Not a hard problem today, but I still managed to over-complicate part 2 in my head. Then it finally clicked, and I solved it basically the same way as part 1.
Part 1 - we find all pairs of corners, consider them as the left side of the square and then transpose them to where the right side of the square should be. Then check if those points do exist. ~~I used hashset here, because I was lazy to do bounds checks, reading original grid is probably faster?~~
Part 2 - same as part 1, but second corner can also be to the left of first corner.
Runtime: ~~24.5 ms~~ 8.6 ms
older slower version
Full code on Codeberg: solution.nim
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!