aboutsummaryrefslogtreecommitdiff
path: root/challenge-285/sgreen/python/ch-1.py
blob: f0cbf0030e824d0e507c7a09644871ee06e7b221 (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
#!/usr/bin/env python3

import sys


def no_connection(routes: list) -> str:
    # Calculate a list of origins (first value) and destinations (second value)
    origins = [v[0] for v in routes]
    destinations = [v[1] for v in routes]

    # Find route destinations that aren't also an origin
    dead_ends = [d for d in destinations if d not in origins]

    if len(dead_ends) > 1:
        raise ValueError(
            'There are multiple routes with no outgoing connection')

    if len(dead_ends) == 0:
        raise ValueError('All routes have an outgoing connection')

    return dead_ends[0]


def main():
    # Convert input into pairs. The first value is the start of route.
    # The second value is the destination
    routes = []
    for i in range(1, len(sys.argv), 2):
        routes.append([sys.argv[i], sys.argv[i+1]])
    result = no_connection(routes)
    print(result)


if __name__ == '__main__':
    main()