-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpuzzle_input.py
66 lines (54 loc) · 1.55 KB
/
puzzle_input.py
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
#!/usr/bin/env python3
from contextlib import nullcontext
from dataclasses import dataclass, field, fields
from pathlib import Path
import sys
@dataclass
class PuzzleInput:
path: Path
solution: int
lines: list[str] = field(repr=False)
base: Path = None
SEP = '#'
@classmethod
def parse(
cls: type['PuzzleInput'], path: Path | str, seen: list[Path] = None
) -> 'PuzzleInput':
def fatal(exc: Exception):
print(
'\n'.join(
['PuzzleInput stack:']
+ [f' {path}' for path in seen]
+ ['']
),
file=sys.stderr,
)
raise exc
path = Path(path).resolve()
seen = seen or []
seen.append(path)
if path in seen[:-1]:
fatal(RuntimeError('PuzzleInput import cycle'))
(meta, text, in_text) = ([], [], False)
try:
with open(path) as file:
for line in file:
if (line := line.strip()).startswith(cls.SEP):
in_text = True
else:
(text if in_text else meta).append(line)
except Exception as e:
fatal(e)
metadata = {}
data_fields = {field.name: field for field in fields(cls)}
for line in meta:
if not line:
continue
(key, val) = map(str.strip, line.split('=', 1))
metadata[key] = data_fields[key].type(val)
if base := metadata.get('base'):
metadata['base'] = base if base.is_absolute() else path.parent / base
text = cls.parse(metadata['base'], seen).lines
while not text[-1]:
del text[-1]
return cls(path=path, lines=text, **metadata)