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
from typing import NamedTuple, List, Tuple
from cs11puzzles.pzpr_helpers import conns_to_str_grid
WHITE = 1
BLACK = 2
WHITE_SYM = ""
BLACK_SYM = ""
class MasyuInstance(NamedTuple):
pearls: List[List[int]]
def load_puzzle_and_solution(fname: str) -> Tuple[MasyuInstance, List[List[str]]]:
with open(fname) as f:
assert f.readline().strip() == "pzprv3"
assert f.readline().strip() == "mashu"
height = int(f.readline())
_ = int(f.readline()) # width
pearls = [
[0 if i == "." else int(i) for i in f.readline().strip().split(" ")]
for _ in range(height)
]
conn_right = [
[int(n) == 1 for n in f.readline().strip().split(" ")]
for _ in range(height)
]
conn_down = [
[int(n) == 1 for n in f.readline().strip().split(" ")]
for _ in range(height - 1)
]
return MasyuInstance(pearls), conns_to_str_grid(conn_right, conn_down)
def load_puzzle(fname: str):
puzzle, _ = load_puzzle_and_solution(fname)
return puzzle