Simplified solution to problem 4

This commit is contained in:
David Doblas Jiménez 2022-02-05 14:31:25 +01:00
parent 4d5681a0be
commit 79da60681c

View File

@ -76,7 +76,7 @@
# To guarantee victory against the giant squid, figure out which board will win
# first. What will your final score be if you choose that board?
from itertools import chain, zip_longest
from typing import Tuple
with open("files/P4.txt", "r") as f:
data = [line for line in f.read().strip().split("\n\n")]
@ -85,59 +85,42 @@ with open("files/P4.txt", "r") as f:
]
def iter_to_list(lst):
def _boards(lst: list[str]) -> list[Tuple[str, ...]]:
return [tuple(tup.split()) for tup in lst]
def transpose_board(lst):
args = iter_to_list(lst)
return list(zip_longest(*args))
def sum_board(board: list[Tuple[str, ...]], lst: list[str]) -> int:
return sum(
int(number) for row in board for number in row if number not in lst
)
def check_num(board, number):
flatten_list = list(chain.from_iterable(board))
for num in flatten_list:
if int(num.rjust(2, "0")) == int(number):
return True
def has_won(board: list[Tuple[str, ...]], lst: list[str]) -> bool:
for row in board:
winner = all(num in lst for num in row)
if winner:
return winner
for col in range(5):
winner = all(num in lst for num in [row[col] for row in board])
if winner:
return winner
return False
def complete_row(lst, board):
for num in lst:
if num not in board:
return False
return True
boards = [_boards(board) for board in all_boards]
def sum_board(board, row, lst):
flatten_list = set(chain.from_iterable(board))
row_set = set(row)
remain = flatten_list - row_set
return sum(int(number) for number in list(remain) if number not in lst)
boards = [iter_to_list(board) for board in all_boards]
boards_transpose = [transpose_board(board) for board in all_boards]
def part_1():
def part_1() -> None:
numbers_drawn = []
for number in numbers:
numbers_drawn.append(number)
if len(numbers_drawn) < 5:
continue
for b, bt in zip(boards, boards_transpose):
for row_b, row_bt in zip(b, bt):
if complete_row(row_b, numbers_drawn):
return print(
sum_board(b, row_b, numbers_drawn)
* int(numbers_drawn[-1])
)
if complete_row(row_bt, numbers_drawn):
return print(
sum_board(bt, row_bt, numbers_drawn)
* int(numbers_drawn[-1])
)
for board in boards:
if has_won(board, numbers_drawn):
return print(sum_board(board, numbers_drawn) * int(number))
# --- Part Two ---
@ -158,45 +141,10 @@ def part_1():
# Figure out which board will win last. Once it wins, what would its final
# score be?
def part_2():
(numbers_drawn,) = []
for number in numbers:
numbers_drawn.append(number)
if len(numbers_drawn) < 5:
continue
for b, bt in zip(boards, boards_transpose):
for row_b, row_bt in zip(b, bt):
if complete_row(row_b, numbers_drawn):
# wining_boards.append(
# (b, row_b, numbers_drawn[-1], numbers_drawn)
# )
total = print(
sum_board(b, row_b, numbers_drawn)
* int(numbers_drawn[-1])
)
if total is not None and total > 0:
print(total)
if complete_row(row_bt, numbers_drawn):
# wining_boards.append(
# (bt, row_bt, numbers_drawn[-1], numbers_drawn)
# )
total = print(
sum_board(bt, row_bt, numbers_drawn)
* int(numbers_drawn[-1])
)
if total is not None and total > 0:
print(total)
# b, r, n, nd = wining_boards[0]
# # print(b, r, n)
# print(sum_board(b, r, nd) * int(n))
# # last_board, row = wining_boards[-1]
# # print(last_board, row, numbers_drawn[-1])
# # return print(sum_board(last_board, row, numbers_drawn)) * int(
# # numbers_drawn[-1]
# # )
# from collections import OrderedDict
# from copy import copy
if __name__ == "__main__":
part_1()
part_2()
# part_2()