Module advent_of_code_2021::day_20 [−][src]
Expand description
This is my solution for Advent of Code - Day 20 - Trench Map
Today was iterating a grid, with the twist being that the puzzle input exhibited behaviour that the sample input didn’t. This feels unfair, maybe it’s just to test debugging skills, but those will be challenged throughout anyway. I don’t like that it seemed to be purposefully setting people up to fail.
Luckily I noticed the trick. The first bit of the puzzle bitmap being set means that all the
infinite co-ordinates beyond the edge of the image data, which are initially set to 0 will all
flip to 1, on each odd iteration. They will then flip back to 0 on even iterations as the
final bit of the bitmap is set to 0. Having spotted this I decided to implement the grid as
a set of co-ordinates that are set within the known central area, the current bounds of the
known area, and the default value of every bit beyond that. Image and the methods
implemented for it make up the bulk of today’s solution.
parse_input takes the first line and transforms it into the bitmap to lookup new pixel
values, then passes the rest of the lines to Image::from to parse the rest into the seed
image. I originally had this inlined in parse_input but extracted it and included variables
for the origin as a way to parse the examples of iterated images in the specification when
writing tests.
Image::map_pixel handles iterating a single pixel by looking up its surrounds, building them
into the bitmap index, and returning the relevant bit. Image::iterate handles a single
iteration of the image. This grows the image area by 1 (only pixels adjacent to the existing
image data will be affected by it), map all the pixels in the new area, and set the new default
value for pixels outside the area. Finally Image::iterate_n iterates the image the required
number of times, two for part one, fifty for part two.
Structs
Represents an image as the set of pixels that are on, the bounds of the current image data, and the default value for pixels outside this area.
Functions
Extract the first line as the bitmap lookup, then delegate parsing the seed image to
Image::from
The entry point for running the solutions with the ‘real’ puzzle input.
