Skip to content

Commit 8ad34aa

Browse files
authored
Add 2023/11 py
1 parent 3a837da commit 8ad34aa

File tree

8 files changed

+383
-0
lines changed

8 files changed

+383
-0
lines changed

2023/11/README.txt

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
--- Day 11: Cosmic Expansion ---
2+
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.
3+
4+
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.
5+
6+
Maybe you can help him with the analysis to speed things up?
7+
8+
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:
9+
10+
...#......
11+
.......#..
12+
#.........
13+
..........
14+
......#...
15+
.#........
16+
.........#
17+
..........
18+
.......#..
19+
#...#.....
20+
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.
21+
22+
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.
23+
24+
In the above example, three columns and two rows contain no galaxies:
25+
26+
v v v
27+
...#......
28+
.......#..
29+
#.........
30+
>..........<
31+
......#...
32+
.#........
33+
.........#
34+
>..........<
35+
.......#..
36+
#...#.....
37+
^ ^ ^
38+
These rows and columns need to be twice as big; the result of cosmic expansion therefore looks like this:
39+
40+
....#........
41+
.........#...
42+
#............
43+
.............
44+
.............
45+
........#....
46+
.#...........
47+
............#
48+
.............
49+
.............
50+
.........#...
51+
#....#.......
52+
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:
53+
54+
....1........
55+
.........2...
56+
3............
57+
.............
58+
.............
59+
........4....
60+
.5...........
61+
............6
62+
.............
63+
.............
64+
.........7...
65+
8....9.......
66+
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.)
67+
68+
For example, here is one of the shortest paths between galaxies 5 and 9:
69+
70+
....1........
71+
.........2...
72+
3............
73+
.............
74+
.............
75+
........4....
76+
.5...........
77+
.##.........6
78+
..##.........
79+
...##........
80+
....##...7...
81+
8....9.......
82+
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:
83+
84+
Between galaxy 1 and galaxy 7: 15
85+
Between galaxy 3 and galaxy 6: 17
86+
Between galaxy 8 and galaxy 9: 5
87+
In this example, after expanding the universe, the sum of the shortest path between all 36 pairs of galaxies is 374.
88+
89+
Expand the universe, then find the length of the shortest path between every pair of galaxies. What is the sum of these lengths?
90+
91+
Your puzzle answer was 9556896.
92+
93+
--- Part Two ---
94+
The galaxies are much older (and thus much farther apart) than the researcher initially estimated.
95+
96+
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.
97+
98+
(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.)
99+
100+
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?
101+
102+
Your puzzle answer was 685038186836.

2023/11/input

Lines changed: 140 additions & 0 deletions
Large diffs are not rendered by default.

2023/11/input1.0

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
...#......
2+
.......#..
3+
#.........
4+
..........
5+
......#...
6+
.#........
7+
.........#
8+
..........
9+
.......#..
10+
#...#.....

2023/11/output1.0

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
374

2023/11/output2.0

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
1030

2023/11/output2.1

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
8410

2023/11/part1.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#!/usr/bin/env python3
2+
import fileinput
3+
from collections import defaultdict
4+
from itertools import combinations
5+
6+
PIXEL_SPACE = "."
7+
PIXEL_GALAXY = "#"
8+
9+
10+
def read_image():
11+
galaxies = []
12+
rows = list(fileinput.input())
13+
for row, line in enumerate(rows):
14+
for col, pixel in enumerate(line[:-1]):
15+
if pixel == PIXEL_GALAXY:
16+
galaxies.append((row, col))
17+
return (len(rows), len(rows[0]), galaxies)
18+
19+
20+
def expand_rows(univ_rows, galaxies):
21+
galaxies_by_row = defaultdict(list)
22+
for row, col in galaxies:
23+
galaxies_by_row[row].append(col)
24+
25+
galaxies_expanded = []
26+
row = 0
27+
rows_expanded = 0
28+
while row < univ_rows:
29+
if row in galaxies_by_row:
30+
for col in galaxies_by_row[row]:
31+
galaxies_expanded.append((row + rows_expanded, col))
32+
else:
33+
rows_expanded += 1
34+
row += 1
35+
36+
return galaxies_expanded
37+
38+
39+
def expand_universe(univ_rows, univ_cols, galaxies):
40+
exp_row = expand_rows(univ_rows, galaxies)
41+
exp_row_inv = sorted([(col, row) for row, col in exp_row])
42+
exp_row_col_inv = expand_rows(univ_cols, exp_row_inv)
43+
return sorted([(row, col) for col, row in exp_row_col_inv])
44+
45+
46+
def shortest_distance(galaxy1, galaxy2):
47+
# Manhattan distance
48+
return abs(galaxy2[0] - galaxy1[0]) + abs(galaxy2[1] - galaxy1[1])
49+
50+
51+
def main():
52+
univ_rows, univ_cols, galaxies = read_image()
53+
galaxies_expanded = expand_universe(univ_rows, univ_cols, galaxies)
54+
55+
len_sum = 0
56+
for g1, g2 in combinations(galaxies_expanded, 2):
57+
len_sum += shortest_distance(g1, g2)
58+
print(len_sum)
59+
60+
61+
if __name__ == "__main__":
62+
main()

2023/11/part2.py

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#!/usr/bin/env python3
2+
import fileinput
3+
from collections import defaultdict
4+
from itertools import combinations
5+
6+
PIXEL_SPACE = "."
7+
PIXEL_GALAXY = "#"
8+
9+
# SPACE_EXPANSION = 10 - 1
10+
# SPACE_EXPANSION = 100 - 1
11+
SPACE_EXPANSION = 1000000 - 1
12+
13+
14+
def read_image():
15+
galaxies = []
16+
rows = list(fileinput.input())
17+
for row, line in enumerate(rows):
18+
for col, pixel in enumerate(line[:-1]):
19+
if pixel == PIXEL_GALAXY:
20+
galaxies.append((row, col))
21+
return (len(rows), len(rows[0]), galaxies)
22+
23+
24+
def expand_rows(univ_rows, galaxies):
25+
galaxies_by_row = defaultdict(list)
26+
for row, col in galaxies:
27+
galaxies_by_row[row].append(col)
28+
29+
galaxies_expanded = []
30+
row = 0
31+
rows_expanded = 0
32+
while row < univ_rows:
33+
if row in galaxies_by_row:
34+
for col in galaxies_by_row[row]:
35+
galaxies_expanded.append((row + rows_expanded * SPACE_EXPANSION, col))
36+
else:
37+
rows_expanded += 1
38+
row += 1
39+
40+
return galaxies_expanded
41+
42+
43+
def expand_universe(univ_rows, univ_cols, galaxies):
44+
exp_row = expand_rows(univ_rows, galaxies)
45+
exp_row_inv = sorted([(col, row) for row, col in exp_row])
46+
exp_row_col_inv = expand_rows(univ_cols, exp_row_inv)
47+
return sorted([(row, col) for col, row in exp_row_col_inv])
48+
49+
50+
def shortest_distance(galaxy1, galaxy2):
51+
# Manhattan distance
52+
return abs(galaxy2[0] - galaxy1[0]) + abs(galaxy2[1] - galaxy1[1])
53+
54+
55+
def main():
56+
univ_rows, univ_cols, galaxies = read_image()
57+
galaxies_expanded = expand_universe(univ_rows, univ_cols, galaxies)
58+
59+
len_sum = 0
60+
for g1, g2 in combinations(galaxies_expanded, 2):
61+
len_sum += shortest_distance(g1, g2)
62+
print(len_sum)
63+
64+
65+
if __name__ == "__main__":
66+
main()

0 commit comments

Comments
 (0)