From bfd580c2e535ab8b3dfce5989cda79a441f99d35 Mon Sep 17 00:00:00 2001 From: Jake Pullen Date: Mon, 11 Dec 2023 11:36:10 +0000 Subject: [PATCH] day 11 part 1 done --- 2023/day_11/input.txt | 141 ++++++++++++++++++++++++++++++++- 2023/day_11/part 1 solution.py | 106 ++++++++++++++++++++++++- 2023/day_11/puzzle_text.md | 90 ++++++++++++++++++++- 3 files changed, 332 insertions(+), 5 deletions(-) diff --git a/2023/day_11/input.txt b/2023/day_11/input.txt index b221142..232a501 100644 --- a/2023/day_11/input.txt +++ b/2023/day_11/input.txt @@ -1 +1,140 @@ -Paste any inputs into this file. \ No newline at end of file +.............................................................................................#.............................................. +...#....................#............................#......................#........................#...........#................#......... +................#.....................................................#..............#.....................#................................ +................................................................#.......................................................#................... +.......#........................#.........................#..............................#.....#............................................ +............#............................#.................................................................................................. +......................#........................#......................................................#..................................... +....................................#.....................................#.................#..........................................#.... +................................................................................................................#..............#............ +...................#.................................................#............................#......................................... +............................................#.........#...............................#.................#................................... +.....#.....................#..................................................#............................................................. +........................................................................#.................#................................................. +...............................................................#.....................................................#............#......... +..............#.......................#...........................................#......................................................... +.......................#.......................#...........................................................#................................ +.........#........................................................#......................................................................... +.........................................................................#................................................................#. +............................#..........................#........................#.....#...............................#..................... +.....................................#..................................................................#....................#.............. +#........................................................................................................................................... +..........#......................#...........#............#............#.................................................................... +....................#.........................................................#.................................#........................... +.................................................#.............#.....................#..........#......................#..........#......... +....................................#....................................................................................................... +..........................................#..............................................................................................#.. +......................#.....................................................................................#.....#.........#............... +...........#................#.............................#.............#.....................#............................................. +#.................#...............................................#...........#............................................................. +.......................................#.................................................#.................................................. +.....#.......................................#.......................................................#..............................#......# +..............#.................#.................#..................#..............#..............................#........................ +...........................................................................#................................................................ +.........................#............................#....................................................#................#............... +.#..............................................................................#........................................................... +.......................................#................................#......................#............................................ +................#...........#...................#.......................................................................................#... +..............................................................#..........................................................#.................. +.......#......................................................................#.........#.............#..................................... +.......................................................#.................................................................................... +......................#................................................#....................................#.............................#. +#.........................................#......................................................................#.......................... +..................................#................#............................................#.......#................................... +.....#.....................#.............................#.............................#.................................................... +................#...............................................#........................................................................... +...............................#................................................#....................#..........................#........... +......................................................#....................#................#................#.............................. +..#..........................................#......................................................................#....................... +.............#.............................................................................................................................. +.......................................................................................................................................#.... +.......................#..............#...........................#............................#...............#............................ +.........#..................#...............................................#...........#................................................... +......................................................................#........................................................#............ +..............................................................#....................#...............#......#........#........................ +..............#...................#.............#.....................................................................................#..... +............................................................................................................................................ +.....................#.....#............#...........#......................................#................................................ +..........#...................................................................................................................#...........#. +...#......................................................................#.....................#................#.......................... +.............................................#...........................................................................#.................. +......................................................#...........#................#................#.................................#..... +.................................#.....................................#..................#...............#......................#.......... +............................................................................................................................................ +.......#..............................................................................................................#..................... +.....................#..............#......#.................................................#.............................................. +..............................#..................................#..............#............................#.............................. +..........#...............................................#............................#..........................................#......... +#.................................................#...................#...........................#......................................... +.......................................#.................................................................................................... +.......#...........................................................................#........................................................ +................#............................................#............................................#.........#....................#.. +.......................#........#.........#...............................................................................#................. +............................................................................................................................................ +..#................#........................................................................................................................ +..........#...........................................#......................................#.............................................. +............................................................................................................................................ +.....#.....................................#...................#....................................................................#....... +.........................#...........#............#.................................................................#....................#.. +........................................................#...............#................................................................... +............#.................#.................................................#......#..............#..................................... +.............................................................................................#................#................#............ +........................................#.......................#..........................................................................# +..#.................#..........................#......................#.............#..............#...................#.............#...... +............................#............................#................................#......................#.......................... +.............#.................................................................................#............................................ +.................................#.........................................#................................................................ +.........................#.........................#.................................................#........................#............. +.......#......................................................#............................................................................. +............................................................................................................................................ +............................................................................................................................................ +.......................#.........................................................................................#.......................... +...#...........................#..................................................................#......#................#................. +...............................................#................#........................................................................... +....................#.................................#.................#.......#..........#........................................#....... +...........#.................................................................................................#.............................# +.#..................................................................................................#....................................... +.....................................#.................................................................................#.................... +...............#.........#...........................................................#...................................................... +................................#........................#........#......................................................................... +......#..........................................#.............................................................................#............ +...............................................................................................#.......#.................................... +...............................................................................................................#............................ +..............................#...........#............................#.................................................#............#..... +...................#...........................#............................................................................................ +...............................................................#.....................#.....#................................................ +........#.................#.......................................................................................#...........#............. +...............#............................#.........#.......................#...........................#................................. +.#...................................#...................................#................................................................#. +.........................................................................................#...........................................#...... +........................#................................................................................................................... +....................................................................................................................#....................... +........................................#.....#.........#.........#......................................................................... +.......................................................................................#............#....................................... +.#..............................#.............................................................#........................#.....#.....#........ +............#...............................................................#.................................#.........................#... +..............................................................#.........................................#................................... +........................#..............................#...............#........#.........#................................................. +...................#........................#.......................................................................#....................... +.......................................#...................................................................#................................ +..............#..................#..............#...................................................................................#....... +........#......................................................#..............#.................#.........................................#. +....................................................................#.......................................................#............... +.............................#..........................................................................#................................... +.................#..................................................................#....................................................... +............................................#..........................#............................................#..............#........ +......#..............#..........................................#...........#..............................#................................ +............#.....................#.................................................................#....................................... +...........................#.............#...............#..................................................................#............... +#....................................................................................................................................#...... +.............................................................#.....................#........................................................ +.......#..........................................#.................#....................................#........#.......................#. +..........................................................................................#................................................. +..........................#..............................................#......................#........................................... +...............................#.....................#..........................#.............................#............................. +....#......#................................................#..........................#.......................................#............ +..................#.............................#........................................................................................... +...........................................#..................................................#............................................. +.......................#...........................................#...................................................#...................# +.......#......#.............#.........................#..................#..............................#...........................#....... +..#..............................................................................................................#.......................... \ No newline at end of file diff --git a/2023/day_11/part 1 solution.py b/2023/day_11/part 1 solution.py index 05cdeb0..eefef34 100644 --- a/2023/day_11/part 1 solution.py +++ b/2023/day_11/part 1 solution.py @@ -1,6 +1,106 @@ -import os +from queue import Queue +# import os with open(r'advent_of_code\2023\day_11\input.txt', 'r') as file: - input = file.read() + input = file.readlines() -print(input) +# print(input) + +test_input = '''...#...... +.......#.. +#......... +.......... +......#... +.#........ +.........# +.......... +.......#.. +#...#.....''' + +#print(test_input) +#input = test_input.split('\n') + +def expand_universe(input): + '''add an extra row or column of empty spaces to each row or column that has no # in it''' + print('Expanding universe') + # expand empty rows + new_input = [] + for row in input: + if '#' in row: + new_input.append(row) + else: + new_input.append(row) + new_input.append(row) + + # expand empty columns + + # Transpose the input + transposed_input = list(map(list, zip(*new_input))) + + new_input = [] + for row in transposed_input: + if '#' in row: + new_input.append(row) + else: + new_input.append(row) + new_input.append(row) + + # Transpose the input back + new_input = list(map(list, zip(*new_input))) + + # Convert rows back to strings + new_input = [''.join(row) for row in new_input] + + return new_input + +input = expand_universe(input) + +def number_galaxies(input): + '''gives a unique number to each galaxy(#) in the input grid''' + print('Numbering galaxies') + # make a copy of the input + input_copy = [list(row) for row in input] # Convert strings to lists + # replace the galaxies with their numbers + galaxy_number = 1 + galaxy_coordinates = {} # This dictionary will store the coordinates of each galaxy + for i in range(len(input_copy)): + for j in range(len(input_copy[i])): + if input_copy[i][j] == '#': + input_copy[i][j] = str(galaxy_number) # Convert number back to string + galaxy_coordinates[galaxy_number] = (i, j) # Store the coordinates of the galaxy + galaxy_number += 1 # Increment the galaxy number + # Convert lists back to strings + input_copy = [''.join(row) for row in input_copy] + return input_copy, galaxy_coordinates + +input, galaxy_coordinates = number_galaxies(input) +#print(galaxy_coordinates) + +def manhattan_distance(point1, point2): + return abs(point1[0] - point2[0]) + abs(point1[1] - point2[1]) + +# find the shortest path between each pair of galaxies +print('Finding the shortest path between each pair of galaxies') +galaxy_distances = {} +for galaxy in galaxy_coordinates: + galaxy_distances[galaxy] = {} + for other_galaxy in galaxy_coordinates: + if galaxy != other_galaxy: + galaxy_distances[galaxy][other_galaxy] = manhattan_distance(galaxy_coordinates[galaxy], galaxy_coordinates[other_galaxy]) +#print(galaxy_distances) + +# remove the duplicate distances ie 1->2 and 2->1 +print('Removing the duplicate distances') +for galaxy in galaxy_distances: + for other_galaxy in galaxy_distances[galaxy]: + if other_galaxy in galaxy_distances: + del galaxy_distances[other_galaxy][galaxy] +#print(galaxy_distances) + +# sum up all the distances +print('Summing up all the distances') +total_distance = 0 +for galaxy in galaxy_distances: + for other_galaxy in galaxy_distances[galaxy]: + total_distance += galaxy_distances[galaxy][other_galaxy] +print(total_distance) \ No newline at end of file diff --git a/2023/day_11/puzzle_text.md b/2023/day_11/puzzle_text.md index bba8c30..b1d8f9e 100644 --- a/2023/day_11/puzzle_text.md +++ b/2023/day_11/puzzle_text.md @@ -1 +1,89 @@ -# Day 11 Puzzle Text. \ No newline at end of file + +

--- 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:

+ +

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?

+ +

Your puzzle answer was 9724940.

The first half of this puzzle is complete! It provides one gold star: *

+

--- 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?