8
votes
Day 20: Trench Map
Today's problem description: https://adventofcode.com/2021/day/20
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>
I think I'm 99% of the way there but I don't understand the catch.
Edit: Ultimately got it, phew
Question
The problem doesn't mention anything about how to handle enhancement[0] being #, which would always turn on every pixel in the infinite image. There should be an infinite number of lit pixels after 1 pass--why isn't there? The sample doesn't cover this at all.
Rust
Answer & spoiler
There are , but the sample input was chosen carefully to hide that. In the sample:This means the example images displayed seem as if the surr9ounding image would always be dark.
My real algorithm (and it seems yours too) has a dark pixel surrounded by dark pixel changing to light and a light pixel surrounded by light changed to dark. If you change the sample algorithm to enhancement[0] = "#" and enhancement[511] = "." you would see
You need to find a way to cope with that.
This is pretty much the root of the puzzle. I think every input enhancement string starts with a "#" character.
Without getting too far into spoilers: the edge case that's catching you out is when nine "."s become a "#", but what happens in the infinite expanse of "#"s on the next step?
Thanks, I did just figure it out. Phew. Wasn't ready to spend a lot of time on this one after the last 2 days.
You're right that this was the root of the puzzle--given that, there were effectively no samples for this puzzle.
This was refreshingly easy after that last one!
Part 1 Ruby
The only tricky thing here is to keep a close eye on your puzzle input, especially the "enhancement string".
if your test case passes, but doesn't work on the real input
Pay attention to what's happening in the infinite expanse outside the main area, what happens to a pixel when every value is a `1`, and what happens when every value is a `0`?Part 2 Ruby
A little slow on the runtime here, there's certainly room to optimize, but 15s isn't bad.
Data Structure
I use a struct to store the pixels, the algorithm, image dimensions (as it grows) and the canvas colour.
The algorithm/index is just an array for lookup.
The image is a hash from co-ordinate -> colour.
I only store the lit pixels in the image though, so the colour is always light.
I save the size of the image too since it's needed later (and to display the image)
Part 1
I noticed at the start that my 'algorithm' sets any dark pixel surrounded by dark pixels to light and vice-versa. So I knew I couldn't rely on anything not stored in the pixel hash to be dark.
This means the infinite image was going to change from light to dark so I store the current 'canvas' colour.
Anything out of range of the current image dimensions will be canvas colour and any point not saved in pixels will be dark (since I'm only storing light pixels).
Part 2
CSI levels of enhancement.