mirror of
https://github.com/sstent/Advent2021.git
synced 2026-01-27 01:22:12 +00:00
120 lines
2.8 KiB
Python
120 lines
2.8 KiB
Python
from aocd import get_data
|
|
from aocd import submit
|
|
from pprint import pprint
|
|
from itertools import *
|
|
input_data = get_data(day=4, year=2021)
|
|
|
|
|
|
|
|
test_data = """7,4,9,5,11,17,23,2,0,14,21,24,10,16,13,6,15,25,12,22,18,20,8,19,3,26,1
|
|
|
|
22 13 17 11 0
|
|
8 2 23 4 24
|
|
21 9 14 16 7
|
|
6 10 3 18 5
|
|
1 12 20 15 19
|
|
|
|
3 15 0 2 22
|
|
9 18 13 17 5
|
|
19 8 7 25 23
|
|
20 11 10 24 4
|
|
14 21 16 12 6
|
|
|
|
14 21 17 24 4
|
|
10 16 15 9 19
|
|
18 8 23 26 20
|
|
22 11 13 6 5
|
|
2 0 12 3 7"""
|
|
|
|
|
|
def grouper(n, iterable, fillvalue=None):
|
|
"grouper(3, 'ABCDEFG', 'x') --> ABC DEF Gxx"
|
|
args = [iter(iterable)] * n
|
|
return zip_longest(fillvalue=fillvalue, *args)
|
|
|
|
lines = input_data.split("\n")
|
|
# lines = test_data.split("\n")
|
|
blocks_in = input_data.split("\n\n")
|
|
|
|
number_draw = lines[0].split(",")
|
|
number_draw = [int(item) for item in number_draw]
|
|
number_draw = grouper(5, number_draw)
|
|
print(number_draw)
|
|
|
|
|
|
blocks_in.pop(0)
|
|
|
|
blocks = []
|
|
# print(blocks)
|
|
for index, block in enumerate(blocks_in):
|
|
print("------------")
|
|
print(index)
|
|
rows = block.split("\n")
|
|
new_block = []
|
|
for row in rows:
|
|
new_block.append(row.split())
|
|
blocks.append(new_block)
|
|
|
|
print(blocks)
|
|
|
|
def check_for_winner(blocks):
|
|
winner = []
|
|
for block in blocks:
|
|
if winner == []:
|
|
for row in block:
|
|
if row == ["X","X","X","X","X"]:
|
|
print("ROW WINNER")
|
|
# print(block)
|
|
winner = block
|
|
if winner == []:
|
|
for column, num in enumerate(block[0]):
|
|
block_column = [ block[0][column], block[1][column],block[2][column],block[3][column],block[4][column] ]
|
|
# print(block_column)
|
|
if block_column == ["X","X","X","X","X"]:
|
|
print("COL WINNER")
|
|
winner = block
|
|
break
|
|
|
|
return winner
|
|
|
|
|
|
last_draw = 0
|
|
last_drawblock = 0
|
|
for draw in number_draw:
|
|
print("====================================================================================================")
|
|
print("DrawBlock:" + str(draw))
|
|
last_drawblock = draw[4]
|
|
for number in draw:
|
|
winner = check_for_winner(blocks)
|
|
if winner != []:
|
|
#winner
|
|
print("WINNER")
|
|
print(winner)
|
|
break
|
|
else:
|
|
print("Draw:" + str(number))
|
|
last_draw = number
|
|
for b, block in enumerate(blocks):
|
|
for r, row in enumerate(block):
|
|
for n, num in enumerate(row):
|
|
if num == str(number):
|
|
blocks[b][r][n] = "X"
|
|
# pprint(blocks)
|
|
print("---------------------------------")
|
|
else:
|
|
continue
|
|
break
|
|
|
|
print("FINAL WINNER")
|
|
print(winner)
|
|
sum = 0
|
|
for row in winner:
|
|
for number in row:
|
|
if number != "X":
|
|
sum += int(number)
|
|
|
|
print(sum)
|
|
print(last_draw)
|
|
print(sum*last_draw)
|
|
submit(sum*last_draw)
|
|
# pprint(blocks) |