Module advent_of_code_2023::day_3
source · Expand description
This is my solution for Advent of Code - Day 3: Gear Ratios
parse_grid
turns the plain text 2D grid of characters, in to a list of PartNumber
s and a SymbolLookup
.
sum_valid_part_numbers
solves part one, delegating to has_adjacent_symbol
and get_adjacent_points
to
determine if each number is valid.
sum_gear_ratios
solves part two. The key logic is in find_gears
which uses
explode_adjacent_points
(which usesget_adjacent_points
again) to list all adjacent pointsis_point_a_gear_symbol
to filter that list to*
symbols that might be gears- Then turns those that are valid into the expected list of
Gear
s
Structs
- Gear 🔒A representation of a gear by the two part numbers that make up its “gear ratio”
- Represents a part number as the position of the first digit, and the number it represents
Functions
- Turn a PartNumber into a list of pairs of the (bare number, point) for each point it is adjacent to
- Return a list of valid gears. A gear is any
*
symbol with exactly two adjacent PartNumbers. - Return the list of points adjacent to the whole part number that have non-negative co-ordinates
- Part numbers are valid if adjacent to a symbol
- Returns true if a given 2D co-ordinate maps to a
*
symbol - Parse a string representing a 2D grid into a list of part numbers and a lookup table of points with character symbols
- The entry point for running the solutions with the ‘real’ puzzle input.
- Solution to part 2 - finds all the valid gears and sums the multiplications of their “gear ratio” numbers.
- Solves part 1 - the sum of part numbers next to a symbol
Type Aliases
- Point 🔒A point in 2D space
- A map of 2D points to the part symbol at that point