Skip to content

Instantly share code, notes, and snippets.

@loudambiance
Created December 16, 2021 21:47
Show Gist options
  • Save loudambiance/286f1469d45ed3c0535ee8ddad0d56d9 to your computer and use it in GitHub Desktop.
Save loudambiance/286f1469d45ed3c0535ee8ddad0d56d9 to your computer and use it in GitHub Desktop.
bingoboards = []
bingonumbers = []
with open('task4a.txt') as f:
bingonumbers = f.readline().strip('\n\r ').split(',')
f.readline()
while(True):
bingoboard = []
for x in range(5):
line = []
for square in f.readline().strip('\n\r').split(' '):
if square == '':
continue
line.append({'square':square, 'marked':False})
bingoboard.append(line)
bingoboards.append(bingoboard)
if not f.readline():
break
def markSquare(boards, number):
for board in boards:
for line in board:
for square in line:
if square['square'] == number:
square['marked'] = True
def checkWin(boards):
for board in boards:
#row check
for line in board:
if checkListWin(line):
return calcWinValue(board)
#column check
for x in range(5):
tmp0 = zip(*board)
tmp = next(tmp0)
if checkListWin(list(tmp)):
return calcWinValue(board)
return None
def checkListWin(list):
win = True
for square in list:
if square['marked'] == False:
win = False
return win
def calcWinValue(board):
squares = [item for x in board for item in x]
score = 0
for square in squares:
if square['marked'] == False:
score+=int(square['square'])
return score
for nextnumber in bingonumbers:
markSquare(bingoboards,nextnumber)
winval = checkWin(bingoboards)
if winval is not None:
print(checkWin(bingoboards)*int(nextnumber))
break
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment