4.py 892 B

12345678910111213141516171819202122232425262728
  1. from util import get_input, transpose
  2. from more_itertools import grouper, flatten
  3. input = get_input("4.input")
  4. numbers = [int(a) for a in input[0].split(",")]
  5. boards = [[[int(a) for a in row.split()] for row in c[1:]] for c in grouper(input[1:], 6, [])]
  6. def check_board(numbers, board):
  7. for row in board + transpose(board):
  8. if len([a for a in row if a in numbers]) == 5:
  9. return True
  10. return False
  11. def winners(numbers, boards):
  12. all_winners = []
  13. for i in range(len(numbers)):
  14. winners = [b for b in boards if check_board(numbers[:1+i], b)]
  15. for board in winners:
  16. boards.remove(board)
  17. unmarked = [a for a in flatten(board) if a not in numbers[:1+i]]
  18. all_winners.append(sum(unmarked) * numbers[i])
  19. return all_winners
  20. all_winners = winners(numbers, boards)
  21. print(all_winners[0])
  22. print(all_winners[-1])