Post Reply 
Fifteen puzzle solvability, Numworks Python
04-08-2020, 10:41 AM (This post was last modified: 04-28-2020 02:24 PM by Don Shepherd.)
Post: #1
Fifteen puzzle solvability, Numworks Python
I just received a Numworks calculator and implemented a Python program to determine if a given configuration of the fifteen puzzle is solvable. Very interesting and beautiful calculator. I contacted Numworks with a question and the president of the company replied; excellent support.

List a contains the initial configuration of the 15 puzzle pieces (that you want to test) and the empty (0) cell. The 99 at the beginning is there so I could more easily convert an existing HP-17 solver equation, since the Numworks list starts at index 0.

The program returns the sum of the number of inversions and row number (1-4) of the empty cell. If this number is even, the puzzle configuration is solvable.

Code:

def fifteen():
  a = [99,11,0,3,8,1,6,15,10,7,14,2,13,4,12,9,5]
  b=0  
  
  for i in range (1,17):
    
    if a[i]==0:
      b = int((i+3)/4)
      
  for i in range (1,16):
    for j in range (i+1,17):
      if a[j] != 0 and a[j]<a[i]:
        b=b+1
        
  return b
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
Fifteen puzzle solvability, Numworks Python - Don Shepherd - 04-08-2020 10:41 AM



User(s) browsing this thread: 2 Guest(s)