Day 11: Cosmic Expansion
Part 1
You continue following signs for “Hot Springs” and eventually come across an observatory. The Elf within turns out to be a researcher studying cosmic expansion using the giant telescope here.
He doesn’t know anything about the missing machine parts; he’s only visiting for this research project. However, he confirms that the hot springs are the next-closest area likely to have people; he’ll even take you straight there once he’s done with today’s observation analysis.
Maybe you can help him with the analysis to speed things up?
The researcher has collected a bunch of data and compiled the data into a single giant image (your puzzle input). The image includes empty space (.) and galaxies (#). For example:
...#...... .......#.. #......... .......... ......#... .#........ .........# .......... .......#.. #...#..... The researcher is trying to figure out the sum of the lengths of the shortest path between every pair of galaxies. However, there's a catch: the universe expanded in the time it took the light from those galaxies to reach the observatory. Due to something involving gravitational effects, only some space expands. In fact, the result is that any rows or columns that contain no galaxies should all actually be twice as big. In the above example, three columns and two rows contain no galaxies: v v v ...#...... .......#.. #......... >..........< ......#... .#........ .........# >..........< .......#.. #...#..... ^ ^ ^ These rows and columns need to be twice as big; the result of cosmic expansion therefore looks like this: ....#........ .........#... #............ ............. ............. ........#.... .#........... ............# ............. ............. .........#... #....#....... Equipped with this expanded universe, the shortest path between every pair of galaxies can be found. It can help to assign every galaxy a unique number: ....1........ .........2... 3............ ............. ............. ........4.... .5........... ............6 ............. ............. .........7... 8....9....... In these 9 galaxies, there are 36 pairs. Only count each pair once; order within the pair doesn't matter. For each pair, find any shortest path between the two galaxies using only steps that move up, down, left, or right exactly one . or # at a time. (The shortest path between two galaxies is allowed to pass through another galaxy.) For example, here is one of the shortest paths between galaxies 5 and 9: ....1........ .........2... 3............ ............. ............. ........4.... .5........... .##.........6 ..##......... ...##........ ....##...7... 8....9....... This path has length 9 because it takes a minimum of nine steps to get from galaxy 5 to galaxy 9 (the eight locations marked # plus the step onto galaxy 9 itself). Here are some other example shortest path lengths: Between galaxy 1 and galaxy 7: 15 Between galaxy 3 and galaxy 6: 17 Between galaxy 8 and galaxy 9: 5 In this example, after expanding the universe, the sum of the shortest path between all 36 pairs of galaxies is 374. Expand the universe, then find the length of the shortest path between every pair of galaxies. What is the sum of these lengths?
Input
input = "...#......
.......#..
#.........
..........
......#...
.#........
.........#
..........
.......#..
#...#....."
input = File.read!(__DIR__ <> "/day_11.in")
Parse
lines =
input
|> String.split()
|> Enum.map(&String.graphemes/1)
Solution
defmodule Part1 do
def parse(lines) do
{row_count, col_count} = {length(lines), length(hd(lines))}
{rows, cols, galaxies} = do_parse(lines)
rows = Range.to_list(0..(row_count - 1)) -- MapSet.to_list(rows)
cols = Range.to_list(0..(col_count - 1)) -- MapSet.to_list(cols)
convert(rows, cols, galaxies)
end
def do_parse(lines) do
do_parse(lines, 0, MapSet.new(), MapSet.new(), [])
end
def do_parse([], _row_index, rows, cols, galaxies), do: {rows, cols, galaxies}
def do_parse([line | lines], row_index, rows, cols, galaxies) do
{rows, cols, galaxies} = do_parse(line, row_index, 0, rows, cols, galaxies)
do_parse(lines, row_index + 1, rows, cols, galaxies)
end
def do_parse([], _row_index, _col_index, rows, cols, galaxies), do: {rows, cols, galaxies}
def do_parse(["." | characters], row_index, col_index, rows, cols, galaxies) do
do_parse(characters, row_index, col_index + 1, rows, cols, galaxies)
end
def do_parse(["#" | characters], row_index, col_index, rows, cols, galaxies) do
rows = MapSet.put(rows, row_index)
cols = MapSet.put(cols, col_index)
galaxies = [{col_index, row_index} | galaxies]
do_parse(characters, row_index, col_index + 1, rows, cols, galaxies)
end
def convert(rows, cols, galaxies) do
for {x, y} <- galaxies do
y = y + Enum.count(rows, fn r -> r < y end)
x = x + Enum.count(cols, fn c -> c < x end)
{x, y}
end
end
end
galaxy_map = Part1.parse(lines)
for {x1, y1} <- galaxy_map,
{x2, y2} <- galaxy_map do
abs(x1 - x2) + abs(y1 - y2)
end
|> Enum.sum()
|> Kernel./(2)
Part 2
The galaxies are much older (and thus much farther apart) than the researcher initially estimated.
Now, instead of the expansion you did before, make each empty row or column one million times larger. That is, each empty row should be replaced with 1000000 empty rows, and each empty column should be replaced with 1000000 empty columns.
(In the example above, if each empty row or column were merely 10 times larger, the sum of the shortest paths between every pair of galaxies would be 1030. If each empty row or column were merely 100 times larger, the sum of the shortest paths between every pair of galaxies would be 8410. However, your universe will need to expand far beyond these values.)
Starting with the same initial image, expand the universe according to these new rules, then find the length of the shortest path between every pair of galaxies. What is the sum of these lengths?
defmodule Part2 do
def parse(lines) do
{row_count, col_count} = {length(lines), length(hd(lines))}
{rows, cols, galaxies} = Part1.do_parse(lines)
rows = Range.to_list(0..(row_count - 1)) -- MapSet.to_list(rows)
cols = Range.to_list(0..(col_count - 1)) -- MapSet.to_list(cols)
convert(rows, cols, galaxies)
end
def convert(rows, cols, galaxies) do
for {x, y} <- galaxies do
y = y + Enum.count(rows, fn r -> r < y end) * 999_999
x = x + Enum.count(cols, fn c -> c < x end) * 999_999
{x, y}
end
end
end
galaxy_map = Part2.parse(lines)
for {x1, y1} <- galaxy_map,
{x2, y2} <- galaxy_map do
abs(x1 - x2) + abs(y1 - y2)
end
|> Enum.sum()
|> Kernel./(2)