-
Notifications
You must be signed in to change notification settings - Fork 0
/
part_a.py
executable file
·164 lines (141 loc) · 5.28 KB
/
part_a.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#!/usr/bin/env python3
import re
from dataclasses import dataclass
from typing import Dict, List, Optional
import utils
class Challenge(utils.BaseChallenge):
def solve(self, _input, debug=False):
"""
>>> Challenge().default_solve()
'airlri'
"""
return Node.from_nodes_text(_input).name
@dataclass
class Node:
name: str
weight: int
children: List['Node']
parent: Optional['Node']
re_node = re.compile(r'^(\w+) \((\d+)\)(?: -> (\w+(?:, \w+)*))?$')
@classmethod
def from_nodes_text(cls, nodes_text):
"""
>>> Node.from_nodes_text(
... "pbga (66)\\n"
... "xhth (57)\\n"
... "ebii (61)\\n"
... "havc (66)\\n"
... "ktlj (57)\\n"
... "fwft (72) -> ktlj, cntj, xhth\\n"
... "qoyq (66)\\n"
... "padx (45) -> pbga, havc, qoyq\\n"
... "tknk (41) -> ugml, padx, fwft\\n"
... "jptl (61)\\n"
... "ugml (68) -> gyxo, ebii, jptl\\n"
... "gyxo (61)\\n"
... "cntj (57)\\n"
... )
Node(name='tknk', weight=41, children=['ugml', 'padx', 'fwft'],
parent=None)
"""
nodes_info_by_name = cls.parse_infos(nodes_text)
if not nodes_info_by_name:
return None
nodes_by_name: Dict[str, Node] = {
name: cls(name, weight, [], None)
for name, weight, _ in nodes_info_by_name.values()
}
for node in nodes_by_name.values():
node.children = [
nodes_by_name[sub_node_name]
for sub_node_name in nodes_info_by_name[node.name][2]
]
for child in node.children:
if child.parent:
raise Exception(
f"Node '{child.name}' had multiple parents: "
f"'{child.parent.name}' and '{node.name}'")
child.parent = node
a_node = next(iter(nodes_by_name.values()))
a_root = a_node.find_root()
descendants = a_root.get_descendants()
if descendants != set(nodes_by_name.values()):
extra_descendants = descendants - set(nodes_by_name.values())
if extra_descendants:
raise Exception(f"Got more descendants than nodes")
else:
raise Exception(f"Graph is a forest, not a tree")
return a_root
@classmethod
def parse_infos(cls, nodes_text):
nodes_info_by_name = {}
for line in map(str.strip, nodes_text.strip().splitlines()):
name, weight, children_names = cls.parse_node(line)
if name in nodes_info_by_name:
raise Exception(f"Got duplicate node {name}")
nodes_info_by_name[name] = (name, weight, children_names)
cls.check_referenced_nodes(nodes_info_by_name)
return nodes_info_by_name
@classmethod
def check_referenced_nodes(cls, nodes_info_by_name):
referenced_children_names = sum((
children_names
for _, _, children_names in nodes_info_by_name.values()
), [])
if len(referenced_children_names) \
!= len(set(referenced_children_names)):
raise Exception(f"Some nodes were referenced more than once")
unknown_referenced_children_names = \
set(referenced_children_names) - set(nodes_info_by_name)
if unknown_referenced_children_names:
raise Exception(
f"Got {len(unknown_referenced_children_names)} unknown node "
f"names: "
f"{', '.join(sorted(unknown_referenced_children_names))}")
@classmethod
def parse_node(cls, node_text):
"""
>>> Node.parse_node('vpryah (310) -> iedlpkf, epeain')
('vpryah', 310, ['iedlpkf', 'epeain'])
>>> Node.parse_node('vpryah (310) -> iedlpkf')
('vpryah', 310, ['iedlpkf'])
>>> Node.parse_node('vpryah (310)')
('vpryah', 310, [])
"""
name, weight_str, children_names_str = \
cls.re_node.match(node_text).groups()
weight = int(weight_str)
if children_names_str:
children_names = children_names_str.split(', ')
else:
children_names = []
if len(children_names) != len(set(children_names)):
raise Exception(f"Parsed duplicate sub-nodes in '{node_text}'")
return name, weight, children_names
def __repr__(self):
return (
f"{type(self).__name__}("
f"name={repr(self.name)}, "
f"weight={repr(self.weight)}, "
f"children={repr([child.name for child in self.children])}, "
f"parent={repr(self.parent.name if self.parent else None)}"
f")"
)
def __hash__(self):
return hash(self.name)
def find_root(self):
root = self
while root.parent:
root = root.parent
return root
def get_descendants(self):
descendants = {self}
stack = [self]
while stack:
node = stack.pop(0)
new_descendants = set(node.children) - descendants
stack.extend(new_descendants)
descendants.update(new_descendants)
return descendants
Challenge.main()
challenge = Challenge()