Plan region go-lives around soak times and change windows
Your task
A new version of your support agent goes live region by region. A region that depends on others waits until each of them has soaked: run the new version for a set number of hours while the on-call team is watching. The team only watches during a daily change window, and go-lives happen only inside it too.
Write go_live_hours(regions, start, window) that returns the hour each region goes live.
regions: a list of[name, needs, soak], in any order.needslists the names of the regions it waits for.soakis how many watched hours this region must run the new version before a region that needs it may go live, from 0 to 100,000. Names are unique.start: the first hour any region may go live, 0 or more. Hours count from midnight of day 0, so hour 26 is 02:00 on day 1.window:[open, close], whole hours with0 <= open < close <= 24. Hourhis inside the window whenopen <= h % 24 < close.- Return a dict that maps every region's name to its go-live hour, or to
-1if it can never go live.
Rules:
- A region goes live at the earliest whole hour that is at least
start, inside the window, and no earlier than the end of the soak of every region it needs. - A region that goes live at hour
twith soaksends its soak at the first houresuch thatsof the hourst,t + 1, ...,e - 1are inside the window. A soak of 0 ends att. - A region can never go live if it needs a name that is not in
regions. The same goes for a region that needs itself through a chain of needs, or that needs a region that can never go live. - Regions do not compete: any number of them may go live in the same hour.
Examples
go_live_hours(regions: list[list], start: int, window: list[int]) -> dict[str, int]Example 1
Input
go_live_hours([["us-east", [], 24], ["us-west", ["us-east"], 12], ["eu", ["us-east"], 6]], 9, [9, 17])Output
{"us-east": 9, "us-west": 81, "eu": 81}Example 2
Input
go_live_hours([["eu-2", ["eu"], 0], ["eu", ["us"], 4], ["us", [], 10]], 0, [8, 20])Output
{"eu-2": 34, "eu": 18, "us": 8}Example 3
Input
go_live_hours([["a", ["b"], 1], ["b", ["a"], 1], ["c", ["a"], 1], ["d", ["nowhere"], 1], ["e", [], 1]], 5, [0, 24])Output
{"a": -1, "b": -1, "c": -1, "d": -1, "e": 5}