Stanislav Berkov
Posted on
Denver Dev Day snake puzzle solver
Athttps://www.meetup.com/DenverDevDay/ 2023 Fall I get the following puzzle.
When disassembled it looks this way
Quite hard puzzle I would say. I decided to write a C# program to solve it.
constintdim=3;int[]snake={3,3,3,3,2,2,2,3,3,2,2,3,2,3,2,2,3};bool[,,]used=newbool[dim,dim,dim];Stack<Point>path=new();used[0,0,0]=true;if(DFS((0,0,0),0,(0,0,0))){foreach(varminpath.Reverse()){Console.WriteLine(m);}};boolDFS(Pointp,intidx,Pointm){if(idx>=snake.Length){returnfalse;}if(p==(dim-1,dim-1,dim-1)&&idx==snake.Length-1){Console.WriteLine("done!");returntrue;}foreach(varmoveinPoint.Moves){if(move==m){continue;}varlen=(snake[idx]-1);varnextLen1Or2=p+(move*len);varnextLen1=p+move;if(!nextLen1Or2.InCube(dim)){continue;}if(used[nextLen1Or2.X,nextLen1Or2.Y,nextLen1Or2.Z]){continue;}if(used[nextLen1.X,nextLen1.Y,nextLen1.Z]){continue;}path.Push(move);used[nextLen1Or2.X,nextLen1Or2.Y,nextLen1Or2.Z]=true;used[nextLen1.X,nextLen1.Y,nextLen1.Z]=true;if(DFS(nextLen1Or2,idx+1,move)){returntrue;}path.Pop();used[nextLen1Or2.X,nextLen1Or2.Y,nextLen1Or2.Z]=false;used[nextLen1.X,nextLen1.Y,nextLen1.Z]=false;}returnfalse;}publicrecordstructPoint(intX,intY,intZ){publicstaticreadonlyPoint[]Moves={(-1,0,0),(0,-1,0),(0,0,-1),(1,0,0),(0,1,0),(0,0,1)};publicstaticPointoperator+(Pointa,Pointb)=>new(a.X+b.X,a.Y+b.Y,a.Z+b.Z);publicstaticPointoperator*(Pointa,intf)=>new(a.X*f,a.Y*f,a.Z*f);publicstaticimplicitoperatorPoint((intX,intY,intZ)p)=>new(p.X,p.Y,p.Z);publicreadonlyboolInCube(intsize)=>X>=0&&X<size&&Y>=0&&Y<size&&Z>=0&&Z<size;}
So the solution is:
Point { X = 1, Y = 0, Z = 0 }Point { X = 0, Y = 1, Z = 0 }Point { X = -1, Y = 0, Z = 0 }Point { X = 0, Y = 0, Z = 1 }Point { X = 1, Y = 0, Z = 0 }Point { X = 0, Y = 0, Z = -1 }Point { X = 1, Y = 0, Z = 0 }Point { X = 0, Y = -1, Z = 0 }Point { X = -1, Y = 0, Z = 0 }Point { X = 0, Y = 0, Z = 1 }Point { X = 0, Y = 1, Z = 0 }Point { X = 0, Y = 0, Z = -1 }Point { X = 1, Y = 0, Z = 0 }Point { X = 0, Y = 0, Z = 1 }Point { X = 1, Y = 0, Z = 0 }Point { X = 0, Y = 1, Z = 0 }
Top comments(0)
Subscribe
For further actions, you may consider blocking this person and/orreporting abuse