Skip to content

Commit

Permalink
Merge pull request #97 from wimglenn/2023/11
Browse files Browse the repository at this point in the history
day 11
  • Loading branch information
wimglenn authored Dec 11, 2023
2 parents 1be2c69 + 958df68 commit de5e495
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
36 changes: 36 additions & 0 deletions aoc_wim/aoc2023/q11.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
"""
--- Day 11: Cosmic Expansion ---
https://adventofcode.com/2023/day/11
"""
from aocd import data
from itertools import combinations
from aoc_wim.zgrid import ZGrid
from aoc_wim.zgrid import manhattan_distance

grid = ZGrid(data)
h, w = grid.height, grid.width
gs = grid.z("#", first=False)
g_cols = {g.real for g in gs}
g_rows = {g.imag for g in gs}
empty_cols = [i for i in range(w) if i not in g_cols]
empty_rows = [i for i in range(h) if i not in g_rows]

# expansion factor for part b
f = 1_000_000
if len(gs) == 9:
# 9 galaxies for the example data
# use a smaller expansion factor
f = 100

a = b = 0
for g0, g1 in combinations(gs, 2):
d = manhattan_distance(g0, g1)
c01 = range(*sorted([int(g0.real), int(g1.real)]))
r01 = range(*sorted([int(g0.imag), int(g1.imag)]))
c = sum(1 for c in empty_cols if c in c01)
r = sum(1 for r in empty_rows if r in r01)
a += d + c + r
b += d + (c + r) * (f - 1)

print("answer_a:", a)
print("answer_b:", b)
12 changes: 12 additions & 0 deletions tests/2023/11/0.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
...#......
.......#..
#.........
..........
......#...
.#........
.........#
..........
.......#..
#...#.....
374
8410 # expansion factor 100

0 comments on commit de5e495

Please sign in to comment.