This commit is contained in:
Stefan Harmuth 2023-12-11 06:32:08 +01:00
parent 606a4bf4b2
commit 10800eb266
3 changed files with 210 additions and 0 deletions

60
day11.py Normal file
View File

@ -0,0 +1,60 @@
from tools.aoc import AOCDay
from tools.coordinate import Coordinate, DistanceAlgorithm
from tools.grid import Grid
from typing import Any
def expand_universe(grid: Grid, amount: int = 2):
"""amount as in 'one empty row should be {amount} empty rows instead"""
empty_x = set(grid.rangeX())
empty_y = set(grid.rangeY())
for c in grid.getActiveCells():
if c.x in empty_x:
empty_x.remove(c.x)
if c.y in empty_y:
empty_y.remove(c.y)
for c in reversed(sorted(grid.getActiveCells())):
shift_x = len([x for x in empty_x if x < c.x]) * (amount - 1)
shift_y = len([y for y in empty_y if y < c.y]) * (amount - 1)
grid.set(c, False)
grid.set(Coordinate(c.x + shift_x, c.y + shift_y), True)
def get_sum_dist(grid) -> int:
dist_sum = 0
c_list = list(grid.getActiveCells())
for i, a in enumerate(c_list[:-1]):
for b in c_list[i + 1 :]:
dist_sum += a.getDistanceTo(b, algorithm=DistanceAlgorithm.MANHATTAN)
return dist_sum
class Day(AOCDay):
inputs = [
[
(374, "input11_test"),
(9445168, "input11"),
],
[
(742305960572, "input11"),
],
]
def part1(self) -> Any:
grid = Grid.from_data(self.getInput(), translate={".": False, "#": True})
expand_universe(grid)
return get_sum_dist(grid)
def part2(self) -> Any:
grid = Grid.from_data(self.getInput(), translate={".": False, "#": True})
expand_universe(grid, 1_000_000)
return get_sum_dist(grid)
if __name__ == "__main__":
day = Day(2023, 11)
day.run(verbose=True)

140
inputs/input11 Normal file
View File

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

10
inputs/input11_test Normal file
View File

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