You meet a researcher studying space.
You want to help with today’s analysis.
It’s a 2D map of space.
An example input looks like this:
a # represents a galaxy
a . represents empty space
Parsing
I represented every point in the grid as a Tile and stored them in a 2D list.
Part 1
The researcher is trying to figure out the sum of the lengths of the shortest path between every pair of galaxies.
But because the universe is expanding, the readings are out of date.
Because of ✨✨reasons✨✨ only some space expands.
Any rows or columns that contain no galaxies should all actually be twice as big.
The question asks for the sum of all distances between each pair galaxies.
some skeleton/pseudo-code:
Helpers
I represent each point as a Coord
Because it’s a 2D list, the distance between 2 points can be calculated with the Manhattan distance.
Next is the helper that turns the 2D grid into a list of Coord of galaxies.
Because of that expansion rule, I can’t loop over the original grid and use those coordinates, so I keep track of them manually.
Each time I come across an empty row or column, I increment the corresponding coordinate appropriately.
If I come across a galaxy during my loop, I add the current coordinates to a list.
This function uses 2 helper functions to keep track of indexes for empty rows or empty columns.
Time to put it all together.
Code
Part 2
Turns out the galaxies are much further apart.
Any rows or columns that contain no galaxies should all actually be one million times as big.
The question asks for the sum of all distances between each pair galaxies.
Code
The galaxy_coordinates jumped 2 steps when it found an empty line in part1, it jumps 1_000_000 steps in part2.
I normally don’t do this Advent of Code, but I generalized a helper function so it can be used for part 1 and part 2.