import re


def load_instance(instance_name, field):
    """ load instance file

    Args:
    - instance_name (string): file name
    - field (dictionary): see load_map

    Returns:
    - dictionary
      - agents (int)
      - starts (list of node indexes)
      - goals (list of node indexes)
    """

    r_loc = re.compile(r"(\d+),(\d+),(\d+),(\d+)")

    agents = 0
    starts = []
    goals = []

    with open(instance_name) as f:
        for row in f:
            m = re.match(r_loc, row)
            if m:
                s = field["width"] * int(m.group(2)) + int(m.group(1))  # start
                g = field["width"] * int(m.group(4)) + int(m.group(3))   # goal
                if field["body"][s] and field["body"][g]:
                    starts.append(s)
                    goals.append(g)

    return {
        "agents": len(starts),
        "starts": starts,
        "goals": goals
    }