From 8f64cdb569e9542c3a56cc2315eba1d9e91ebd43 Mon Sep 17 00:00:00 2001 From: Thomas Voss Date: Sat, 4 Dec 2021 06:58:48 +0100 Subject: Add day 4 solutions --- 2021/04/puzzles.py | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 2021/04/puzzles.py (limited to '2021/04/puzzles.py') diff --git a/2021/04/puzzles.py b/2021/04/puzzles.py new file mode 100644 index 0000000..581aef5 --- /dev/null +++ b/2021/04/puzzles.py @@ -0,0 +1,51 @@ +#!/usr/bin/env python3 + +board = list[list[int]] +last: board = [[]] * 5 + + +def bingo(board: board) -> bool: + global last + + if any( + board[0][i] == board[1][i] + and board[1][i] == board[2][i] + and board[2][i] == board[3][i] + and board[3][i] == board[4][i] + or len(set(board[i])) == 1 + for i in range(5) + ): + last = board + return True + + return False + + +def main() -> None: + boards: list[board] = [] + with open("input", "r", encoding="utf-8") as f: + lines = f.readlines() + for i, line in enumerate(lines): + if i == 0: + draws = list(map(int, line.strip().split(","))) + elif line == "\n": + boards.append([list(map(int, lines[i + j].strip().split())) for j in range(1, 6)]) + + # START PART 1 + while not any(bingo(b) for b in boards): + # END PART 1 START PART 2 + while len(draws) > 0 and len(boards) > 0: + # END PART 2 + num = draws.pop(0) + for i, board in enumerate(boards): + for j, row in enumerate(board): + boards[i][j] = [-1 if n == num else n for n in row] + # START PART 2 + boards = list(filter(lambda b: not bingo(b), boards)) + # END PART 2 + + print(num * sum(map(lambda x: sum(filter(lambda n: n != -1, x)), last))) + + +if __name__ == "__main__": + main() -- cgit v1.2.3