blob: 51e19e556e0bba0ef93b5a194eb0bc7d96d384b0 (
plain)
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
|
# SPDX-FileCopyrightText: Peter Pentchev <roam@ringlet.net>
# SPDX-License-Identifier: BSD-2-Clause
"""Solve the first task in Perl weekly challenge 285, "No Connection"."""
from __future__ import annotations
import dataclasses
import typing
from . import defs
if typing.TYPE_CHECKING:
from typing import Final
@dataclasses.dataclass
class NoLeafError(defs.NoSolutionError):
"""No leaf destinations at all."""
def __str__(self) -> str:
"""Provide a human-readable error message."""
return "No leaf destinations could be found"
@dataclasses.dataclass
class MultipleLeavesError(defs.NoSolutionError):
"""More than one leaf destination."""
multiple: list[str]
"""The leaf destinations found."""
def __str__(self) -> str:
"""Provide a human-readable error message."""
return f"Multiple leaf destinations found: {self.multiple!r}"
def solve_no_connection(routes: list[tuple[str, str]]) -> str:
"""Solve the first task, "No Connection."""
loc_from: Final = {item[0] for item in routes}
loc_to: Final = {item[1] for item in routes}
match sorted(loc_to - loc_from):
case []:
raise NoLeafError
case [single]:
return single
case multiple:
raise MultipleLeavesError(multiple)
|