Module advent_of_code_2021::day_13 [−][src]
Expand description
This is my solution for Advent of Code - Day 13 - Transparent Origami
The hardest part of today’s solution was parsing the input. I opted for a HashSet of
co-ordinates rather than representing the full grid as the dots were very sparse. I also created
a simple enum Axis to track the axis of each fold. parse_input is mostly checks to
confirm each line confirms to spec, as Rust is very explicit about making you handle everything.
apply_fold iterates through the dots adding them to a new folded set. If they are left of /
above the relevant axis, they are inserted as is, otherwise the new position is calculated and
inserted. The len() of the resulting set when applying the first fold gives the answer to part
one. Part two requires two extra functions apply_folds uses apply_fold with each fold in
turn, and display_dots takes the resulting set and renders it as a grid so that the code can
be read by a human.
Enums
Controls the axis each fold will be applied using
Functions
Return a new set where the first has been folded along the given axis
Fold the list of folds into the starting set of dots #tooManyFolds
This calculates the maximum x and y in the set to determine the grid bounds, then loops through
each row and column outputting a ▮ or based on if the current coordinate is in the set.
The puzzle input is in two sections separated by a blank line. Section one is the initial set of
dot co-ordinates, in the format x,y. Section two is a list of folds in the format
fold along <axis>=<co-ordinate>.
The entry point for running the solutions with the ‘real’ puzzle input.
