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
from typing import Dict, List, NamedTuple, Tuple
from cs11puzzles.pzpr_helpers import conns_to_str_grid
class CountryInstance(NamedTuple):
areas: List[List[int]]
area_nums: Dict[int, int]
def load_puzzle_and_solution(fname: str) -> Tuple[CountryInstance, List[List[str]]]:
with open(fname) as f:
assert f.readline().strip() == "pzprv3.1"
assert f.readline().strip() == "country"
height = int(f.readline())
width = int(f.readline())
f.readline() # empty line for some reason
_ = int(f.readline()) # n_regions
areas = [
[int(n) for n in f.readline().strip().split(" ")] for _ in range(height)
]
numbers = [
[int(n) if n != "." else 0 for n in f.readline().strip().split(" ")]
for _ in range(height)
]
area_nums = {}
# pzpr displays number in leftmost, topmost cell
for x in range(width):
for y in range(height):
if areas[y][x] not in area_nums:
area_nums[areas[y][x]] = numbers[y][x]
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 CountryInstance(areas, area_nums), conns_to_str_grid(
conn_right, conn_down
)
def load_puzzle(fname: str):
puzzle, _ = load_puzzle_and_solution(fname)
return puzzle