AdventOfCode/2021/day4/day4_part1.py

76 lines
1.6 KiB
Python

import numpy as np
import re, sys
filename = open(sys.argv[1],"r")
x = []
xr = filename.readlines()[0].strip()
#print(filename.readline().rstrip())
#for i in filename.readlines():
# print (i.strip())
llist=[]
bingo_numbers=[]
with open(sys.argv[1]) as f:
# x = [ j for j in x.split() ]
#lines = f.readlines()
#lines = [line.rstrip() for line in lines]
for line in f:
if re.search("^[0-9]+,",line.rstrip()):
bingo_numbers = [int(i) for i in line.rstrip().split(',')]
else:
for x in line.rstrip().split(' '):
if x != '':
llist.append(x)
plates = len(llist)/25
numbers=np.zeros((int(plates),5,5))
bingo=np.zeros((int(plates),5,5))
for l in range(0,int(plates)):
for i in range(0,5):
for j in range(0,5):
numbers[l][i][j]=llist[0]
llist.pop(0)
#print()
#print()
print("----")
print(bingo_numbers)
force_break = False
called_numbers = []
winner = []
for k in range(0,len(bingo_numbers)):
for l in range(0,int(plates)):
for i in range(0,5):
for j in range(0,5):
if numbers[l][i][j] == bingo_numbers[0]:
bingo[l][i][j] = 1
if sum(bingo[l][i]) == 5:
winner = numbers[l]
force_break = True
break
called_numbers.append(bingo_numbers[0])
bingo_numbers.pop(0)
if force_break:
break
print(called_numbers)
print(winner)
winner_sum = 0
for i in winner:
for j in i:
if int(j) not in called_numbers:
winner_sum += int(j)
print(winner_sum*called_numbers[-1])