day 11 part 1 done

This commit is contained in:
Jake Pullen
2023-12-11 11:36:10 +00:00
parent 59bb5f9682
commit bfd580c2e5
3 changed files with 332 additions and 5 deletions
+140 -1
View File
@@ -1 +1,140 @@
Paste any inputs into this file.
.............................................................................................#..............................................
...#....................#............................#......................#........................#...........#................#.........
................#.....................................................#..............#.....................#................................
................................................................#.......................................................#...................
.......#........................#.........................#..............................#.....#............................................
............#............................#..................................................................................................
......................#........................#......................................................#.....................................
....................................#.....................................#.................#..........................................#....
................................................................................................................#..............#............
...................#.................................................#............................#.........................................
............................................#.........#...............................#.................#...................................
.....#.....................#..................................................#.............................................................
........................................................................#.................#.................................................
...............................................................#.....................................................#............#.........
..............#.......................#...........................................#.........................................................
.......................#.......................#...........................................................#................................
.........#........................................................#.........................................................................
.........................................................................#................................................................#.
............................#..........................#........................#.....#...............................#.....................
.....................................#..................................................................#....................#..............
#...........................................................................................................................................
..........#......................#...........#............#............#....................................................................
....................#.........................................................#.................................#...........................
.................................................#.............#.....................#..........#......................#..........#.........
....................................#.......................................................................................................
..........................................#..............................................................................................#..
......................#.....................................................................................#.....#.........#...............
...........#................#.............................#.............#.....................#.............................................
#.................#...............................................#...........#.............................................................
.......................................#.................................................#..................................................
.....#.......................................#.......................................................#..............................#......#
..............#.................#.................#..................#..............#..............................#........................
...........................................................................#................................................................
.........................#............................#....................................................#................#...............
.#..............................................................................#...........................................................
.......................................#................................#......................#............................................
................#...........#...................#.......................................................................................#...
..............................................................#..........................................................#..................
.......#......................................................................#.........#.............#.....................................
.......................................................#....................................................................................
......................#................................................#....................................#.............................#.
#.........................................#......................................................................#..........................
..................................#................#............................................#.......#...................................
.....#.....................#.............................#.............................#....................................................
................#...............................................#...........................................................................
...............................#................................................#....................#..........................#...........
......................................................#....................#................#................#..............................
..#..........................................#......................................................................#.......................
.............#..............................................................................................................................
.......................................................................................................................................#....
.......................#..............#...........................#............................#...............#............................
.........#..................#...............................................#...........#...................................................
......................................................................#........................................................#............
..............................................................#....................#...............#......#........#........................
..............#...................#.............#.....................................................................................#.....
............................................................................................................................................
.....................#.....#............#...........#......................................#................................................
..........#...................................................................................................................#...........#.
...#......................................................................#.....................#................#..........................
.............................................#...........................................................................#..................
......................................................#...........#................#................#.................................#.....
.................................#.....................................#..................#...............#......................#..........
............................................................................................................................................
.......#..............................................................................................................#.....................
.....................#..............#......#.................................................#..............................................
..............................#..................................#..............#............................#..............................
..........#...............................................#............................#..........................................#.........
#.................................................#...................#...........................#.........................................
.......................................#....................................................................................................
.......#...........................................................................#........................................................
................#............................................#............................................#.........#....................#..
.......................#........#.........#...............................................................................#.................
............................................................................................................................................
..#................#........................................................................................................................
..........#...........................................#......................................#..............................................
............................................................................................................................................
.....#.....................................#...................#....................................................................#.......
.........................#...........#............#.................................................................#....................#..
........................................................#...............#...................................................................
............#.................#.................................................#......#..............#.....................................
.............................................................................................#................#................#............
........................................#.......................#..........................................................................#
..#.................#..........................#......................#.............#..............#...................#.............#......
............................#............................#................................#......................#..........................
.............#.................................................................................#............................................
.................................#.........................................#................................................................
.........................#.........................#.................................................#........................#.............
.......#......................................................#.............................................................................
............................................................................................................................................
............................................................................................................................................
.......................#.........................................................................................#..........................
...#...........................#..................................................................#......#................#.................
...............................................#................#...........................................................................
....................#.................................#.................#.......#..........#........................................#.......
...........#.................................................................................................#.............................#
.#..................................................................................................#.......................................
.....................................#.................................................................................#....................
...............#.........#...........................................................#......................................................
................................#........................#........#.........................................................................
......#..........................................#.............................................................................#............
...............................................................................................#.......#....................................
...............................................................................................................#............................
..............................#...........#............................#.................................................#............#.....
...................#...........................#............................................................................................
...............................................................#.....................#.....#................................................
........#.................#.......................................................................................#...........#.............
...............#............................#.........#.......................#...........................#.................................
.#...................................#...................................#................................................................#.
.........................................................................................#...........................................#......
........................#...................................................................................................................
....................................................................................................................#.......................
........................................#.....#.........#.........#.........................................................................
.......................................................................................#............#.......................................
.#..............................#.............................................................#........................#.....#.....#........
............#...............................................................#.................................#.........................#...
..............................................................#.........................................#...................................
........................#..............................#...............#........#.........#.................................................
...................#........................#.......................................................................#.......................
.......................................#...................................................................#................................
..............#..................#..............#...................................................................................#.......
........#......................................................#..............#.................#.........................................#.
....................................................................#.......................................................#...............
.............................#..........................................................................#...................................
.................#..................................................................#.......................................................
............................................#..........................#............................................#..............#........
......#..............#..........................................#...........#..............................#................................
............#.....................#.................................................................#.......................................
...........................#.............#...............#..................................................................#...............
#....................................................................................................................................#......
.............................................................#.....................#........................................................
.......#..........................................#.................#....................................#........#.......................#.
..........................................................................................#.................................................
..........................#..............................................#......................#...........................................
...............................#.....................#..........................#.............................#.............................
....#......#................................................#..........................#.......................................#............
..................#.............................#...........................................................................................
...........................................#..................................................#.............................................
.......................#...........................................#...................................................#...................#
.......#......#.............#.........................#..................#..............................#...........................#.......
..#..............................................................................................................#..........................
+103 -3
View File
@@ -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)
+89 -1
View File
@@ -1 +1,89 @@
# Day 11 Puzzle Text.
<article class="day-desc"><h2>--- Day 11: Cosmic Expansion ---</h2><p>You continue following signs for "Hot Springs" and eventually come across an <a href="https://en.wikipedia.org/wiki/Observatory" target="_blank">observatory</a>. The Elf within turns out to be a researcher studying cosmic expansion using the giant telescope here.</p>
<p>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.</p>
<p>Maybe you can help him with the analysis to speed things up?</p>
<p>The researcher has collected a bunch of data and compiled the data into a single giant <em>image</em> (your puzzle input). The image includes <em>empty space</em> (<code>.</code>) and <em>galaxies</em> (<code>#</code>). For example:</p>
<pre><code>...#......
.......#..
#.........
..........
......#...
.#........
.........#
..........
.......#..
#...#.....
</code></pre>
<p>The researcher is trying to figure out the sum of the lengths of the <em>shortest path between every pair of galaxies</em>. However, there's a catch: the universe expanded in the time it took the light from those galaxies to reach the observatory.</p>
<p>Due to something involving gravitational effects, <em>only some space expands</em>. In fact, the result is that <em>any rows or columns that contain no galaxies</em> should all actually be twice as big.</p>
<p>In the above example, three columns and two rows contain no galaxies:</p>
<pre><code> v v v
...#......
.......#..
#.........
&gt;..........&lt;
......#...
.#........
.........#
&gt;..........&lt;
.......#..
#...#.....
^ ^ ^
</code></pre>
<p>These rows and columns need to be <em>twice as big</em>; the result of cosmic expansion therefore looks like this:</p>
<pre><code>....#........
.........#...
#............
.............
.............
........#....
.#...........
............#
.............
.............
.........#...
#....#.......
</code></pre>
<p>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:</p>
<pre><code>....1........
.........2...
3............
.............
.............
........4....
.5...........
............6
.............
.............
.........7...
8....9.......
</code></pre>
<p>In these 9 galaxies, there are <em>36 pairs</em>. 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 <code>.</code> or <code>#</code> at a time. (The shortest path between two galaxies is allowed to pass through another galaxy.)</p>
<p>For example, here is one of the shortest paths between galaxies <code>5</code> and <code>9</code>:</p>
<pre><code>....1........
.........2...
3............
.............
.............
........4....
.5...........
.##.........6
..##.........
...##........
....##...7...
8....9.......
</code></pre>
<p>This path has length <code><em>9</em></code> because it takes a minimum of <em>nine steps</em> to get from galaxy <code>5</code> to galaxy <code>9</code> (the eight locations marked <code>#</code> plus the step onto galaxy <code>9</code> itself). Here are some other example shortest path lengths:</p>
<ul>
<li>Between galaxy <code>1</code> and galaxy <code>7</code>: 15</li>
<li>Between galaxy <code>3</code> and galaxy <code>6</code>: 17</li>
<li>Between galaxy <code>8</code> and galaxy <code>9</code>: 5</li>
</ul>
<p>In this example, after expanding the universe, the sum of the shortest path between all 36 pairs of galaxies is <code><em>374</em></code>.</p>
<p>Expand the universe, then find the length of the shortest path between every pair of galaxies. <em>What is the sum of these lengths?</em></p>
<p>Your puzzle answer was <code>9724940</code>.</p><p class="day-success">The first half of this puzzle is complete! It provides one gold star: *</p>
<article class="day-desc"><h2 id="part2">--- Part Two ---</h2><p>The galaxies are much <em>older</em> (and thus much <em>farther apart</em>) than the researcher initially estimated.</p>
<p>Now, instead of the expansion you did before, make each empty row or column <em><span title="And you have to have your pinky near your mouth when you do it.">one million</span> times</em> larger. That is, each empty row should be replaced with <code>1000000</code> empty rows, and each empty column should be replaced with <code>1000000</code> empty columns.</p>
<p>(In the example above, if each empty row or column were merely <code>10</code> times larger, the sum of the shortest paths between every pair of galaxies would be <code><em>1030</em></code>. If each empty row or column were merely <code>100</code> times larger, the sum of the shortest paths between every pair of galaxies would be <code><em>8410</em></code>. However, your universe will need to expand far beyond these values.)</p>
<p>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. <em>What is the sum of these lengths?</em></p>