From d6b99454d2e84152af001b14621ecbb7f303b0a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mika=C3=ABl=20Capelle?= Date: Mon, 11 Dec 2023 07:00:19 +0100 Subject: [PATCH] 2023 day 11. --- 2023/day10.py | 2 +- 2023/day11.py | 36 +++++++++-- 2023/inputs/day11.txt | 140 ++++++++++++++++++++++++++++++++++++++++++ 2023/tests/day11.txt | 10 +++ 4 files changed, 183 insertions(+), 5 deletions(-) diff --git a/2023/day10.py b/2023/day10.py index 8a2e9fd..473dac5 100644 --- a/2023/day10.py +++ b/2023/day10.py @@ -68,7 +68,7 @@ mapping: dict[tuple[int, int], dict[tuple[int, int], Symbol]] = { lines[si][sj] = mapping[di1, dj1][di2, dj2] # 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 # diff --git a/2023/day11.py b/2023/day11.py index 992bf35..b18b1c9 100644 --- a/2023/day11.py +++ b/2023/day11.py @@ -1,13 +1,41 @@ import sys -from collections import defaultdict -from dataclasses import dataclass + +import numpy as np 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 -answer_1 = ... +answer_1 = compute_total_distance(2) print(f"answer 1 is {answer_1}") # part 2 -answer_2 = ... +answer_2 = compute_total_distance(1000000) print(f"answer 2 is {answer_2}") diff --git a/2023/inputs/day11.txt b/2023/inputs/day11.txt index e69de29..c238ba0 100644 --- a/2023/inputs/day11.txt +++ b/2023/inputs/day11.txt @@ -0,0 +1,140 @@ +............................................#.........#.......#.................#.......#........#.......................................... +......................................#.........................................................................#.........................#. +#........................................................................................................................#.................. +.....#...................................................#.................................................................................. +.......................................................................#..................#..............#...........#...................... +............#........#...................#..........................................................#........................#.............. +...............................#......................................................................................................#..... +................................................#.....#.........#................................................................#.......... +#........#.......................................................................................#........................#................. +..........................................................#...........................#..................................................... +.......................................................................#....................................#............................... +............#.................#.....#..............#..........................#............................................................. +....................#........................#............................................#............#...............#.................... +...#....................................................#................................................................................... +.................................#...............................#...................#...........................#............#............. +.................................................................................................#.......................................... +................................................#...........................#...............#............................................... +.#........#.............#...........................................................................................#............#.......... +................#....................#......................................................................#.............................#. +................................#............#.....................#........................................................................ +.....................#................................................................#......................................#.............. +........................................#..........#.......#..................................#........................................#.... +.....#..........................................................#..............#.......................#.............#...................... +..............................#...........................................................#........................................#........ +.........#.......................................................................................................#.......................... +......................#............................................#........................................................................ +.......................................#.....#...................................#...............#...........#.........#.................... +..........................................................................#.............#...................................#............... +#......#.................#.........................................................................................#........................ +...............#........................................#.....#.....................#...............#....................................#.. +...................................#..........................................#..........................................#.......#.......... +....................................................................#.......................#............................................... +.................................................#.......................................................................................... +.........................................#................................#...........................................................#..... +#..............................#..........................#..........................#..........................#........................... +.....#..............#...........................................................#........................#....................#............. +...............#............................................................................................................................ +.............................................#.....#.................................................#...................................... +............................................................................................................................................ +.....................................#.................#......#.................................#........................................... +..........................................#.................................#.............#.................................#............... +.................................................................................#................................#..............#.......... +..........#...........#....................................................................................#................................ +...............#..............#.....................................#................................#....................................#. +#.....................................#.......#.......#...................................................................#................. +............................................................................................................................................ +............................................................................................#............................................... +........#...........#.............#.............................................#.........................#........#........................ +.............................#.............#..................................................................................#............. +.......................................................#..........#...................................#.......#......................#...... +.....#.................................................................................#........#........................................... +...........................................................................#................................................................ +..............#..........................#.................................................................................................. +..#.........................#.......#................#.....................................#.........................#...................... +...................................................................................#......................................#................. +..................#.............................#.............#....................................#.........#........................#..... +............................................................................................................................................ +.......................................................#..............................#.........................................#........... +.............................#...................................................#............#............................................# +............................................................................................................................................ +...................................#...........#................#..............................................#........#..............#.... +.#......#...........#.....#.............#..................#........................................#....................................... +............................................................................................#................................#.............. +............................................................................................................................................ +....#........#...................#....................#............#......#.......#.......................................................#. +..............................................#..............#.................................#....................#....................... +............................................................................................................................................ +#......................#.....................................................#.............................................#................ +........#................................#..........................................#.......................#............................... +.......................................................................................................#...........................#........ +..................#...........#................#............................................................................................ +............#.....................................................................................#.......................................#. +......................#..................................................................................................................... +....................................................................#..........................................#............................ +..........................................#...........#...................................#.............#................................... +...#................................................................................................................#........#.............. +...............................#..............................#.................#...........................#............................... +......................................#...........#......................................................................................... +.................#...........................#........................#..............................................................#...... +........................#.............................................................................#..................................... +..........#.......................#..............................#....................#.........#.......................#................... +..#......................................................................................................................................... +.................................................................................#........#..................#....................#......... +............................................................................................................................................ +............................................................................................................................................ +...............................#......................#..............................................................................#...... +.......................#..................#.........................#.......#....................#.......................................... +....#.........................................................................................................#.................#........... +..............#...................#...........#..............#......................#....................................................... +............................................................................................................................................ +..............................#..............................................................#.....................................#........ +.....................................#.......................................#.........#..........#....................#.................... +......#...........................................#.................#....................................................................... +#...........#.......#..................................#..............................................#......................#.............. +................................#...................................................................................#....................... +............................................#................#.............................................................................. +..........................................................................#..................................#.............................. +.......#....................................................................................#..........................#.........#.......... +..#...........#......................................................................#..................................................#... +...........................................................#.....#.......................................................................... +..................#...............#................................................................#........................................ +............................................................................................................................................ +#......................#...............................#....................#..................................#..........#........#........ +..........................................................................................#................................................. +...........#................#..............................................................................................................# +...........................................................#........................#.............#......................................... +................................................#........................................................................................... +...................#.....#..........#......................................................................#.....#...................#...... +........#......................#............................................................#.........................#..................... +..............#..............................................................#.........#.................................................... +....................................................................#..........................................................#............ +#..............................................#............................................................................................ +.....#........................................................................................................#............................. +......................#................#.........................#........................#.........................................#....... +............................................#......#...............................#..............#......................................... +.................................#.......................................................................................................... +..........................#...................................#.........#................................................................... +..........#.................................................................................................................#............... +#...............#...........................................................#..........................................................#.... +..............................................................................................#..................................#.......... +......................#....................#........................................#.............................#........................# +................................................#......#.................................................................................... +.......................................................................#..............................#......#.............................. +.........................#.................................................................#.....#........................#................. +.#..................#..........#......................................................#..................................................... +..........#..............................................#........#......................................................................... +.......................................#.............................................................................#..............#....... +............................#.................#...................................#........................................................# +.....................................................#..........................................................................#........... +....#..........#.........................................................................................#......#........................... +......................#..................................................................................................................... +...........#.............................#.............................#................................................#................... +..................#..................................................................#...................................................... +.............................#.........................................................................#.....................#.........#.... +........#..................................................................#..............#................................................. +#........................................................#.........#........................................................................ +.......................................#........#.............................................................#............................. +...................#..............#................................................................#.....#.........#........................ +.............#.....................................................................#.....................................#............#..... +......#...................................................................#.....................................................#........... diff --git a/2023/tests/day11.txt b/2023/tests/day11.txt index e69de29..986aad4 100644 --- a/2023/tests/day11.txt +++ b/2023/tests/day11.txt @@ -0,0 +1,10 @@ +...#...... +.......#.. +#......... +.......... +......#... +.#........ +.........# +.......... +.......#.. +#...#.....