Advertisement
Guest User

aoc-d4-p2

a guest
Dec 5th, 2021
181
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. file = open('data.txt','r')
  2. data = file.readlines()
  3. file.close()
  4.  
  5. boards = [] # empty arr
  6. order = data[0].split(',') # first line of numbers
  7. order[-1] = order[-1].strip('\n') # get right of \n from first line
  8.  
  9. for i in range(len(data)): # for every other line
  10.     # if there is a \n on its own there is a board after it
  11.     if data[i] == '\n': boards.append([data[i+j].split() for j in range(1,6)])
  12.  
  13. def win(board):
  14.     total = 0
  15.     for i in range(5):
  16.         for j in range(5):
  17.             if board[i][j] != None: total += int(board[i][j])
  18.     return total
  19.  
  20. def check_win(board):
  21.     for i in range(5):
  22.         n = 0
  23.         c = 0
  24.         for j in range(5):
  25.             if board[i][j] == None: n += 1
  26.             if board[j][i] == None: c += 1
  27.         if n == 5 or c == 5: return win(board)
  28.  
  29. last = 0
  30. end = False
  31. for n in order:
  32.     for i in range(len(boards)):
  33.         for j in range(5):
  34.             for k in range(5):
  35.                 if boards[i][j][k] == n:
  36.                     boards[i][j][k] = None
  37.         total = check_win(boards[i])
  38.         if total is not None:
  39.             last = total*int(n)
  40.             boards[i] = None
  41.             pass
  42.     while None in boards:
  43.         for p in range(len(boards)):
  44.             if boards[p] == None:
  45.                 boards.pop(p)
  46.                 break
  47. print(last)
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement