2023 day 11.

This commit is contained in:
Mikaël Capelle 2023-12-11 07:00:19 +01:00 committed by Mikael CAPELLE
parent f5aabbee8f
commit 1a6ab1cc0e
4 changed files with 183 additions and 5 deletions

View File

@ -68,7 +68,7 @@ mapping: dict[tuple[int, int], dict[tuple[int, int], Symbol]] = {
lines[si][sj] = mapping[di1, dj1][di2, dj2] lines[si][sj] = mapping[di1, dj1][di2, dj2]
# find the points inside the loop using an adaptation of ray casting for a discrete # find the points inside the loop using an adaptation of ray casting for a discrete
# grid (https://en.wikipedia.org/wiki/Ray_casting) # grid (https://stackoverflow.com/a/218081/2666289)
# #
# use a set for faster '... in loop' check # use a set for faster '... in loop' check
# #

View File

@ -1,13 +1,41 @@
import sys import sys
from collections import defaultdict
from dataclasses import dataclass import numpy as np
lines = sys.stdin.read().splitlines() lines = sys.stdin.read().splitlines()
data = np.array([[c == "#" for c in line] for line in lines])
rows = {c for c in range(data.shape[0]) if not data[c, :].any()}
columns = {c for c in range(data.shape[1]) if not data[:, c].any()}
galaxies_y, galaxies_x = np.where(data) # type: ignore
def compute_total_distance(expansion: int) -> int:
distances: list[int] = []
for g1 in range(len(galaxies_y)):
x1, y1 = int(galaxies_x[g1]), int(galaxies_y[g1])
for g2 in range(g1 + 1, len(galaxies_y)):
x2, y2 = int(galaxies_x[g2]), int(galaxies_y[g2])
dx = sum(
1 + (expansion - 1) * (x in columns)
for x in range(min(x1, x2), max(x1, x2))
)
dy = sum(
1 + (expansion - 1) * (y in rows)
for y in range(min(y1, y2), max(y1, y2))
)
distances.append(dx + dy)
return sum(distances)
# part 1 # part 1
answer_1 = ... answer_1 = compute_total_distance(2)
print(f"answer 1 is {answer_1}") print(f"answer 1 is {answer_1}")
# part 2 # part 2
answer_2 = ... answer_2 = compute_total_distance(1000000)
print(f"answer 2 is {answer_2}") print(f"answer 2 is {answer_2}")

View File

@ -0,0 +1,140 @@
............................................#.........#.......#.................#.......#........#..........................................
......................................#.........................................................................#.........................#.
#........................................................................................................................#..................
.....#...................................................#..................................................................................
.......................................................................#..................#..............#...........#......................
............#........#...................#..........................................................#........................#..............
...............................#......................................................................................................#.....
................................................#.....#.........#................................................................#..........
#........#.......................................................................................#........................#.................
..........................................................#...........................#.....................................................
.......................................................................#....................................#...............................
............#.................#.....#..............#..........................#.............................................................
....................#........................#............................................#............#...............#....................
...#....................................................#...................................................................................
.................................#...............................#...................#...........................#............#.............
.................................................................................................#..........................................
................................................#...........................#...............#...............................................
.#........#.............#...........................................................................................#............#..........
................#....................#......................................................................#.............................#.
................................#............#.....................#........................................................................
.....................#................................................................#......................................#..............
........................................#..........#.......#..................................#........................................#....
.....#..........................................................#..............#.......................#.............#......................
..............................#...........................................................#........................................#........
.........#.......................................................................................................#..........................
......................#............................................#........................................................................
.......................................#.....#...................................#...............#...........#.........#....................
..........................................................................#.............#...................................#...............
#......#.................#.........................................................................................#........................
...............#........................................#.....#.....................#...............#....................................#..
...................................#..........................................#..........................................#.......#..........
....................................................................#.......................#...............................................
.................................................#..........................................................................................
.........................................#................................#...........................................................#.....
#..............................#..........................#..........................#..........................#...........................
.....#..............#...........................................................#........................#....................#.............
...............#............................................................................................................................
.............................................#.....#.................................................#......................................
............................................................................................................................................
.....................................#.................#......#.................................#...........................................
..........................................#.................................#.............#.................................#...............
.................................................................................#................................#..............#..........
..........#...........#....................................................................................#................................
...............#..............#.....................................#................................#....................................#.
#.....................................#.......#.......#...................................................................#.................
............................................................................................................................................
............................................................................................#...............................................
........#...........#.............#.............................................#.........................#........#........................
.............................#.............#..................................................................................#.............
.......................................................#..........#...................................#.......#......................#......
.....#.................................................................................#........#...........................................
...........................................................................#................................................................
..............#..........................#..................................................................................................
..#.........................#.......#................#.....................................#.........................#......................
...................................................................................#......................................#.................
..................#.............................#.............#....................................#.........#........................#.....
............................................................................................................................................
.......................................................#..............................#.........................................#...........
.............................#...................................................#............#............................................#
............................................................................................................................................
...................................#...........#................#..............................................#........#..............#....
.#......#...........#.....#.............#..................#........................................#.......................................
............................................................................................#................................#..............
............................................................................................................................................
....#........#...................#....................#............#......#.......#.......................................................#.
..............................................#..............#.................................#....................#.......................
............................................................................................................................................
#......................#.....................................................#.............................................#................
........#................................#..........................................#.......................#...............................
.......................................................................................................#...........................#........
..................#...........#................#............................................................................................
............#.....................................................................................#.......................................#.
......................#.....................................................................................................................
....................................................................#..........................................#............................
..........................................#...........#...................................#.............#...................................
...#................................................................................................................#........#..............
...............................#..............................#.................#...........................#...............................
......................................#...........#.........................................................................................
.................#...........................#........................#..............................................................#......
........................#.............................................................................#.....................................
..........#.......................#..............................#....................#.........#.......................#...................
..#.........................................................................................................................................
.................................................................................#........#..................#....................#.........
............................................................................................................................................
............................................................................................................................................
...............................#......................#..............................................................................#......
.......................#..................#.........................#.......#....................#..........................................
....#.........................................................................................................#.................#...........
..............#...................#...........#..............#......................#.......................................................
............................................................................................................................................
..............................#..............................................................#.....................................#........
.....................................#.......................................#.........#..........#....................#....................
......#...........................................#.................#.......................................................................
#...........#.......#..................................#..............................................#......................#..............
................................#...................................................................................#.......................
............................................#................#..............................................................................
..........................................................................#..................................#..............................
.......#....................................................................................#..........................#.........#..........
..#...........#......................................................................#..................................................#...
...........................................................#.....#..........................................................................
..................#...............#................................................................#........................................
............................................................................................................................................
#......................#...............................#....................#..................................#..........#........#........
..........................................................................................#.................................................
...........#................#..............................................................................................................#
...........................................................#........................#.............#.........................................
................................................#...........................................................................................
...................#.....#..........#......................................................................#.....#...................#......
........#......................#............................................................#.........................#.....................
..............#..............................................................#.........#....................................................
....................................................................#..........................................................#............
#..............................................#............................................................................................
.....#........................................................................................................#.............................
......................#................#.........................#........................#.........................................#.......
............................................#......#...............................#..............#.........................................
.................................#..........................................................................................................
..........................#...................................#.........#...................................................................
..........#.................................................................................................................#...............
#...............#...........................................................#..........................................................#....
..............................................................................................#..................................#..........
......................#....................#........................................#.............................#........................#
................................................#......#....................................................................................
.......................................................................#..............................#......#..............................
.........................#.................................................................#.....#........................#.................
.#..................#..........#......................................................#.....................................................
..........#..............................................#........#.........................................................................
.......................................#.............................................................................#..............#.......
............................#.................#...................................#........................................................#
.....................................................#..........................................................................#...........
....#..........#.........................................................................................#......#...........................
......................#.....................................................................................................................
...........#.............................#.............................#................................................#...................
..................#..................................................................#......................................................
.............................#.........................................................................#.....................#.........#....
........#..................................................................#..............#.................................................
#........................................................#.........#........................................................................
.......................................#........#.............................................................#.............................
...................#..............#................................................................#.....#.........#........................
.............#.....................................................................#.....................................#............#.....
......#...................................................................#.....................................................#...........

View File

@ -0,0 +1,10 @@
...#......
.......#..
#.........
..........
......#...
.#........
.........#
..........
.......#..
#...#.....