From 933fc2da0b75990029497821f18f6367b6786875 Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Mon, 11 Dec 2023 07:05:49 +0000 Subject: [PATCH 1/4] 2023: d11: ex1: add input --- 2023/d11/ex1/input | 140 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 140 insertions(+) create mode 100644 2023/d11/ex1/input diff --git a/2023/d11/ex1/input b/2023/d11/ex1/input new file mode 100644 index 0000000..264f0a7 --- /dev/null +++ b/2023/d11/ex1/input @@ -0,0 +1,140 @@ +..#..........#................................................#............................................................................. +...................................#............................................#..........................#............................#... +......................................................................#.............................#....................................... +........#...................................#..................................................................................#............ +.................#.........#..........#...............#........................................#.................#.......................... +.................................#..............#...................................#....................................................... +#.........................................................................#...............#............................#.................... +.......................#........................................................#........................................................... +.......#.........................................................#................................#.................................#....... +..................#...............................#...........................................................#............................. +...........#.................................#..............#................................................................#...........#.. +..........................#........#........................................................................................................ +.#..................................................................#................#.................#.................................... +......................#.........................#.....................................................................................#..... +..........................................#....................#..............#....................#........................................ +.........................................................................................#.........................#.....#.................. +..............#............................................................................................................................. +..........................................................................................................#........................#........ +.....#.........................#.............#....................#.......#........#........................................#............... +..........................#...........#...............................................................#..................................... +...................#...........................................................................#................#........................... +#............#........................................#................................................................................#.... +......................................................................................................................#.........#........... +........................................#................................................................................................... +...#............#........#...........................................................#...................................................... +.............................................#.............................................................................................. +............#.......#..........................................................................#..................#.....#................... +................................................................................#.........................#....................#.....#...... +.................................................................#.......................................................................... +..........................#...............#.................................................#.................#............................. +#.........#...........................................#............................................#........................................ +.....................#................#................................#.................................................................... +.....................................................................................#.....................#............#................... +............................................................................................................................................ +.............................#..........................................................................................................#... +.................#..................#......#......#............................#......................#..........#...........#.............. +.......#.................#...........................................#...................................................................... +......................................................................................#..................................................... +...#....................................................#......#..................................#......................................... +............#........#.........#..............#.........................#....................................#.............................. +.......................................................................................................#................#.....#............. +..............................................................................................#............................................. +....................................................................................#..............................#......................#. +#.............#.........#..................#.............................................#...........................................#...... +.............................#......#...........#.........#...................#............................................................. +.......#...........................................................................................#.....#.................................. +...................#........................................................................................................#.....#......... +....................................................#..................#.................................................................... +.................................#............................#............................................................................. +.......................#.................................#...........................................................................#...... +........................................#..........................................#............#...............#........................... +.........................................................................................#.................................................. +.............................................#......................#....................................................................... +..........................#...........................................................................#.........................#........... +............................................................................................................................................ +...........#....................................................................#......#.................................................... +....................................................................................................................#......................# +...............#........................#....................................................#.............................................. +.....#.........................................#................#........................................................................... +.......................#.............................................#................................#..................#.........#........ +#.........#.........................................#..........................#........#.......................#........................... +..............................#............................................................................................................. +.....................................#....................................................................#.....................#........... +..............................................................#.............#............................................................... +..#......................................................................................................................................#.. +.................#.........#........................................................#....................................#.................. +................................#.......................#.......................................#........................................... +..........................................#.....#...................................................................#....................... +......#......#.........................................................................................#......................#............. +......................................#..............#...................................................................................... +..................#...........#.................................#........................................................................... +..#.......#............................................................................#...................................................# +......................#..................................#..............#................................................................... +..........................................................................................................................#................. +.................................#.........#....................................................#........................................... +............................................................................................................................................ +....................#...............................................................................#....................................... +.#...................................#..........#........................................................................................... +.......#..............................................................#.................................#................................... +.............................#...........#.....................#.................................#.............................#............ +.......................#....................................................................#...................#......................#.... +.............#.....................#.................#..........................#....................................#...................... +...........................................................................#............#...........#..............................#........ +..........................................................................................................................................#. +.#..............................#........................................................................................................... +.............................................#............#...............................................#................................. +...............#.......#..........................#.................#.......................#...........................#................... +...............................................................................#.................................................#.......... +.........#..............................#..............................................................#......#............................. +............................................................................................................................................ +................................................#........................................#.................................................. +.........................................................................#......................#.....................................#..... +...........#......#..........#......................#......................................................................#................ +..........................................................#...................#.....#.......#.....................#......................... +......................................#..................................................................................................... +........#................................................................................................................................#.. +......................#........................#................#....................................#........#............................. +..#.......................................#.......................................................................................#......... +..............#............................................................................................................................. +.........................................................................#.................................................................. +.........................#..........#........#........................................#..................................................... +.....#..................................................#..................................................................................# +..............................#................................#..............#..............#................#............#................ +........................................................................................................#.............#.........#........... +.#.............#.......................................................#.................................................................... +.......................................#.................................................................................................... +.......................#..........................#.......#................................................................................. +.......#........................................................................#........................................................... +............................#........................................#................................................................#..... +..........................................#..........#........................................................#............................. +..............#..........................................................................#.....#..................................#......... +#........................................................................................................................................... +..................................#................................................................#........................#............... +..................................................................#.........................#...............#..........................#.... +.......................................#...................................#..........#............................#........................ +..................#.....#.....#.............#..........................................................#.................................... +...................................................................................................................................#........ +......................................................#.......................................................#............................. +...........#...............#...................#...............#....................#...............#.................#..................... +.........................................#......................................................................................#........... +...............................#.........................................................#......#.................#.....................#... +..#.................................................#....................................................#...............#.................. +........#.............................#..................................................................................................... +..............#.........#................................................#.................................................................. +..........................................#.....................................#....................#.......................#.............. +............................................................................................................................................ +................................#...............#..........................................................#.....#...............#.......... +..#.......#................................................................................................................................. +........................................................................#...............................................#................... +............................#.................................#.........................#..........#........................................ +....................#.............................#.........................................................................#............... +........................................................#....................................#..........#......#............................ +........#................................................................................................................................... +.........................................................................#..........#....................................................... +............#......................#............#.................................................................#.....................#... +.............................#.....................................................................#........................................ +........................................................................................................................#................... +.......#.........#...........................................................................#.........#......#............................. +........................#.............................#..........#...................................................................#...... +...#.................................#........#................................#.....#.......................................#.............. From 3d09397c54345aeae0f28b180c86e6a60c456747 Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Mon, 11 Dec 2023 07:05:54 +0000 Subject: [PATCH 2/4] 2023: d11: ex1: add solution --- 2023/d11/ex1/ex1.py | 64 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100755 2023/d11/ex1/ex1.py diff --git a/2023/d11/ex1/ex1.py b/2023/d11/ex1/ex1.py new file mode 100755 index 0000000..6a7bbec --- /dev/null +++ b/2023/d11/ex1/ex1.py @@ -0,0 +1,64 @@ +#!/usr/bin/env python + +import dataclasses +import sys +from typing import NamedTuple + + +class Point(NamedTuple): + x: int + y: int + + +@dataclasses.dataclass +class Expansion: + lines: set[int] + rows: set[int] + + +def solve(input: list[str]) -> int: + def parse(input: list[str]) -> tuple[list[Point], Expansion]: + res: list[Point] = [] + expansion = Expansion(set(range(len(input))), set(range(len(input[0])))) + + for x, line in enumerate(input): + for y, c in enumerate(line): + if c != "#": + continue + res.append(Point(x, y)) + expansion.lines.discard(x) + expansion.rows.discard(y) + + return res, expansion + + def do_expansion(galaxies: list[Point], expansion: Expansion) -> list[Point]: + new_lines = [0] * (len(input) + len(expansion.lines)) + new_rows = [0] * (len(input[0]) + len(expansion.rows)) + + for i in range(len(new_lines)): + new_lines[i] = new_lines[i - 1] + (i in expansion.lines) + 1 + + for i in range(len(new_rows)): + new_rows[i] = new_rows[i - 1] + (i in expansion.rows) + 1 + + return [Point(new_lines[p.x], new_rows[p.y]) for p in galaxies] + + def dist(a: Point, b: Point) -> int: + return abs(a.x - b.x) + abs(a.y - b.y) + + galaxies, expansion = parse(input) + galaxies = do_expansion(galaxies, expansion) + return sum( + dist(galaxies[i], galaxies[j]) + for i in range(len(galaxies)) + for j in range(i + 1, len(galaxies)) + ) + + +def main() -> None: + input = sys.stdin.read().splitlines() + print(solve(input)) + + +if __name__ == "__main__": + main() From c7e9bf217e4403d345dcbb8952ec0a4e0e881d43 Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Mon, 11 Dec 2023 07:06:01 +0000 Subject: [PATCH 3/4] 2023: d11: ex2: add input --- 2023/d11/ex2/input | 140 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 140 insertions(+) create mode 100644 2023/d11/ex2/input diff --git a/2023/d11/ex2/input b/2023/d11/ex2/input new file mode 100644 index 0000000..264f0a7 --- /dev/null +++ b/2023/d11/ex2/input @@ -0,0 +1,140 @@ +..#..........#................................................#............................................................................. +...................................#............................................#..........................#............................#... +......................................................................#.............................#....................................... +........#...................................#..................................................................................#............ +.................#.........#..........#...............#........................................#.................#.......................... +.................................#..............#...................................#....................................................... +#.........................................................................#...............#............................#.................... +.......................#........................................................#........................................................... +.......#.........................................................#................................#.................................#....... +..................#...............................#...........................................................#............................. +...........#.................................#..............#................................................................#...........#.. +..........................#........#........................................................................................................ +.#..................................................................#................#.................#.................................... +......................#.........................#.....................................................................................#..... +..........................................#....................#..............#....................#........................................ +.........................................................................................#.........................#.....#.................. +..............#............................................................................................................................. +..........................................................................................................#........................#........ +.....#.........................#.............#....................#.......#........#........................................#............... +..........................#...........#...............................................................#..................................... +...................#...........................................................................#................#........................... +#............#........................................#................................................................................#.... +......................................................................................................................#.........#........... +........................................#................................................................................................... +...#............#........#...........................................................#...................................................... +.............................................#.............................................................................................. +............#.......#..........................................................................#..................#.....#................... +................................................................................#.........................#....................#.....#...... +.................................................................#.......................................................................... +..........................#...............#.................................................#.................#............................. +#.........#...........................................#............................................#........................................ +.....................#................#................................#.................................................................... +.....................................................................................#.....................#............#................... +............................................................................................................................................ +.............................#..........................................................................................................#... +.................#..................#......#......#............................#......................#..........#...........#.............. +.......#.................#...........................................#...................................................................... +......................................................................................#..................................................... +...#....................................................#......#..................................#......................................... +............#........#.........#..............#.........................#....................................#.............................. +.......................................................................................................#................#.....#............. +..............................................................................................#............................................. +....................................................................................#..............................#......................#. +#.............#.........#..................#.............................................#...........................................#...... +.............................#......#...........#.........#...................#............................................................. +.......#...........................................................................................#.....#.................................. +...................#........................................................................................................#.....#......... +....................................................#..................#.................................................................... +.................................#............................#............................................................................. +.......................#.................................#...........................................................................#...... +........................................#..........................................#............#...............#........................... +.........................................................................................#.................................................. +.............................................#......................#....................................................................... +..........................#...........................................................................#.........................#........... +............................................................................................................................................ +...........#....................................................................#......#.................................................... +....................................................................................................................#......................# +...............#........................#....................................................#.............................................. +.....#.........................................#................#........................................................................... +.......................#.............................................#................................#..................#.........#........ +#.........#.........................................#..........................#........#.......................#........................... +..............................#............................................................................................................. +.....................................#....................................................................#.....................#........... +..............................................................#.............#............................................................... +..#......................................................................................................................................#.. +.................#.........#........................................................#....................................#.................. +................................#.......................#.......................................#........................................... +..........................................#.....#...................................................................#....................... +......#......#.........................................................................................#......................#............. +......................................#..............#...................................................................................... +..................#...........#.................................#........................................................................... +..#.......#............................................................................#...................................................# +......................#..................................#..............#................................................................... +..........................................................................................................................#................. +.................................#.........#....................................................#........................................... +............................................................................................................................................ +....................#...............................................................................#....................................... +.#...................................#..........#........................................................................................... +.......#..............................................................#.................................#................................... +.............................#...........#.....................#.................................#.............................#............ +.......................#....................................................................#...................#......................#.... +.............#.....................#.................#..........................#....................................#...................... +...........................................................................#............#...........#..............................#........ +..........................................................................................................................................#. +.#..............................#........................................................................................................... +.............................................#............#...............................................#................................. +...............#.......#..........................#.................#.......................#...........................#................... +...............................................................................#.................................................#.......... +.........#..............................#..............................................................#......#............................. +............................................................................................................................................ +................................................#........................................#.................................................. +.........................................................................#......................#.....................................#..... +...........#......#..........#......................#......................................................................#................ +..........................................................#...................#.....#.......#.....................#......................... +......................................#..................................................................................................... +........#................................................................................................................................#.. +......................#........................#................#....................................#........#............................. +..#.......................................#.......................................................................................#......... +..............#............................................................................................................................. +.........................................................................#.................................................................. +.........................#..........#........#........................................#..................................................... +.....#..................................................#..................................................................................# +..............................#................................#..............#..............#................#............#................ +........................................................................................................#.............#.........#........... +.#.............#.......................................................#.................................................................... +.......................................#.................................................................................................... +.......................#..........................#.......#................................................................................. +.......#........................................................................#........................................................... +............................#........................................#................................................................#..... +..........................................#..........#........................................................#............................. +..............#..........................................................................#.....#..................................#......... +#........................................................................................................................................... +..................................#................................................................#........................#............... +..................................................................#.........................#...............#..........................#.... +.......................................#...................................#..........#............................#........................ +..................#.....#.....#.............#..........................................................#.................................... +...................................................................................................................................#........ +......................................................#.......................................................#............................. +...........#...............#...................#...............#....................#...............#.................#..................... +.........................................#......................................................................................#........... +...............................#.........................................................#......#.................#.....................#... +..#.................................................#....................................................#...............#.................. +........#.............................#..................................................................................................... +..............#.........#................................................#.................................................................. +..........................................#.....................................#....................#.......................#.............. +............................................................................................................................................ +................................#...............#..........................................................#.....#...............#.......... +..#.......#................................................................................................................................. +........................................................................#...............................................#................... +............................#.................................#.........................#..........#........................................ +....................#.............................#.........................................................................#............... +........................................................#....................................#..........#......#............................ +........#................................................................................................................................... +.........................................................................#..........#....................................................... +............#......................#............#.................................................................#.....................#... +.............................#.....................................................................#........................................ +........................................................................................................................#................... +.......#.........#...........................................................................#.........#......#............................. +........................#.............................#..........#...................................................................#...... +...#.................................#........#................................#.....#.......................................#.............. From c65d56801640e30d2eee31c35e264d34b8136b1a Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Mon, 11 Dec 2023 07:06:10 +0000 Subject: [PATCH 4/4] 2023: d11: ex2: add solution --- 2023/d11/ex2/ex2.py | 64 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100755 2023/d11/ex2/ex2.py diff --git a/2023/d11/ex2/ex2.py b/2023/d11/ex2/ex2.py new file mode 100755 index 0000000..b457cbc --- /dev/null +++ b/2023/d11/ex2/ex2.py @@ -0,0 +1,64 @@ +#!/usr/bin/env python + +import dataclasses +import sys +from typing import NamedTuple + + +class Point(NamedTuple): + x: int + y: int + + +@dataclasses.dataclass +class Expansion: + lines: set[int] + rows: set[int] + + +def solve(input: list[str]) -> int: + def parse(input: list[str]) -> tuple[list[Point], Expansion]: + res: list[Point] = [] + expansion = Expansion(set(range(len(input))), set(range(len(input[0])))) + + for x, line in enumerate(input): + for y, c in enumerate(line): + if c != "#": + continue + res.append(Point(x, y)) + expansion.lines.discard(x) + expansion.rows.discard(y) + + return res, expansion + + def do_expansion(galaxies: list[Point], expansion: Expansion) -> list[Point]: + new_lines = [0] * (len(input) + len(expansion.lines)) + new_rows = [0] * (len(input[0]) + len(expansion.rows)) + + for i in range(len(new_lines)): + new_lines[i] = new_lines[i - 1] + (1000000 - 1) * (i in expansion.lines) + 1 + + for i in range(len(new_rows)): + new_rows[i] = new_rows[i - 1] + (1000000 - 1) * (i in expansion.rows) + 1 + + return [Point(new_lines[p.x], new_rows[p.y]) for p in galaxies] + + def dist(a: Point, b: Point) -> int: + return abs(a.x - b.x) + abs(a.y - b.y) + + galaxies, expansion = parse(input) + galaxies = do_expansion(galaxies, expansion) + return sum( + dist(galaxies[i], galaxies[j]) + for i in range(len(galaxies)) + for j in range(i + 1, len(galaxies)) + ) + + +def main() -> None: + input = sys.stdin.read().splitlines() + print(solve(input)) + + +if __name__ == "__main__": + main()