def total_distance(solution, distanceMap):
    """
    Calculate the total distance among a solution of cities.
    Uses a dictionary to get lookup the each pairwise distance.
    :param solution: A list of cities in random order.
    :distanceMap: The dictionary lookup tool.
    :return: The total distance between all the cities.
    """
    totalDistance = 0
    for i, city in enumerate(solution[:-1]): # Stop at the second to last city.
        cityA = city
        cityB = solution[i + 1]
        buildKey = (cityA, cityB)
        totalDistance += distanceMap[buildKey]
    # Build the key to return home
    cityA = solution[-1]
    cityB = solution[0]
    goHome = (cityA, cityB)
    totalDistance += distanceMap[goHome]
    return totalDistance