"""Replay the invented two-archive charter. Run with Python 3, no packages required."""
import json
from itertools import product

CASH = 6000
ROUTE = 4000
RACKS = 4000
# The paper archivist normally chairs. The film archivist is present and seeks the route.
DIARIES = [
    dict(name="Paper chair in the stacks", chairs=False, volunteers=False, reachable=True, objects=False),
    dict(name="Paper chair away until one", chairs=False, volunteers=False, reachable=False, objects=False),
    dict(name="Paper delivery confirmed", chairs=True, volunteers=True, reachable=True, objects=True),
    dict(name="Both at the table", chairs=True, volunteers=True, reachable=True, objects=False),
]


def resolve(diary, either_calls, reply_right, film_spends):
    convened = diary["chairs"] or either_calls
    heard = convened and diary["reachable"] and (diary["volunteers"] or reply_right)
    assent = heard and not diary["objects"]
    reserved = bool(convened and (film_spends or assent))
    remaining = CASH - ROUTE if reserved else CASH
    exposure = bool(diary["objects"] and remaining < RACKS)
    if not convened:
        hearing = "No meeting before noon."
    elif heard:
        hearing = "Paper objects on the record." if diary["objects"] else "Paper assents before noon."
    elif reply_right:
        hearing = "A reply was requested. Paper could not answer in time."
    else:
        hearing = "No answer from Paper by noon."
    wall = "Route reserved" if reserved else "Wall closed"
    if exposure:
        consequence = "Paper’s confirmed racks are £2,000 short."
    elif diary["objects"]:
        consequence = "Paper can still fund the confirmed racks."
    else:
        consequence = "No paper delivery has been confirmed."
    return dict(wall=wall, hearing=hearing, reserve=reserved, heard=bool(heard),
                cash=remaining, exposure=exposure, consequence=consequence)


def replay():
    outcomes = {}
    for i, diary in enumerate(DIARIES):
        for flags in product((0, 1), repeat=3):
            outcomes[str(i) + "-" + "".join(map(str, flags))] = resolve(diary, *flags)
    assert len(outcomes) == 32
    assert sum(r["reserve"] for r in outcomes.values()) == 17
    assert sum(r["exposure"] for r in outcomes.values()) == 4
    assert all(outcomes["3-"+key]["reserve"] for key in ("000","001","010","011","100","101","110","111"))
    assert [outcomes["0-"+key]["reserve"] for key in ("000","100","110")] == [False,False,True]
    assert outcomes["1-110"]["reserve"] is False and outcomes["1-111"]["reserve"] is True
    assert outcomes["2-110"]["exposure"] is False and outcomes["2-111"]["exposure"] is True
    return dict(diaries=[d["name"] for d in DIARIES], outcomes=outcomes)


if __name__ == "__main__":
    print(json.dumps(replay(), ensure_ascii=False, indent=2))
