Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # others[N,D]: N is the tower itself (0,1,2), and D is the ODD/EVEN inidicator, where 0 means EVEN and 1 means ODD.
- others = ((1, 2), (0, 2), (0, 1))
- size = int(input("Set the size of the tower (discs): "))
- movesRequired = (2**size - 1)
- towers = [
- list(size - i for i in range(size)), # full tower (size, size - 1, size - 2, ..., 1)
- [],
- []
- ]
- def move(f, t):
- piece = towers[f][-1]
- if len(towers[t])>0 and towers[t][-1]<=piece: raise "Invalid Movement"
- towers[f].pop()
- towers[t].append(piece)
- print(f"Start: {towers[0]} {towers[1]} {towers[2]}")
- nextMoveFrom = 0
- moveCount = 0
- while len(towers[2]) < size:
- moveTo = others[nextMoveFrom][len(towers[nextMoveFrom]) % 2]
- move(nextMoveFrom, moveTo)
- moveCount += 1
- percentage = 100 * moveCount / movesRequired
- print(f"Move {moveCount}: {nextMoveFrom} -> {moveTo}; Towers: {towers[0]} {towers[1]} {towers[2]} ({percentage}%)")
- o1 = others[moveTo][0]
- o2 = others[moveTo][1]
- nextMoveFrom = (
- o2 if len(towers[o1])==0 else
- o1 if len(towers[o2])==0 else
- (o1 if towers[o1][-1] < towers[o2][-1] else o2))
Add Comment
Please, Sign In to add comment