// A delivery network and a project plan: the fastest route between depots, the order to do tasks
// in, and the circular dependency that blocks it.
//
//   quillo run graph.qwe      then open http://localhost:8080/route?from=Depot&to=Harbour

use "graph.qwer"

let roads = [["Depot", "Market", 7], ["Depot", "Mill", 9], ["Depot", "Farm", 14], ["Market", "Mill", 10], ["Market", "Station", 15],
             ["Mill", "Station", 11], ["Mill", "Farm", 2], ["Farm", "Harbour", 9], ["Station", "Harbour", 6], ["Island", "Lighthouse", 3]]

route GET /route ? { from: str, to: str } {
    let p = graph_shortest_path(roads, from, to, false)?
    if p == null { reply 404 { error: `no road from ${from} to ${to}` } }
    { route: join(p.path, " → "), minutes: p.distance }
}

route GET /islands = graph_components(roads)? | map((c) => len(c))

let tasks = [["design", "build"], ["build", "test"], ["design", "docs"], ["test", "release"], ["docs", "release"], ["setup", "build"]]

route GET /plan = {
    order: graph_toposort(tasks)?.order,
    after_build: graph_reachable(tasks, "build")?,
    blocked: graph_toposort(push(tasks, ["release", "design"]))?.cycle,
}
