Untitled

mail@pastecode.io avatar
unknown
python
2 years ago
1.9 kB
3
Indexable
Never
with open('04.in', 'r') as file:
    data = (line.rstrip() for line in file)
    data = list(line for line in data if line)

numbersraw = data.pop(0)
numbers = numbersraw.split(",")

boards = []

while True:
    board = []
    for y in range(5):
        board.append(data.pop(0).split())
    boards.append(board)
    if not data:
        break

bingoboards = []
lastnumber = ""

def checkbingo(board, number, bingoboards):
    global lastnumber
    for i in range(5):   # check rows
        bingo = True
        for j in range(5):
            if board[i][j] != 'x':
                bingo = False
        if bingo:
            bingoboards.append(board)
            lastnumber = number
            # calcvalue(board, number) # uncomment for part one solution

    
    for i in range(5):   # check columns
        bingo = True
        for j in range(5):
            if board[j][i] != 'x':
                bingo = False
        if bingo:
            bingoboards.append(board)
            lastnumber = number
            # calcvalue(board, number) # uncomment for part one solution

    return bingoboards

def calcvalue(board, number):
    print(board, number)
    total = 0
    for row in board:
        for item in row:
            if item.isdigit():
                total += int(item)
    total *= int(number)
    print(total)
    quit()



for number in numbers:
    print(number)
    for b, board in enumerate(boards):
        for y, row in enumerate(board):
            for x, item in enumerate(row):
                if item == str(number):
                    boards[b][y][x] = 'x'
                    bingolen = len(bingoboards)
                    bingoboards = checkbingo(board, number, bingoboards)
                    if len(bingoboards) > bingolen:
                        del boards[b]

calcvalue(bingoboards[-1], lastnumber)