Part 1

a guest
Dec 14th, 2021
481
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. with open('input.txt', 'r') as f:
2. nums, *boards = f.read().split('\n\n')
3. f.close()
4.
5. nums = list(map(int, nums.split(',')))
6. sp_boards = [[[int(i) for i in x.split()] for x in r.split('\n') if x] for r in boards]
7.
8. def mark(board, n):
9. assert len(board) == 5
10. for i in range(5):
11. board[i] = [x if x != n else 'X' for x in board[i]]
12. return board
13.
14. def left(board):
15. ttl = 0
16. for row in board:
17. for x in row:
18. if x != 'X':
19. ttl += x
20. return ttl
21.
22. def check(nums, sp_boards):
23. for n in nums:
24. for i, board in enumerate(sp_boards):
25. # mark
26. sp_boards[i] = mark(board, n)
27. # check
28. for i, board in enumerate(sp_boards):
29. for row in board:
30. if set(row) == {'X'}:
31. return n * left(board)
32. cols = [[row[i] for row in board] for i in range(5)]
33. for c in cols:
34. if set(c) == {'X'}:
35. return n * left(board)
36.
37. return None
38.
39. print(check(nums, sp_boards))