Day 12: Christmas Tree Farm
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
18 Comments
mykl@lemmy.world · 5 pts · 248d
Obviously very spoilery code, if you dare read it closely. try it here
It feels very anticlimactic to be ending so early in the month, but see you all next year!
Pyro@programming.dev · 4 pts · 248d
Python
You got me good, Eric.
::: spoiler hint Think of the simplest check you can make for a region. :::
::: spoiler click to view code
:::
Deebster@programming.dev · 4 pts · 248d
Mild spoilers ahead, but you're reading the solutions thread.
I was just doing some preliminary checking of the data on my phone with Nushell (to see how much my ageing laptop would suffer) when I discovered there weren't any non trivial cases.
Normally I get the test data working before trying the input data, this is definitely teaching me the value of investigating the data before heading down into the code mines.
Unfortunately I can't get the second star yet because I missed a few days.
Pyro@programming.dev · 2 pts · 248d
I actually did a lot of optimizations before I had to give up and search what I was missing. I did have a look at my input data, but still wouldn't make the connection because in my mind, any solution had to work for the sample too.
Deebster@programming.dev · 2 pts · 248d
Yeah, it's quite a mean trick really - kinda a big middle finger to anyone who does TDD
CameronDev@programming.dev · 2 pts · 248d
::: spoiler spoiler I had an feeling it would be trivial, but didnt think it would be that trivial. I got the answer without even parsing the shapes, just called them all 9. :::
Pyro@programming.dev · 2 pts · 248d
::: spoiler Tap for spoiler That's very interesting because that was one of my early approaches too, but it actually gave me the wrong answer for my input! :::
addie@feddit.uk · 3 pts · 247d
C++
Hah, got me good too Mr Wastl. Was wondering how long this could possibly take to run - the worst-case is very bad indeed. This prints out the first "fit" it finds, for verification purposes, and keeps the successful ones in case they're needed for "part 2".
Merry Xmas, everyone.
CameronDev@programming.dev · 2 pts · 247d
Ha, that is beautiful. How long did it run for?
addie@feddit.uk · 3 pts · 247d
For the test cases (ironically, the only difficult ones) it finds the solution for 1 basically instantly, 2 in 0.2 seconds, and checks all possibilities for 3 in about 18 seconds before rejecting it.
For the thousand 'puzzle' cases, about half are rejected since the area is simply too small for the presents in an instant. A possible solution is found for all of them in about 80 seconds, so about five per second.
I was concerned that the puzzle cases were about four times the area, and some of them had 255 presents (which might be the maximum stack depth in some languages - not C++, though). So maybe some of them would find a solution quickly, and excluding the worst might take ~ 4 times the size * 30 times the presents * 20 seconds = the best part of an hour. Multithreading it and hoping not too many of them were 'worst case', and I could leave my computer on overnight number-crunching it. Box packing is NP-complete and we need a 'true' answer rather than an approximation, so I couldn't see any better way of doing it than checking every possibility. Sorting the list didn't really show any evidence of puzzles that could be 'pruned' - areas that were the same but with increasing numbers of presents, say, so that you could reject the larger ones after the first failure.
Was kicking myself when it ran so quickly.
mr_satan@lemmy.zip · 3 pts · 213d
I know I'm late, but I'd thought, I'd share some of the visualizations I wanted to see. The code is woefully inefficient running on a 2D array with some greedy heuristics
Here's my shapes with symmetries:

Here's some of the solutions:





The visuals are terminal based, for simple cells I used two spaces with colored background and for solutions I also used braille symbols to better delineate distinctions between cells.
Pyro@programming.dev · 2 pts · 180d
Really cool visualizations!
mr_satan@lemmy.zip · 2 pts · 180d
After archiving the repo I figured that I could've filled internal corners in tiles with braille symbols but oh well.
chunkystyles@sopuli.xyz · 3 pts · 248d
Kotlin
Looking at the puzzle, I knew that I had no clue how to solve it. So I came here to see if I was missing something or if there were any hints.
And the hint I saw was to do the simplest check possible, so I gave it a shot.
And that got the test input wrong, but I ran it against the real input anyway just to see if it was right. And it was.
I think if I had gone on my instincts and just tried to solve this, I could have gone around in circles for hours or days trying to get it right.
Pyro@programming.dev · 2 pts · 247d
I struggled with optimizing this puzzle and had to go online to figure it out too, so I'm glad my hint helped you out.
CameronDev@programming.dev · 2 pts · 248d
Rust
Its not Christmas, but this one was a gift :D. Merry Christmas all, thanks for everyone who has contributed solutions!
Rest easy little advent bot, your work is done now.
::: spoiler spoiler
:::
Camille@lemmy.ml · 2 pts · 248d
Go
Well... I was about to dive into bin packing and stuff. I started by pruning the obvious candidates (those which can fit all the shapes one next to the other, without more computation) so save CPU time for the real stuff. I ran my code on the real input just to see and... what to do mean there are no candidate left? I write the number of obvious boys into the website's input box, just to check and... Ah. I understand why people on reddit said they felt dirty x)
Anyway the code:
::: spoiler day12.go
:::
Avicenna@programming.dev · 2 pts · 243d
After reading multiple papers on stuff like polyomino and coverings etc over the weekend, I sat down to formulate an ILP approach. All the way through I had at the back of my mind "surely he would not expect people to solve something which requires reading research papers, there must be some angle to this which makes it easier". I don't think I have ever been more right in my life and I am really glad I made the obvious fail and succeed checks based on areas lol.