Situations like this are already well-solved in EDA tools like gEDA and other schematic capture and physical layout packages. These challenges would be a good place for Higher-Order Perl applications, as they're microworld problems like the 'Blocks World' problem detailed in AI books like
Patrick Winston's Artificial Intelligence. What you describe is actually a fairly straightforward 2D+layers situation, and if you limit the allowable turnout designs and curve-piece radii, a recursive try-and-test application becomes very doable. It would work a lot like the Tetris solver used as an example in the Jaguar book, although the solution network would be much more complex and certainly the visualizer would, too.