From e06e305614f60d2894c1f377a14efa5277c02874 Mon Sep 17 00:00:00 2001 From: Jake Date: Sat, 4 Dec 2021 09:47:24 +0000 Subject: [PATCH] Completed day 4, task 2 --- Day-4/day4-2.py | 86 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 Day-4/day4-2.py diff --git a/Day-4/day4-2.py b/Day-4/day4-2.py new file mode 100644 index 0000000..7aa4083 --- /dev/null +++ b/Day-4/day4-2.py @@ -0,0 +1,86 @@ +#!/usr/bin/python3 + +numbersDrawn = [] +boards = [] +winningBoards = [] + +# Function to check a given board for completed columns +def checkColumns(board): + match = False + for i in range(0,len(board[0])): + for line in board: + if(line[i] == '*'): + match = True + continue + else: + match = False + break + if(match): + return True + else: + continue + return False + +# Function to check a given board for completed rows. +def checkRows(board): + for line in board: + match = False + for num in line: + if(num == '*'): + match = True + continue + else: + match = False + break + if(match): + return True + else: + continue + return False + +# Open the file. Put the numbers to be drawn in one array and the boards in another. +with open('/home/jake/Documents/AoC-2021/Day-4/input.txt', 'r') as inputFile: + numbersDrawn = inputFile.readline().split(',') + boardsFromFile = inputFile.readlines()[1:] + + currentBoard = [] + for line in boardsFromFile: + currentLine = [] + + if(line == '\n'): + boards.append(currentBoard) + currentBoard = [] + continue + + currentLine = line.split() + currentBoard.append(currentLine) + boards.append(currentBoard) + +# Mark off the numbers drawn on the boards. +for i in range(0, len(numbersDrawn)): + for board in boards: + for line in board: + for num in range(len(line)): + if(line[num] == numbersDrawn[i]): + line[num] = '*' + + # Check for a winning board + if(checkRows(board) or checkColumns(board)): + if(board in winningBoards): + continue + else: + winningBoards.append(board) + if(len(winningBoards) < len(boards)): + continue + else: + print("Found final winning board: " + str(board)) + total = 0 + for line in board: + for num in line: + if(num != '*'): + total = total+int(num) + score = total * int(numbersDrawn[i]) + print("Total score of final winning board is: " + str(score)) + exit() + else: + continue \ No newline at end of file