Last active
November 14, 2021 10:01
-
-
Save ProgramRipper/73fbac229313445dab9d4c79278eda2e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from pprint import pprint | |
| from random import shuffle | |
| from typing import Set, List | |
| class LaunchComponent: | |
| id: str | |
| required: Set[str] | |
| def __init__(self, id, required=None): | |
| if required is None: | |
| required = set() | |
| self.id = id | |
| self.required = required | |
| def __repr__(self): | |
| return f'({self.id},{self.required})' | |
| components = { | |
| LaunchComponent(0), | |
| LaunchComponent(3), | |
| LaunchComponent(1, {0}), | |
| LaunchComponent(2, {0, 1, 3}), | |
| LaunchComponent(4, {2, 3}), | |
| LaunchComponent(5, {4}), | |
| LaunchComponent(6, {5}), | |
| LaunchComponent(7, {6}), | |
| LaunchComponent(8, {7, 1, 2}), | |
| LaunchComponent(9, {5, 6, 7}), | |
| LaunchComponent(10, {2, 3, 6}), | |
| LaunchComponent(11, {0, 1, 4, 5}), | |
| LaunchComponent(12, {0, 1, 4, 5}) | |
| } | |
| def resolve_requirements(components: Set[LaunchComponent]) -> List[Set[LaunchComponent]]: | |
| resolved = set() | |
| result = [] | |
| while components: | |
| layer = set() | |
| for index, component in enumerate(components): | |
| if component.required.issubset(resolved): | |
| layer.add(component) | |
| if layer: | |
| components -= layer | |
| resolved.update(component.id for component in layer) | |
| result.append(layer) | |
| else: | |
| raise Exception | |
| return result | |
| pprint(components) | |
| pprint(resolve_requirements(components)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment