-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbingo.py
More file actions
75 lines (64 loc) · 2.32 KB
/
bingo.py
File metadata and controls
75 lines (64 loc) · 2.32 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
70
71
72
73
74
75
"""
Advent of Code - tentative pour J4.
Classe Bingo utilisée effectivement lors de la résolution.
Cette classe a été extraite du fichier principal afin de pouvoir tester
une variante.
Daniel Kessler (aka Dalker), le 2021.12.04
"""
from typing import Union, TextIO
import numpy as np
class Bingo:
"""
A bingo board.
Attributes:
- numbers: the playing card, a 5x5 numpy array of ints
- beans: the beans placed on the card, a 5x5 numpy array of bool
"""
def __init__(self, stream: Union[TextIO, None] = None):
if stream is None:
# good practice: always initialize all documented attributes
# (and all public attributes should be documented in docstring)
self.numbers = np.array([])
self.beans = np.array([])
return
numbers = []
for _ in range(5):
numbers.append([int(n) for n in stream.readline().split()])
self.numbers = np.array(numbers)
self.beans = np.zeros((5, 5), dtype=bool)
def __str__(self):
"""Return nice representation for debugging purposes."""
res = ""
for row, numbers in enumerate(self.numbers):
for col, number in enumerate(numbers):
res += f"{number:3d}" + ("*" if self.beans[row, col] else " ")
res += "\n"
return res
def __repr__(self):
"""Return ugly representation for debugging purposes."""
return "Bingo(" + str(self.numbers) + ", " + str(self.beans) + ")"
def copy(self):
"""Return a fresh copy of self."""
clone = Bingo()
clone.numbers = np.copy(self.numbers)
clone.beans = np.copy(self.beans)
return clone
def place(self, called: int):
"""Check if a called number is in board and place bean if it is."""
for row, numbers in enumerate(self.numbers):
for col, number in enumerate(numbers):
if number == called:
self.beans[row, col] = True
def check(self):
"""Check if board won."""
bingo = False
for row in self.beans:
if all(row):
bingo = True
for row in self.beans.transpose():
if all(row):
bingo = True
# if bingo:
# print("bingo!")
# print(self)
return bingo