Simplified solution to problem 4
This commit is contained in:
parent
4d5681a0be
commit
79da60681c
@ -76,7 +76,7 @@
|
|||||||
# To guarantee victory against the giant squid, figure out which board will win
|
# 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?
|
# 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:
|
with open("files/P4.txt", "r") as f:
|
||||||
data = [line for line in f.read().strip().split("\n\n")]
|
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]
|
return [tuple(tup.split()) for tup in lst]
|
||||||
|
|
||||||
|
|
||||||
def transpose_board(lst):
|
def sum_board(board: list[Tuple[str, ...]], lst: list[str]) -> int:
|
||||||
args = iter_to_list(lst)
|
return sum(
|
||||||
return list(zip_longest(*args))
|
int(number) for row in board for number in row if number not in lst
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
def check_num(board, number):
|
def has_won(board: list[Tuple[str, ...]], lst: list[str]) -> bool:
|
||||||
flatten_list = list(chain.from_iterable(board))
|
for row in board:
|
||||||
for num in flatten_list:
|
winner = all(num in lst for num in row)
|
||||||
if int(num.rjust(2, "0")) == int(number):
|
if winner:
|
||||||
return True
|
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
|
return False
|
||||||
|
|
||||||
|
|
||||||
def complete_row(lst, board):
|
boards = [_boards(board) for board in all_boards]
|
||||||
for num in lst:
|
|
||||||
if num not in board:
|
|
||||||
return False
|
|
||||||
return True
|
|
||||||
|
|
||||||
|
|
||||||
def sum_board(board, row, lst):
|
def part_1() -> None:
|
||||||
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():
|
|
||||||
numbers_drawn = []
|
numbers_drawn = []
|
||||||
for number in numbers:
|
for number in numbers:
|
||||||
numbers_drawn.append(number)
|
numbers_drawn.append(number)
|
||||||
if len(numbers_drawn) < 5:
|
if len(numbers_drawn) < 5:
|
||||||
continue
|
continue
|
||||||
for b, bt in zip(boards, boards_transpose):
|
for board in boards:
|
||||||
for row_b, row_bt in zip(b, bt):
|
if has_won(board, numbers_drawn):
|
||||||
if complete_row(row_b, numbers_drawn):
|
return print(sum_board(board, numbers_drawn) * int(number))
|
||||||
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])
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
# --- Part Two ---
|
# --- Part Two ---
|
||||||
@ -158,45 +141,10 @@ def part_1():
|
|||||||
# Figure out which board will win last. Once it wins, what would its final
|
# Figure out which board will win last. Once it wins, what would its final
|
||||||
# score be?
|
# score be?
|
||||||
|
|
||||||
|
# from collections import OrderedDict
|
||||||
def part_2():
|
# from copy import copy
|
||||||
(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]
|
|
||||||
# # )
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
part_1()
|
part_1()
|
||||||
part_2()
|
# part_2()
|
||||||
|
Loading…
Reference in New Issue
Block a user