Day 22 - shortened code, more precise annotations

This commit is contained in:
Stefan Harmuth 2023-12-22 08:44:24 +01:00
parent 4a53e7efa4
commit 561f3ed765

View File

@ -3,7 +3,7 @@ from tools.aoc import AOCDay
from typing import Any
def get_space_below(brick: tuple[tuple], max_y: dict[tuple, int]) -> int:
def get_space_below(brick: tuple[tuple[int, int, int], ...], max_y: dict[tuple[int, int], int]) -> int:
below_y = 0
for x in range(brick[0][0], brick[-1][0] + 1):
for z in range(brick[0][2], brick[-1][2] + 1):
@ -12,7 +12,7 @@ def get_space_below(brick: tuple[tuple], max_y: dict[tuple, int]) -> int:
return brick[0][1] - below_y - 1
def drop(bricks: set[tuple]) -> (set[tuple], int):
def drop(bricks: set[tuple[tuple[int, int, int], ...]]) -> (set[tuple[tuple[int, int, int], ...]], int):
dropped_bricks = set()
max_y = defaultdict(int)
count = 0
@ -31,16 +31,6 @@ def drop(bricks: set[tuple]) -> (set[tuple], int):
return dropped_bricks, count
def can_disintegrate(brick: tuple, bricks: set[tuple]) -> bool:
_, count = drop(bricks - {brick})
return count == 0
def would_fall(brick: tuple, bricks: set[tuple]) -> int:
_, count = drop(bricks - {brick})
return count
class Day(AOCDay):
inputs = [
[
@ -53,7 +43,7 @@ class Day(AOCDay):
]
]
def parse_input(self) -> set[tuple]:
def parse_input(self) -> set[tuple[tuple[int, int, int], ...]]:
bricks = set()
for line in self.getInput():
a, b = line.split("~")
@ -71,14 +61,12 @@ class Day(AOCDay):
return bricks
def part1(self) -> Any:
bricks = self.parse_input()
dropped_bricks, _ = drop(bricks)
return sum(can_disintegrate(x, dropped_bricks) for x in dropped_bricks)
dropped_bricks, _ = drop(self.parse_input())
return sum(c[1] == 0 for c in (drop(dropped_bricks - {x}) for x in dropped_bricks))
def part2(self) -> Any:
bricks = self.parse_input()
dropped_bricks, _ = drop(bricks)
return sum(would_fall(x, dropped_bricks) for x in dropped_bricks)
dropped_bricks, _ = drop(self.parse_input())
return sum(c[1] for c in (drop(dropped_bricks - {x}) for x in dropped_bricks))
if __name__ == '__main__':