changeset 18:ddb69833346c

Implement day 11 distance finding in space "shortest distance" is simplified by it being cardinal directions, so it's the same as taking the right-angle between them. The Part 1 space expansion was quite clean, but the Part 2 approach generalises it to something even nicer.
author IBBoard <dev@ibboard.co.uk>
date Mon, 11 Dec 2023 20:08:47 +0000
parents 92144824cbb7
children 1e16a25a9553
files day11.rb day11.txt day11b.rb
diffstat 3 files changed, 164 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/day11.rb	Mon Dec 11 20:08:47 2023 +0000
@@ -0,0 +1,26 @@
+#! /usr/bin/env ruby
+
+if ARGV.length != 1
+        abort("Incorrect arguments - needs input file")
+elsif not File.exist? (ARGV[0])
+	abort("File #{ARGV[0]} did not exist")
+end
+
+file = ARGV[0]
+
+OPEN_SPACE = "."
+GALAXY = "#"
+
+input_space = File.open(file, "r").each_line(chomp: true).map(&:each_char).map(&:to_a)
+
+first, *rest = input_space
+open_columns = first.zip(*rest).map {|col| col.all? {|cell| cell == OPEN_SPACE}}
+
+expanded_space = input_space.flat_map do |row|
+	new_row = row.zip(open_columns).flat_map {|cell, is_open| is_open ? [cell, cell] : [cell]}
+	if row.all? {|cell| cell == OPEN_SPACE} then [new_row, new_row] else [new_row] end
+end
+
+galaxies = expanded_space.each_with_index.flat_map {|row, y| row.each_with_index.map {|cell, x| [x, y] if cell == GALAXY}}.compact
+galactic_distances = galaxies.combination(2).map {|a, b| (a[0] - b[0]).abs + (a[1] - b[1]).abs}
+puts galactic_distances.sum
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/day11.txt	Mon Dec 11 20:08:47 2023 +0000
@@ -0,0 +1,106 @@
+--- Day 11: Cosmic Expansion ---
+
+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?
+
+--- Part Two ---
+
+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?
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/day11b.rb	Mon Dec 11 20:08:47 2023 +0000
@@ -0,0 +1,32 @@
+#! /usr/bin/env ruby
+
+if ARGV.length != 1
+        abort("Incorrect arguments - needs input file")
+elsif not File.exist? (ARGV[0])
+	abort("File #{ARGV[0]} did not exist")
+end
+
+file = ARGV[0]
+
+OPEN_SPACE = "."
+GALAXY = "#"
+EXPANSION_FACTOR = 1000000
+
+input_space = File.open(file, "r").each_line(chomp: true).map(&:each_char).map(&:to_a)
+
+first, *rest = input_space
+open_columns = first.zip(*rest).each_with_index.map {|col, idx| col.all? {|cell| cell == OPEN_SPACE} ? idx : nil}.compact
+open_rows = input_space.each_with_index.map {|row, idx| row.all? {|cell| cell == OPEN_SPACE} ? idx : nil}.compact
+
+
+galaxies = input_space.each_with_index.flat_map {|row, y| row.each_with_index.map {|cell, x| [x, y] if cell == GALAXY}}.compact
+galactic_distances = galaxies.combination(2).map do |a, b|
+	x_1, x_2 = [a[0], b[0]].minmax
+	y_1, y_2 = [a[1], b[1]].minmax
+	expanded_cols = ((x_1..x_2).to_a & open_columns).length
+	expanded_rows = ((y_1..y_2).to_a & open_rows).length
+
+	# Avoid double-counting the expanded cell by subtracting the number of columns first rather any complex logic
+	x_2 - x_1 + - expanded_cols + (expanded_cols * EXPANSION_FACTOR) + y_2 - y_1 + - expanded_rows + (expanded_rows * EXPANSION_FACTOR)
+end
+puts galactic_distances.sum