from pathlib import Path


def get_cost(res_dir, exp):
    """Extract program cost from an experiment."""
    err_file = res_dir / Path(exp["stderr"])
    with err_file.open("r") as f:
        lines = f.readlines()
        if "Cost:" in lines[-1]:
            return float(lines[-1].split(" ")[1].strip())
        else:
            return -1.0