added day 04
This commit is contained in:
58
04/solve01.py
Normal file
58
04/solve01.py
Normal file
@ -0,0 +1,58 @@
|
||||
#!/usr/bin/env python
|
||||
|
||||
|
||||
def check_number(number, bingo):
|
||||
for n, line in enumerate(bingo):
|
||||
try:
|
||||
x = line.index(number)
|
||||
line[x] = -1
|
||||
|
||||
# check row
|
||||
t = [i for i in line if i >= 0]
|
||||
if len(t) == 0:
|
||||
return bingo
|
||||
# check col
|
||||
t = [j[x] for j in bingo if j[x] >= 0]
|
||||
if len(t) == 0:
|
||||
return bingo
|
||||
except ValueError:
|
||||
continue
|
||||
return None
|
||||
|
||||
def bingo_number(bingo):
|
||||
result = 0
|
||||
for l in bingo:
|
||||
tmp = sum([i for i in l if i >= 0])
|
||||
result += tmp
|
||||
return result
|
||||
|
||||
|
||||
numbers = None
|
||||
bingo = []
|
||||
with open("input01.txt", "r") as f:
|
||||
line = f.readline()
|
||||
numbers = [int(i) for i in line.split(',')]
|
||||
|
||||
tmp = []
|
||||
for line in f:
|
||||
line = line.strip()
|
||||
if len(line) == 0:
|
||||
if len(tmp) == 0:
|
||||
continue
|
||||
bingo.append(tmp)
|
||||
tmp = []
|
||||
continue
|
||||
tmp.append([int(i) for i in line.split(" ") if len(i)>0])
|
||||
if len(tmp):
|
||||
bingo.append(tmp)
|
||||
|
||||
winner = None
|
||||
for n in numbers:
|
||||
for b in bingo:
|
||||
winner = check_number(n, b)
|
||||
if winner is not None:
|
||||
break
|
||||
if winner is not None:
|
||||
score = bingo_number(winner)
|
||||
print(score*n)
|
||||
break
|
||||
Reference in New Issue
Block a user