8
votes
Day 8: Space Image Format
Today's problem description: https://adventofcode.com/2019/day/8
Join the Tildes private leaderboard! You can do that on this page, by entering join code 730956-de85ce0c
.
Please post your solutions in your own top-level comment. Here's a template you can copy-paste into your comment to format it nicely, with the code collapsed by default inside an expandable section with syntax highlighting (you can replace python
with any of the "short names" listed in this page of supported languages):
<details>
<summary>Part 1</summary>
```python
Your code here.
```
</details>
Today's seems pretty easy. I accidentally read 'most' instead of 'least' so that threw me off a bit during part 1 and explains why I have functionality that's unnecessary
Part 1 - Python
Literally half this code is unnecessary if you just want the solution and nothing elsePart 2 - Python
I created a string-stand-in the length of the layer and set it all to 'transparent'. Then I looped through the layers in order and replaced any 'transparent' values with either a space or a black block. Once that was done I took that string and formatted it the way it was supposed to be and printed it outNon-spoilery Tip
Depending on how you make the final image be careful. In my example my 'white' 'pixels' showed up as nothing against the white-background, making my left-most 'L' hard to read, because it just make the image look smallerEdit:
I get that this one is relatively easy, but how the actual fuck did someone get both stars in < 4 minutes. I'm convinced that a lot of the scores on the official leader boards are from people who are cheating
Wait, you're supposed to actually color the output? I just output mine in ones and zeroes like in the examples, although I did note that it was a bit hard to read (but easily solvable with a
sed 's/0/ /g'
).Also, considering my solutions are 17 and 28 lines respectively with blank lines and shebang and all, it seems possible for a hardcore programmer to finish it in 4 minutes, although unlikely, perhaps. Either way, I wouldn't worry about the leaderboards, as I mentioned in a previous thread.
I just think that you're supposed to make the output readable, however that works for you.
Remember that 4 minutes also includes reading and understanding the question. It seems more likely to me that that is how long it took them to brute force it
God I love when I can visualize the problem, way more fun! The whole thing only took me 20 minutes, most of that spent not realizing javascript treats text like numbers in pretty much every case except when you expect it to. I also inverted white and black to make it more readable.
Part 1+2
This one felt much simpler than yesterday's problem and is definitely amenable to quick, ad hoc solutions.
C++ solution:
Parts 1/2
The code is basically 3 nested for loops: 1 to go through each layer, 1 to go through each row in each layer, and 1 to go through each column in each row.Part 1: Keep a count of all 0s,1s, and 2s per layer and keep a running minimum where when the number of 0s is less than the min, update the min and remember the number of 1s * the number of 2s in that layer.
Part 2: We're going down layer by layer, so for each pixel in a layer, if that pixel (row/col) is not transparent and our "final image" currently doesn't have anything for that pixel position, stick it in the final image.
Yes we have to do some interpretation, but the final result is definitely good enough to spell something out.
Once done, print the product of the min layer and the final image (with black pixels emphasized).
I was pretty fast (at least for me), but spent 3 hours in part 2, looking for a mistake during decoding. I did finally see that the error was not in the decoding itself, but solely in the output. (Off by one error, causing each row to be slightly shifted, making the image completely unreadable).
Here's the cleaned up swift code:
Part 1 & 2
I started treating the pixels as enum almost from the beginning. Most refactoring I did happened in the layer parsing. I never looked much into ArraySlices and general Array handling in Swift, so I had a pretty ugly index fumbling and Array re-creating solution. After some reading into I think I found a nice solution to chunk the input into layers.
Okay, thankfully today's challenge was pretty easy, so I have time to work on yesterday's part 2. I almost got them both right on my first try, but forgot a closing parenthesis on part 1, and accidentally used
+=
when I meant to do concatenation on part 2.Part 1
Part 2
Also, was I the only one who had trouble reading the image? It's partially because I was expecting numbers for some reason, but I had to pipe it to
sed
to color it in order to see the messageThis was nice and quick, I had fun using a library to pretty-print a colorized version of the output on the console.
Day 8, Part 1 - Crystal
Day 8, Part 2
This was a good one, nice change of pace from the debacles with the Intputer.
Part 1 & 2, Python
My code below has a slightly cleaned up method of output, since I submitted the answer and then realized I was a fool for squinting at a grid of numbers instead of formatting it nicely.