Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
sympy can handle permutations solution in 3rd party category for 15-puzzle Solvability by Phil15
from sympy.combinatorics.permutations import Permutation
def fifteen_puzzle(grid: list[list[int]]) -> bool:
line = [n - 1 for row in grid for n in row] # "Permutation" below needs numbers to start at 0.
r, c = divmod(line.index(16 - 1), 4) # position of 16 in the grid.
dist = 3 - r + 3 - c # Distance between 16 at (r, c) and the bottom right corner at (3, 3).
return Permutation(line).parity() == dist % 2
Feb. 23, 2023
Comments: