-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday11.py
56 lines (40 loc) · 1.33 KB
/
day11.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
"""
Advent of Code 2023, Day 11: Cosmic Expansion.
"""
import sys
from itertools import combinations
from typing import TextIO
from aoc_2023.grid import Grid
def sum_distances(grid: Grid, offset: int) -> int:
columns = []
column_offset = 0
for x in range(grid.width):
if not any(p.real == x for p in grid.keys()):
column_offset += offset - 1
columns.append(column_offset)
rows = []
row_offset = 0
for y in range(grid.height):
if not any(p.imag == y for p in grid.keys()):
row_offset += offset - 1
rows.append(row_offset)
expanded = {
(p.real + columns[int(p.real)], p.imag + rows[int(p.imag)]) for p in grid.keys()
}
return sum(
abs(x1 - x2) + abs(y1 - y2) for (x1, y1), (x2, y2) in combinations(expanded, 2)
)
def part_one(file: TextIO) -> int:
grid = Grid.from_ascii_grid(file)
return sum_distances(grid, 2)
def part_two(file: TextIO, n: int) -> int:
grid = Grid.from_ascii_grid(file)
return sum_distances(grid, n)
def main():
filename = sys.argv[0].replace(".py", ".txt")
with open(filename, encoding="utf-8") as file:
print("Part one:", part_one(file))
with open(filename, encoding="utf-8") as file:
print("Part two:", part_two(file, 1000000))
if __name__ == "__main__":
main()