def solvable(tiles, size):
    """
    Check whether a sliding puzzle is solvable. For 3x3 or any odd dimension square puzzle,
    Checks the number of "inversions". If this is even then the puzzle configuration is solvable.
    An inversion is when two tiles are in the wrong order.
    For example, the sequence 1, 3, 4, 7, 0, 2, 5, 8, 6 has nine inversions:
    The empty tile (8) is ignored.
    Even dimension square puzzles are a bit more complicated. The puzzle is solvable if
    the blank is on an even row counting from the bottom (second-last, fourth-last, etc.)
    and number of inversions is odd OR
    the blank is on an odd row counting from the bottom and number of inversions is even.
    """
    count = 0
    if size == 3:
        for i in range(9):
            if tiles[i] != 8:
                for j in range(i+1, 9):
                    if tiles[i]  > tiles[j]:
                        count += 1
        return count % 2 == 0
    else:
        for i in range(16):
            if tiles[i] != 15:
                for j in range(i+1, 16):
                    if tiles[i]  > tiles[j]:
                        count += 1
        if int(tiles.index(15) / 4) in [0, 2]: # even rows from bottom
            return count % 2 != 0
        else:
            return count % 2 == 0