My Advent of Code solutions in Python. kevinyap.ca/2019/12/going-fast-in-advent-of-code/
advent-of-code python
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Add solution for 2023/11

+42
+42
2023/day11.py
··· 1 + import fileinput 2 + from itertools import combinations 3 + 4 + 5 + # Parse problem input. 6 + GALAXIES = set() 7 + 8 + for y, line in enumerate(fileinput.input()): 9 + for x, c in enumerate(line.strip()): 10 + if c == '#': 11 + GALAXIES.add((x, y)) 12 + 13 + # Find "expanded" rows and columns. 14 + empty_row = set(range(y)) 15 + empty_col = set(range(x)) 16 + 17 + for (gx, gy) in GALAXIES: 18 + empty_col.discard(gx) 19 + empty_row.discard(gy) 20 + 21 + # Solve problem. 22 + part_1 = 0 23 + part_2 = 0 24 + 25 + for (sx, sy), (ex, ey) in combinations(GALAXIES, 2): 26 + crossed = 0 27 + for y in range(min(sy, ey), max(sy, ey) + 1): 28 + if y in empty_row: 29 + crossed += 1 30 + 31 + for x in range(min(sx, ex), max(sx, ex) + 1): 32 + if x in empty_col: 33 + crossed += 1 34 + 35 + # Raw distance is the Manhattan distance between the pair. 36 + raw_distance = abs(sy - ey) + abs(sx - ex) 37 + part_1 += raw_distance + crossed * 1 38 + part_2 += raw_distance + crossed * 999999 39 + 40 + print("Part 1:", part_1) 41 + print("Part 2:", part_2) 42 +