Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Stanislav Berkov
Stanislav Berkov

Posted on

Denver Dev Day snake puzzle solver

Athttps://www.meetup.com/DenverDevDay/ 2023 Fall I get the following puzzle.

Solved puzzle

When disassembled it looks this way

Disassembled puzzle

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;}
Enter fullscreen modeExit fullscreen mode

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 }
Enter fullscreen modeExit fullscreen mode

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Developing trading frontend using typescript, angular, angular material (angular components), protobuf, aspnet 6, webapi, cassandra db, webpack and many-many other stuff.
  • Work
    CQG Inc.
  • Joined

More fromStanislav Berkov

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp