You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
42 lines
975 B
42 lines
975 B
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)
|
|
|
|
|
|
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
|
|
|
|
for number in numbers:
|
|
boards[boards == number] = -1
|
|
for board in boards:
|
|
if is_solved(board):
|
|
solved_board = board
|
|
solved = True
|
|
last_number = number
|
|
if solved:
|
|
break
|
|
|
|
|
|
score = score_unmarked(solved_board)
|
|
print(last_number * score)
|