-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaoc_04.py
More file actions
69 lines (57 loc) · 2.16 KB
/
aoc_04.py
File metadata and controls
69 lines (57 loc) · 2.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
"""
Advent of Code - tentative pour J4.
Daniel Kessler (aka Dalker), le 2021.12.04
"""
from bingo import Bingo
Data = tuple[list[int], list[Bingo]] # typing for today's data
def play_bingo(boards: list[Bingo], numbers: list[int],
lose=False) -> tuple[int, Bingo]:
"""Play bingo with given boards and numbers until someone wins."""
for number in numbers:
for board in boards:
already_won = board.check()
board.place(number)
if not already_won and board.check():
last_winner = number, board.copy()
if not lose:
return number, board
return last_winner
def easy(data: Data) -> int:
"""Easy problem of the day."""
numbers, boards = data
winning_number, winner = play_bingo(boards, numbers)
score = 0
for row, numbers in enumerate(winner.numbers):
for col, number in enumerate(numbers):
if not winner.beans[row, col]:
score += number
return winning_number * score
def hard(data: Data) -> int:
"""Hard problem of the day. Actually easier than easy this time!"""
numbers, boards = data
winning_number, winner = play_bingo(boards, numbers, lose=True)
score = 0
for row, numbers in enumerate(winner.numbers):
for col, number in enumerate(numbers):
if not winner.beans[row, col]:
score += number
return winning_number * score
def get_data(fname: str) -> Data:
"""Read the day's input file and return contents as a list of ints."""
numbers = []
boards = []
try:
with open(f"{fname}.txt") as datafile:
numbers = [int(num) for num in datafile.readline().split(",")]
while datafile.readline():
boards.append(Bingo(datafile))
except FileNotFoundError:
print("Day's data file not found. Using Hint Data instead.")
return numbers, boards
if __name__ == "__main__":
hintdata = get_data("hintdata04")
data = get_data("input04")
print("check hint 1:", easy(hintdata))
print("check hint 2:", hard(hintdata))
print(" solution 1:", easy(data))
print(" solution 2:", hard(data))