import numpy as np numbers_file = "inputs/day4_1.input" numbers = open(numbers_file, "rb").read().decode("utf-8").split(",") numbers = [int(x) for x in numbers] boards_file = "inputs/day4_2.input" boards = open(boards_file, "rb").readlines() boards = [x.decode().replace("\n", "") for x in boards] boards = [x.split() for x in boards if len(x) > 0] boards = np.array(boards, dtype=int).reshape(-1, 5, 5) n_boards = boards.shape[0] def is_solved(board): if -5 in np.sum(board, axis=0): return True if -5 in np.sum(board, axis=1): return True else: return False def score_unmarked(board): board = np.maximum(0, board) return np.sum(board) solved = False solved_boards = 0 current_number = 0 solved_boards = [] last_board = 0 for number in numbers: boards[boards == number] = -1 for i in range(boards.shape[0]): if is_solved(boards[i]) and (i not in solved_boards): solved_boards.append(i) if len(solved_boards) == n_boards: last_board = i last_number = number if len(solved_boards) == n_boards: break print(score_unmarked(boards[last_board]) * last_number) # score = score_unmarked(current_board)