aboutsummaryrefslogtreecommitdiff
path: root/2021/04/puzzles.py
blob: 46dfa05da1b0a8dcc0967e5f37e210e59e5396a4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#!/usr/bin/env python3

from libaoc import flatten, map2d, matrix

board = matrix[int]
last: board = []


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:
		draws = list(map(int, f.readline().split(",")))
		lines = f.readlines()
		for i, line in enumerate(lines):
			if line == "\n":
				boards.append([list(map(int, lines[i + j].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 board in boards:
			for i, row in enumerate(board):
				board[i] = [-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(flatten(map2d(lambda n: 0 if n == -1 else n, last))))


if __name__ == "__main__":
	main()