Day 9 - Solutions
So this is the map I got.
omg that was hard, now to reading for other solutions
🥲
No solutions today? I can share the image (working on part 2)
Made it by cheating a little bit... (edit: added a native algorithm without reaching for java2d features) https://github.com/genmeblog/advent-of-code/blob/master/src/advent_of_code_2025/day09.clj
OMG, I just did it! It's ugly as hell and very inefficient, but - it worked on the first try!
Yeah - I realized last night I didn't have a clear solution for part2 and opted for sleep. I have some ideas, but I'm unlikely to be able to get to it today.
I've been trying to adapt the algorithm from https://www.geeksforgeeks.org/dsa/how-to-check-if-a-given-point-lies-inside-a-polygon/, but it gave me the wrong answer. My answer was too high, which tells me the algorithm selected a box that shouldn't have qualified, as opposed to missing the correct answer and returning a smaller box. I suspect this is related to every edge having a slope of either 0 or Inf.
Not perfect and slow (~3 mins on my machine) but gave me the correct answer. I'm basically checking the reactangle for each combination of input coords if the edge of the red/green area is crossing it.
My idea was based on two facts: (1) all vertices are ordered and create a closed path and (2) there is no rectangle outside. So the only rectangles which should be taken into consideration are those which are not intersecting with the path. The truth is that all intersect and the trick was to shrink them a little bit to detach them from the path. All coordinates are integers so it was enough to shrink by 0.1.
With some pmap , I'm finally under 1 second for Part 2. (Without it, I'm around 2.5 seconds)
And now I realized that I can throw out 80% of the things I've written — it is waaay easier to check if a rectangle is inside of a polygon. I guess the input was very nice to us.
The notebook is finally here: https://narimiran.github.io/aoc2025/src/day09/
Finished after the company’s early christmas dinner tonight with the first idea that came to mind (skip boxes that are cut by any segment). Straightforward to code; fast enough, but not very. There does seem a lot of potential for small and large optimisations still. So, I should study the solutions already posted above. https://jurjanpaul.github.io/ape-cljs-playground/?code=KHJlcXVpcmUgJ1tjbG9qdXJlLnNldCA6YXMgY3NldF0pCgooZGVmIGlucHV0CiAgOzsgdG8gYmUgcmVwbGFjZWQgd2l0aCByZWFsIGlucHV0CiAgIjcsMQoxMSwxCjExLDcKOSw3CjksNQoyLDUKMiwzCjcsMyIpCgooZGVmbiBwYXJzZQogIFtpbnB1dF0KICAoLT4%2BIChzdHJpbmcvc3BsaXQtbGluZXMgaW5wdXQpCiAgICAgICAobWFwICMoLT4%2BIChzdHJpbmcvc3BsaXQgJSAjIiwiKQogICAgICAgICAgICAgICAgICAobWFwIHBhcnNlLWxvbmcpKSkpKQoKKGRlZiBwYXJzZWQKICAocGFyc2UgaW5wdXQpKQoKKGRlZiBzZWdtZW50cwogIChtYXAgbGlzdAogICAgICAgcGFyc2VkCiAgICAgICAobmV4dCAoY3ljbGUgcGFyc2VkKSkpKQoKKHByaW50bG4gc2VnbWVudHMpCgooZGVmbiB2KiBbdiBuXQogIChtYXAgKHBhcnRpYWwgKiBuKSB2KSkKCihkZWZuIHYtIFsmIHZzXQogIChhcHBseSBtYXAgLSB2cykpCgooZGVmIGFsbC1wYWlycwogIChmb3IgW1twICYgcmVtYWluaW5nXSAKICAgICAgICAodGFrZS13aGlsZSBzb21lPyAoaXRlcmF0ZSBuZXh0IHBhcnNlZCkpCiAgICAgICAgcSAobmV4dCByZW1haW5pbmcpCiAgICAgICAgOndoZW4gcV0KICAgIFtwIHFdKSkKCihkZWZuIHBhcnQxIFtdCiAgKC0%2BPiBhbGwtcGFpcnMKICAgICAgIChtYXAgKGZuIFtbcCBxXV0KICAgICAgICAgICAgICBbKGFwcGx5ICogKG1hcCAoY29tcCBpbmMgTWF0aC9hYnMpICh2LSBwIHEpKSkgcCBxXSkpCiAgICAgICAoc29ydC1ieSBmaXJzdCA%2BKQogICAgICAgZmlyc3QpKQogCihwcmludGxuICJwYXJ0MToiIChwYXJ0MSkpCgooZGVmbiBvdmVybGFwcz8KICBbW2ExIGIxXSBbYTIgYjJdXQogIChhbmQgKDwgYTEgYjIpCiAgICAgICAoPiBiMSBhMikpKQoKKGRlZm4gc2VnbWVudC1jdXRzLWJveD8KICBbW1tweCBweV0gW3F4IHF5XSA6YXMgYm94XSAKICAgW1theCBheV0gW2J4IGJ5XSA6YXMgc2VnbWVudF1dCiAgKG9yCiAgIChhbmQgKD0gYXggYngpCiAgICAgICAgKDwgKG1pbiBweCBxeCkgYXggKG1heCBweCBxeCkpCiAgICAgICAgKG92ZXJsYXBzPyBbKG1pbiBweSBxeSkgKG1heCBweSBxeSldIAogICAgICAgICAgICAgICAgICAgWyhtaW4gYXkgYnkpIChtYXggYXkgYnkpXSkpCiAgIChhbmQgKD0gYXkgYnkpCiAgICAgICAgKDwgKG1pbiBweSBxeSkgYXkgKG1heCBweSBxeSkpCiAgICAgICAgKG92ZXJsYXBzPyBbKG1pbiBweCBxeCkgKG1heCBweCBxeCldIAogICAgICAgICAgICAgICAgICAgWyhtaW4gYXggYngpIChtYXggYXggYngpXSkpKSkKCihkZWZuIHZhbGlkLXBhaXI%2FCiAgW1twIHFdXQogIChub3QgKHNvbWUgKHBhcnRpYWwgc2VnbWVudC1jdXRzLWJveD8gW3AgcV0pCiAgICAgICAgICAgICBzZWdtZW50cykpKQoKKGRlZm4gcGFydDIgW10KICAoLT4%2BIGFsbC1wYWlycwogICAgICAgKGZpbHRlciB2YWxpZC1wYWlyPykKICAgICAgIChtYXAgKGZuIFtbcCBxXV0KICAgICAgICAgICAgICBbKGFwcGx5ICogKG1hcCAoY29tcCBpbmMgTWF0aC9hYnMpICh2LSBwIHEpKSkgcCBxXSkpCiAgICAgICAoc29ydC1ieSBmaXJzdCA%2BKQogICAgICAgZmlyc3QpKQoKKHByaW50bG4gInBhcnQyOiIgKHBhcnQyKSkK&checksum=MTcxMTU3MDIzNQ%3D%3D
I did see the above visualisation by @tsulej before starting on part 2; that indeed helped a lot to not worry about some potential edge cases that the puzzle text seemed to allow (e.g. segments crossing each other, a potential largest box actually being on the outside of the polygon, etc.)
Compute a set of boxes to cover all the tiles and filter out rectangles that are not made out of those boxes 🙂 https://github.com/benfle/advent-of-code/blob/main/src/advent_of_code/2025/09.clj
Now that I am done with my overly complicated solution I thought of a better one: compute the set of boxes that cover the ground (not tiles) and filter out the rectangles as soon as they intersect with at least one of the ground boxes.
Day 8 Part 1 - Question in thread.
I get that the shortest distance is between (162, 817, 812) -> (425, 690, 689) But then it connects (431, 875, 988), is it connecting this to (162, 817, 812) ? Then it connects (906, 360, 560) and (805,96,715) but doesn't say to where ? I can't make heads nor tails of what this is trying to explain at all.
Or are (906, 360, 560) and (805,96,715) a seperate circuit altogether
and I'm really just trying to go through my calculated distances and make 10 connections using unique junction boxes, even if this means they aren't all connected to each other ?
Yes. Here's how I thought of it --
You have a starting pool of 20 circuits of length 1.
After you add the first pair, you have a pool containing 1 circuit of length 2 (162, 817, 812) -> (425, 690, 689) and 18 circuits of length 1.
The next pair gets added to the existing circuit of length 2. Now your pool is 1 circuit of length 3 (431, 875, 988) -> (162, 817, 812) -> (425, 690, 689) and 17 circuits of length 1.
The third pair (906, 360, 560) -> (805,96,715) creates a new circuit of 2 and your pool is 1 circuit of 3, 1 circuit of 2, and 15 circuits of length 1.