Skip to content

Commit 144697f

Browse files
committed
Day 11 Part 2
1 parent 257a892 commit 144697f

File tree

2 files changed

+84
-38
lines changed

2 files changed

+84
-38
lines changed

2023/day_11/Python/cosmic.py

Lines changed: 52 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,95 @@
11
from collections import namedtuple
22
from dataclasses import dataclass
3+
from typing import Literal, LiteralString, List
4+
5+
6+
@dataclass
7+
class Coordinate:
8+
x: int
9+
y: int
10+
11+
12+
Grid = list[list[str | Coordinate]]
313

4-
Grid = list[list[str]]
514

615
def load_file(file_path: str) -> Grid:
716
with open(file_path) as file:
817
return [list(i.strip()) for i in file]
918

10-
def row_is_empty(grid: Grid, row: int) -> bool:
11-
return all(cell == "." for cell in grid[row])
1219

1320
def column_is_empty(grid: Grid, column: int) -> bool:
1421
return all(row[column] == "." for row in grid)
1522

16-
def expand_grid_rows(grid: Grid) -> Grid:
17-
new_grid :Grid = []
18-
for idx, row in enumerate(grid):
19-
new_grid.append(row)
20-
if row_is_empty(grid,idx):
21-
for i in range(1_000_000): # Part 2 Don't do this...
22-
new_grid.append(row)
23-
return new_grid
2423

25-
def expand(grid: Grid) -> Grid:
26-
return transpose(expand_grid_rows(transpose(expand_grid_rows(grid))))
24+
def offset_all_in_column(grid: Grid, column: int, offset: int,
25+
coordinate_to_adjust: Literal["x"] | Literal["y"]) -> None:
26+
for row in grid:
27+
if isinstance(row[column], Coordinate):
28+
if coordinate_to_adjust == "x":
29+
row[column].x += offset
30+
else:
31+
row[column].y += offset
2732

2833

29-
def transpose(grid: Grid) -> Grid:
30-
return [list(x) for x in list(zip(*grid))]
34+
def offset_all_in_row(grid: Grid, row: int, offset: int, coordinate_to_adjust: Literal["x"] | Literal["y"]) -> None:
35+
for x in grid[row]:
36+
if isinstance(x, Coordinate):
37+
if coordinate_to_adjust == "x":
38+
x.x += offset
39+
else:
40+
x.y += offset
3141

32-
namedtuple("Test",["x","y"])
3342

34-
@dataclass(frozen=True)
35-
class Coordinate:
36-
x: int
37-
y: int
43+
def adjust_coordinates(grid: Grid, coordinate_to_adjust: Literal["x"] | Literal["y"]) -> None:
44+
current_offset = 0
45+
for idx, row in enumerate(grid):
46+
if column_is_empty(grid, idx):
47+
current_offset += 1_000_000 - 1
48+
else:
49+
offset_all_in_column(grid, idx, current_offset, coordinate_to_adjust)
50+
51+
52+
def expand(grid: Grid) -> None:
53+
adjust_coordinates(grid, 'x')
54+
grid = transpose(grid)
55+
adjust_coordinates(grid, 'y')
56+
3857

3958
def find_galaxies(grid: Grid) -> list[Coordinate]:
4059
coords = []
4160
for row in range(len(grid)):
4261
for column in range(len(grid[0])):
4362
if grid[row][column] == "#":
44-
coords.append(Coordinate(column,row))
63+
new_coord = Coordinate(column, row)
64+
grid[row][column] = new_coord
65+
coords.append(new_coord)
4566
return coords
4667

68+
69+
def transpose(grid: Grid) -> Grid:
70+
return [list(x) for x in list(zip(*grid))]
71+
72+
4773
def distance(coord1: Coordinate, coord2: Coordinate) -> int:
4874
return abs(coord1.x - coord2.x) + abs(coord1.y - coord2.y)
4975

76+
5077
def all_pairs(coords: list[Coordinate]) -> int:
5178
total = 0
5279
for i in range(len(coords)):
5380
for j in range(i):
54-
total+=distance(coords[i],coords[j])
81+
total += distance(coords[i], coords[j])
5582
return total
5683

5784

5885
def main():
5986
grid = load_file("../input")
60-
expanded = expand(grid)
61-
galaxies = find_galaxies(expanded)
87+
galaxies = find_galaxies(grid)
88+
expand(grid)
6289
result = all_pairs(galaxies)
6390
print(result)
6491
print("Hello, World!")
6592

93+
6694
if __name__ == "__main__":
6795
main()
68-
69-
70-
## Part 2
71-
"""
72-
Find original coordinates first (before any expansion)
73-
Scan the galaxy horizontally, keep track of how many blank columns, add that to the coordinate
74-
Transpose, do again.
75-
"""

2023/day_11/Python/cosmic_test.py

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
from cosmic import expand, find_galaxies, all_pairs
1+
from cosmic import expand, find_galaxies, all_pairs, Coordinate
22

3-
input_str = """...#......
3+
input_str = """\
4+
...#......
45
.......#..
56
#.........
67
..........
@@ -10,7 +11,8 @@
1011
..........
1112
.......#..
1213
#...#....."""
13-
expanded_str = """....#........
14+
expanded_str = """\
15+
....#........
1416
.........#...
1517
#............
1618
.............
@@ -24,19 +26,43 @@
2426
#....#......."""
2527

2628

29+
def test_find_galaxies():
30+
input_grid = [list(i.strip()) for i in input_str.split("\n")]
31+
result = find_galaxies(input_grid)
32+
33+
expected_coords = [Coordinate(3, 0), Coordinate(1, 7), Coordinate(2, 0), Coordinate(4, 6), Coordinate(6, 8),
34+
Coordinate(8, 6), Coordinate(9, 0), Coordinate(9, 4)]
35+
assert result == expected_coords
36+
37+
2738
def test_expand():
2839
input_grid = [list(i.strip()) for i in input_str.split("\n")]
2940
result = expand(input_grid)
3041

3142
expected_grid = [list(i.strip()) for i in expanded_str.split("\n")]
3243
assert result == expected_grid
3344

45+
3446
def test_total():
35-
expanded_grid = [list(i.strip()) for i in expanded_str.split("\n")]
36-
galaxies = find_galaxies(expanded_grid)
47+
input_grid = [list(i.strip()) for i in input_str.split("\n")]
48+
galaxies = find_galaxies(input_grid)
49+
expand(input_grid)
3750
result = all_pairs(galaxies)
3851
assert result == 374
3952

4053

54+
small_input = """\
55+
..#..
56+
.....
57+
...#.
58+
.....
59+
.....\
60+
"""
4161

42-
62+
def test_small():
63+
input_grid = [list(i.strip()) for i in small_input.split("\n")]
64+
galaxies = find_galaxies(input_grid)
65+
expand(input_grid)
66+
result = all_pairs(galaxies)
67+
#assert galaxies == [Coordinate(4,0),Coordinate(5,3)]
68+
assert result == 4

0 commit comments

Comments
 (0)