(* ::Package:: *)

(************************************************************************)
(* This file was generated automatically by the Mathematica front end.  *)
(* It contains Initialization cells from a Notebook file, which         *)
(* typically will have the same name as this file except ending in      *)
(* ".nb" instead of ".m".                                               *)
(*                                                                      *)
(* This file is intended to be loaded into the Mathematica kernel using *)
(* the package loading commands Get or Needs.  Doing so is equivalent   *)
(* to using the Evaluate Initialization Cells menu command in the front *)
(* end.                                                                 *)
(*                                                                      *)
(* DO NOT EDIT THIS FILE.  This entire file is regenerated              *)
(* automatically each time the parent Notebook file is saved in the     *)
(* Mathematica front end.  Any changes you make to this file will be    *)
(* overwritten.                                                         *)
(************************************************************************)



sums[s_,i_]:={}/;s<i||i==0;
sums[s_,s_]:={Table[1,{s}]};
sums[s_,1]:={{s}};
sums[s_,i_]:=Module[{d},Flatten[Table[(Join[{d},#1]&)/@sums[s-d,i-1],{d,s-i+1}],1]]
hanoiP[n_,p_]:=Join[Table[1,{n-1}],Table[0,{p-n-1}],{2 n-1}]/;n<p-1;
hanoiP[n_,p_]:=hanoiP[n,p]=Module[{v,t},t=sums[n-1,p-2];v=(Join[#1,{2 Plus@@Table[Last[hanoiP[#1[[i]],p-i+1]],{i,p-2}]+1}]&)/@t;First[Sort[v,Last[#2]>Last[#1]&]]]
superHanoi[{{d_},{a_,___,b_}}]:={d,a,b}
superHanoi[{tower_,pegs_}]:=Module[{a,pat,lp=Length[pegs],n,ans={},i,p,spread,back},a=Drop[hanoiP[Length[tower],lp],-1];pat=Table[Take[tower,{1+\!\(
\*UnderoverscriptBox[\(\[Sum]\), \(i = 1\), \(n - 1\)]\(a[[i]]\)\),\!\(
\*UnderoverscriptBox[\(\[Sum]\), \(i = 1\), \(n\)]\(a[[i]]\)\)}],{n,lp-2}];spread=Table[p=Drop[pegs,{2,n}];i=Last[p];p[[-1]]=p[[2]];p[[2]]=i;{pat[[n]],p},{n,lp-2}];spread=Cases[spread,{{__},_}];back=({First[#1],Join[{Last[Last[#1]]},Complement[Last[#1],{Last[Last[#1]],Last[pegs]}],{Last[pegs]}]}&)/@Reverse[spread];(AppendTo[ans,superHanoi[#1]]&)/@spread;AppendTo[ans,{Last[tower],First[pegs],Last[pegs]}];(AppendTo[ans,superHanoi[#1]]&)/@back;Partition[Flatten[ans],3]]
towers[numDisks_,numPegs_]:=Module[{t,sH=superHanoi[{Range[numDisks],Range[numPegs]}]},FoldList[(t=#1;t[[#2[[2]]]]=Rest[t[[#2[[2]]]]];t[[Last[#2]]]=Prepend[t[[Last[#2]]],First[#2]];t)&,Join[{Range[numDisks]},Table[{},{numPegs-1}]],sH]]
colors={RGBColor[0.53,0.2,0.18],RGBColor[0.95,0.57,0.38],RGBColor[0.23,0.46,0.52],RGBColor[0.71,0.86,0.5],RGBColor[0.5,0.78,0.78],RGBColor[0.8,0.78,0.68],RGBColor[0.33,0.38,0.48],RGBColor[0.85,0.87,0.32]};
graphtower[n_,li_]:=Module[{y=-2},Graphics[{{RGBColor[0.2,0.4,0.4],Rectangle[{-.4,0},{.4,2 n+1}]},Map[(y=y+2;{EdgeForm[{Black,Thickness[.025]}],colors[[#]],Rectangle[{-2 #+.6,y+.05},{2 #-.6,y+2}]})&,Reverse[li]]},PlotRange->{{-2 n,2 n},{0,2 n+1}},AspectRatio->2]];
showtowers[li_List]:=GraphicsRow[(graphtower[Length[Flatten[li]],#]&)/@li,ImageSize->{500,400}]
HanoiDiagram[NumberOfDisks_,NumberOfPegs_,PositionInmoveList_]:=showtowers[towers[NumberOfDisks,NumberOfPegs][[PositionInmoveList]]]
Manipulate[If[PositionInmoveList>Length@towers[NumberOfDisks,NumberOfPegs],PositionInmoveList=Length@towers[NumberOfDisks,NumberOfPegs]];HanoiDiagram[NumberOfDisks,NumberOfPegs,PositionInmoveList],{{PositionInmoveList,1,"position"},1,{{8,16,32,64,128,256},{6,10,14,18,26,34},{6,8,12,16,20,24}}[[NumberOfPegs-2,NumberOfDisks-2]],1},{{NumberOfPegs,3,"number of pegs"},{3,4,5}},{{NumberOfDisks,4,"number of disks"},Range[3,8]},SaveDefinitions->True]



