Module advent_of_code_2021::day_5[−][src]

Expand description

This is my solution for Advent of Code - Day 5 - Hydrothermal Venture

Today was filling co-ordinates on a grid. Both tasks only needed to know which points had been filled twice, so I was able to implement that with two HashSets of co-ordinates, only setting the second set if that co-ordinate was set in the first. This is implemented in get_intersections. The other key piece of logic is translating the lines of the input into the points along their path, implemented by Line::get_points.

Part one is just a limited version of part two, and my solution works the same for both. get_axial_intersections uses Line::is_axial to filter out the diagonal lines that are only used in part two. To implement part two I just had to add the test cases for the diagonal lines, everything else just worked.

Structs

Represent a line using the co-ordinates of each end.

Functions

Filter out diagonal lines before running the remaining lines through get_intersections

For each line, iterate over it’s points adding each to a set (visited at least once). If that insert fails, we’ve already seen that point so add it to a second set (visited at least twice). Points repeated more than twice can be ignored, as this is not needed to provide the puzzle solution. Return that set, the length of the set will give the number of points where two of more lines intersect.

Takes a string with lines in the form (x1,y1) -> (x2,y2) and converts it into a list of Lines. Parsed using a regular expression.

The entry point for running the solutions with the ‘real’ puzzle input.