private static void WriteSolutionToConsole(FiveBlocks bottomLayerOfSolution, FiveBlocks topLayerOfSolution) { WriteLine(); WriteLine("The two layers are as follows, where each row is a block and one layer lies perpendicular on top of the other: "); WriteLine(); WriteBlockList(bottomLayerOfSolution); WriteLine(); WriteBlockList(topLayerOfSolution); WriteLine(); }
public void SolveTenBlockPuzzle(out FiveBlocks bottomLayerOfSolution, out FiveBlocks topLayerOfSolution) { // get the blocks needed for the 10 piece wooden puzzle BlockFactory blockFactory = new BlockFactory(); TenBlocks tenBlocks = blockFactory.GetPuzzleWithTenBlocks(); // get a collection of all 5 block variations including all possible flipped blocks. // each of these FiveBlocks objects forms the bottom of a potential solution, until disproved HashSet <FiveBlocks> listsOfFiveBlocks = CalculateAllFiveBlockVariationsIncludingReversals(tenBlocks); CalculateLayerOfSolution(out bottomLayerOfSolution, out topLayerOfSolution, tenBlocks, listsOfFiveBlocks); }
private Dictionary <Block, int> GetTopLayerBlockMappingToBottomLayerIndex(FiveBlocks bottomFiveBlocks, FiveBlocks topFiveBlocks) { Dictionary <Block, int> blockToIndexMaping = new Dictionary <Block, int>(); // try each block in every variation across the botton five foreach (var topBlock in topFiveBlocks) { bool topBlockFits = false; int topBlockIndex = -1; bool topBlockReversedFits = false; int topBlockReversedIndex = -1; for (int i = 0; i < topBlock.Count; i++) { // see if this block is compatible at the given index of the bottom 5 if (bottomFiveBlocks.BlockIsCompatibleAtIndex(topBlock, i)) { topBlockFits = true; topBlockIndex = i; } else if (bottomFiveBlocks.BlockIsCompatibleAtIndex(new Block(topBlock), i)) { topBlockReversedFits = true; topBlockReversedIndex = i; } } if (!topBlockFits && !topBlockReversedFits) { // this block hasn't fit anywhere on the bottom five, meaning the bottom five is incorrect break; } else { // we found a way it fits if (topBlockFits) { // non reversed block fits at some index blockToIndexMaping.Add(topBlock, topBlockIndex); } else if (topBlockReversedFits) { // reversed block fits at some index blockToIndexMaping.Add(new Block(topBlock), topBlockReversedIndex); } } } return(blockToIndexMaping); }
private void CalculateLayerOfSolution(out FiveBlocks bottomLayerOfSolution, out FiveBlocks topLayerOfSolution, TenBlocks tenBlocks, HashSet <FiveBlocks> listsOfFiveBlocks) { bottomLayerOfSolution = null; topLayerOfSolution = null; foreach (var bottomFiveBlocks in listsOfFiveBlocks) { // now get the remaining five blocks FiveBlocks topFiveBlocks = new FiveBlocks(tenBlocks.Where(b => !bottomFiveBlocks.Contains(b)).ToList()); Dictionary <Block, int> blockToIndexMaping = GetTopLayerBlockMappingToBottomLayerIndex(bottomFiveBlocks, topFiveBlocks); if (blockToIndexMaping.Count == 5) { // this means we've matched all 5! we're done! // assign the layers bottomLayerOfSolution = bottomFiveBlocks; topLayerOfSolution = new FiveBlocks(blockToIndexMaping.Keys.ToList()); break; } } }