def to_grid(tiles):
    """ Organizes a list of tiles into a matrix that can be iterated over. """
    tiles = sorted(tiles, key=lambda elem: elem.x)
    pivot = 1
    for idx, element in enumerate(tiles):
        if idx == 0:
            continue
        prior_element = tiles[idx-1]
        if element.x != prior_element.x:
            pivot = idx
            break

    return [tiles[i:i+pivot] for i in range(0, len(tiles), pivot)]