aboutsummaryrefslogtreecommitdiff
path: root/2021/04/puzzles.py
diff options
context:
space:
mode:
authorThomas Voss <thomasvoss@live.com> 2021-12-04 06:58:48 +0100
committerThomas Voss <thomasvoss@live.com> 2021-12-04 06:58:48 +0100
commit8f64cdb569e9542c3a56cc2315eba1d9e91ebd43 (patch)
tree1ffa4a03125e019fcaadc328d6069a4d9b537f62 /2021/04/puzzles.py
parenteb34659683a52506f3bbea093dce131c410e955b (diff)
Add day 4 solutions
Diffstat (limited to '2021/04/puzzles.py')
-rw-r--r--2021/04/puzzles.py51
1 files changed, 51 insertions, 0 deletions
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()