-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
119 lines (113 loc) · 3.71 KB
/
main.py
File metadata and controls
119 lines (113 loc) · 3.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
"""This program is able to solve Tower of Hanoi problem for upto 5 towers and 5 disks. For greater than that recursion depth is reached. Need to work on it in upcoming days. """
# towers = [[1,-1,-1],[3,-1,-1],[2,-1,-1]]
# towers = [[1,-1,-1],[3,-1,-1],[3,1,2]]
T_CNT = 5
temp = [[-1]*T_CNT for _ in range(T_CNT-1)]
towers = [[x+1 for x in reversed(range(T_CNT))]]
towers.extend(temp)
print(towers)
hist = []
def isEmpty(l):
return True if l == [-1,-1,-1] else False
def otherTowers(i):
ot = [ x for x in range(T_CNT) if x!=i ]
return ot
def nonEmptyTower(i):
if not isEmpty(towers[i]):
return i
return False
def tTop(l):
for i in reversed(l):
if i != -1:
return i
return 999
def tTopInd(l):
for i, v in enumerate( reversed(l)):
if v != -1:
return len(l) -1 - i
return -1
def solved():
if towers[T_CNT-1] == list(reversed([x+1 for x in range(T_CNT)])):
return True
return False
def getMoves(cur_st):
moves = []
for i in range(T_CNT):
for j in range(T_CNT):
if not i == j:
if (not isEmpty(towers[i]) and isEmpty(towers[j])) or (tTop(towers[i])< tTop(towers[j]) ) :
print("tTops: ",tTop(towers[i]), tTop(towers[j]), "twrs", i,j)
moves.append([i,j])
print("moves", moves)
return moves
def compare_lists(list1, list2):
# Check if the lengths of the lists are different
if len(list1) != len(list2):
# print("list len unequal")
return False
# Iterate over the elements of the lists
for i in range(len(list1)):
# If the elements are lists, recursively compare them
# print("Instances: ", isinstance(list1[i], list) , isinstance(list2[i], list))
if isinstance(list1[i], list) and isinstance(list2[i], list):
if not compare_lists(list1[i], list2[i]):
return False
# If the elements are not lists, compare them directly
else:
if list1[i] != list2[i]:
return False
# All elements are equal
return True
def print_integer_with_width(ints, width):
num_str = ["{:>{width}}".format(num, width=width) for num in ints]
print(" ".join(num_str))
def showTower():
for h in list(reversed(range(T_CNT))):
vals = [ towers[i][h] for i in range(T_CNT) ]
print_integer_with_width(vals, 5)
def makeMove(m):
d_top_ind = tTopInd(towers[m[1]])
s_top_ind = tTopInd(towers[m[0]])
print("Source index: ", s_top_ind,"Dest Ind: ", 1+d_top_ind)
print("Source tower: ", m[0],"Dest twr: ", m[1])
towers[m[1]][d_top_ind + 1] = towers[m[0]][s_top_ind]
towers[m[0]][s_top_ind] = -1
def visited():
for l in hist:
if compare_lists(l,towers):
return True
return False
def backtrack(old_st):
for i in range(T_CNT):
for j in range(T_CNT):
towers[i][j] = old_st[i][j]
def toh(towers):
showTower()
# input()
if visited():
print("Already visited. Returning")
return False
if solved():
print("TOH solved. Congrats!")
return True
# old_st = [list(towers[0]), list(towers[1]), list(towers[2]) ]
# Using a loop
# old_st = []
# for sublist in towers:
# old_st.append(list(sublist))
old_st = [list(sublist) for sublist in towers]
hist.append(old_st)
moves = getMoves(towers)
for m in moves:
makeMove(m)
if toh(towers):
return True
else:
print("visited or truncated, Backtracking")
backtrack(old_st)
# print("oldsfsf", old_st)
# print("towers", towers)
# showTower()
return False
# print(towers)
toh(towers)