advent-of-code/2021/day04_bingo.py

97 lines
2.7 KiB
Python
Raw Normal View History

2021-12-04 10:55:01 +01:00
import itertools
2022-12-02 13:35:44 +01:00
from typing import TypeAlias
2021-12-04 10:55:01 +01:00
2022-12-02 13:35:44 +01:00
Numbers: TypeAlias = list[int]
Grid: TypeAlias = list[Numbers]
2021-12-04 10:55:01 +01:00
def main(filename: str, expected_part_1: int = None, expected_part_2: int = None):
print(f"\n+ Running on {filename}")
with open(filename) as f:
data = f.read().strip().split("\n\n")
numbers, grids = parse_data(data)
solution_part_1 = solve_part_1(numbers, grids)
print(f"1. Found {solution_part_1}")
if expected_part_1:
assert expected_part_1 == solution_part_1
solution_part_2 = solve_part_2(numbers, grids)
print(f"2. Found {solution_part_2}")
if expected_part_2:
assert expected_part_2 == solution_part_2
2022-12-02 13:35:44 +01:00
def parse_data(data: list[str]) -> (Numbers, list[Grid]):
2021-12-04 10:55:01 +01:00
numbers: Numbers = list(map(int, data[0].split(",")))
data = data[1:]
2022-12-02 13:35:44 +01:00
grids: list[Grid] = []
2021-12-04 10:55:01 +01:00
for grid in data:
parsed_grid: Grid = []
grid_lines = grid.split("\n")
for line in grid_lines:
line = list(map(int, line.split()))
parsed_grid.append(line)
grids.append(parsed_grid)
return numbers, grids
2022-12-02 13:35:44 +01:00
def solve_part_1(numbers: Numbers, grids: list[Grid]) -> int:
seen: set[int] = set()
2021-12-04 10:55:01 +01:00
for number in numbers:
seen.add(number)
for grid in grids:
if check_bingo(grid, seen):
return sum(unseen(grid, seen)) * number
2022-12-02 13:35:44 +01:00
def check_bingo(grid: Grid, seen: set[int]) -> bool:
2021-12-04 10:55:01 +01:00
return check_row(grid, seen) or check_column(grid, seen)
2022-12-02 13:35:44 +01:00
def check_row(grid: Grid, seen: set[int]) -> bool:
2021-12-04 10:55:01 +01:00
for row in grid:
if check_line(row, seen):
return True
return False
2022-12-02 13:35:44 +01:00
def check_column(grid: Grid, seen: set[int]) -> bool:
2021-12-04 10:55:01 +01:00
for i in range(len(grid[0])):
column = [row[i] for row in grid]
if check_line(column, seen):
return True
return False
2022-12-02 13:35:44 +01:00
def check_line(line: Numbers, seen: set[int]) -> bool:
2021-12-04 10:55:01 +01:00
for number in line:
if number not in seen:
return False
return True
2022-12-02 13:35:44 +01:00
def unseen(grid: Grid, seen: set[int]) -> Numbers:
2021-12-04 10:55:01 +01:00
return [n for n in itertools.chain.from_iterable(grid) if n not in seen]
2022-12-02 13:35:44 +01:00
def solve_part_2(numbers: Numbers, grids: list[Grid]) -> int:
seen: set[int] = set()
2021-12-04 10:55:01 +01:00
score = 0
2022-12-02 13:35:44 +01:00
winners: set[int] = set()
2021-12-04 10:55:01 +01:00
for number in numbers:
seen.add(number)
for index, grid in enumerate(grids):
if index in winners:
continue
if check_bingo(grid, seen):
score = sum(unseen(grid, seen)) * number
winners.add(index)
return score
if __name__ == "__main__":
main("inputs/day04-test1", expected_part_1=4512, expected_part_2=1924)
main("inputs/day04", expected_part_1=23177)