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.
63 lines
1.9 KiB
63 lines
1.9 KiB
#!/usr/bin/env python3
|
|
|
|
import sys
|
|
|
|
def printb(board):
|
|
for line in board:
|
|
print(str(line[0]) + ' ' + str(line[1]) + ' ' + str(line[2]) + ' ' + str(line[3]) + ' ' + str(line[4]))
|
|
print("\n")
|
|
|
|
def check_win(board):
|
|
for l in board:
|
|
if l == ['x' ,'x', 'x', 'x', 'x']:
|
|
return True
|
|
hit = 0
|
|
for i in range(len(board)):
|
|
for l in board:
|
|
if l[i] == 'x':
|
|
hit += 1
|
|
if hit == len(board):
|
|
return True
|
|
hit = 0
|
|
return False
|
|
|
|
def play(boards):
|
|
for num in nums:
|
|
boards = [mark_board(board, num) for board in boards]
|
|
for board in boards:
|
|
if check_win(board):
|
|
return num * sum([sum([i for i in l if i != 'x']) for l in board])
|
|
|
|
def play2(boards):
|
|
for num in nums:
|
|
boards = [mark_board(board, num) for board in boards]
|
|
if sum([1 if check_win(board) else 0 for board in boards]) == (len(boards) - 1):
|
|
for i in range(len(boards)):
|
|
if not check_win(boards[i]):
|
|
last = i
|
|
if sum([1 if check_win(board) else 0 for board in boards]) == (len(boards)):
|
|
if check_win(boards[last]):
|
|
board = boards[last]
|
|
return num * sum([sum([i for i in l if i != 'x']) for l in board])
|
|
|
|
|
|
def mark_board(board, num):
|
|
return [['x' if col == num else col for col in li] for li in board]
|
|
|
|
if __name__ == '__main__':
|
|
lines = open(sys.argv[1]).read().split('\n\n') # or split \n\n or sth similar
|
|
|
|
nums = [int(i) for i in lines[0].split(',')]
|
|
|
|
boards = [[[int(n) for n in l.split(' ') if n != ''] for l in li.split('\n')] for li in lines[1:]]
|
|
|
|
boards = [[l for l in b if l != []] for b in boards]
|
|
|
|
# challenge 1
|
|
res1 = str(play(boards))
|
|
print("challenge 1:" + res1 + "\n")
|
|
|
|
# challenge 2
|
|
res2 = str(play2(boards))
|
|
print("challenge 2:" + res2 + "\n")
|
|
|
|
|