public bool DoEarlyBailOut() { var bailOut = false; FillPuzzleRoads(UsedTileList); if (DefinitivePuzzleRoads.Count > 0) { bailOut = EarlyBailOut(); } if (bailOut) { var bailOutString = String.Format("Bailing out at: {0}", String.Join(" AND ", UsedTileList.Select(s => s.ToString()))); Debug.WriteLine(bailOutString); //Upvote all tiles in current and deeper layers with corresponding amounts? var allKeys = UsedTileList/*.Where(s => s.PuzzleDepthCounter >= UsedTileList.Count)*/ .Select(s => new UsedTileDictionaryKey(s.PuzzleIndex, s.TileNumber, s.Degrees)).ToList(); var x = "bla"; CheckTileCountAndUpIfNeeded(true); return(true); //There is still a problem when the first bailOut is the second tile, we add += original layer. //The next time the 2nd tile is checked again, the amount is upped by += layer, and it will check again foreach (var key in allKeys.OrderByDescending(s => s.PuzzleIndex)) { var relevantTile = UsedTileList.FirstOrDefault(s => s.TileNumber == key.TileNumber && s.Degrees == key.Degrees); var relevantLayer = PuzzleIndexCounter[key.PuzzleIndex]; if (Initiator.CheckedTileDictionary[key] >= OriginalDepthCounter[relevantLayer]) { return(false); } if (relevantTile.PuzzleDepthCounter >= allKeys.Count) { Initiator.CheckedTileDictionary[key] += OriginalDepthCounter[relevantLayer]; if (DynamicCheckedTileDictionary[key] < AmountOfMaximumTriesPerTile) { DynamicCheckedTileDictionary[key] += OriginalDepthCounter[relevantLayer]; } //var aboveLayer = OriginalDepthCounter.FirstOrDefault(s => s.Key > relevantTile.PuzzleDepthCounter); var usedTileNumbersAboveThisLayer = allKeys.Where(s => s.PuzzleIndex <= DepthToIndex[relevantLayer]).Select(a => a.TileNumber).ToList(); var relevantIndex = DepthToIndex[relevantLayer]; var everyKeyExceptUsedOnes = DynamicCheckedTileDictionary.Where(s => s.Key.PuzzleIndex > relevantIndex && !usedTileNumbersAboveThisLayer.Contains(s.Key.TileNumber)).ToList(); foreach (var keyEx in everyKeyExceptUsedOnes) { //Update the count for the next round var layer = PuzzleIndexCounter[keyEx.Key.PuzzleIndex]; Initiator.CheckedTileDictionary[keyEx.Key] += OriginalDepthCounter[layer]; DynamicCheckedTileDictionary[keyEx.Key] += OriginalDepthCounter[layer]; } RemoveAndResetPuzzleTile(relevantTile); //UsedTileList.Remove(relevantTile); //UsedPuzzleTilesIndices.Remove(key.PuzzleIndex); //relevantTile.PuzzleDepthCounter = 0; //relevantTile.PuzzleIndex = -1; //UsedTileDictionary[key.TileNumber] = false; } else { //var layer = PuzzleIndexCounter[key.PuzzleIndex]; Initiator.CheckedTileDictionary[key] += OriginalDepthCounter[relevantLayer + 1]; if (Initiator.CheckedTileDictionary[key] >= DynamicCheckedTileDictionary[key]) { //var relevantTile = UsedTileList.FirstOrDefault(s => //s.TileNumber == notKey.TileNumber && s.Degrees == notKey.Degrees); RemoveAndResetPuzzleTile(relevantTile); //UsedTileList.Remove(relevantTile); //UsedPuzzleTilesIndices.Remove(key.PuzzleIndex); //relevantTile.PuzzleDepthCounter = 0; //relevantTile.PuzzleIndex = -1; //UsedTileDictionary[key.TileNumber] = false; } } } //We bailed out, clear list and break //var allNotKeys = UsedTileList.Where(s => s.PuzzleDepthCounter <= UsedTileList.Count) //.Select(s => new UsedTileDictionaryKey(s.PuzzleIndex, s.TileNumber, s.Degrees)).ToList(); //foreach (var notKey in allNotKeys) //{ //} return(true); } return(false); }
private void CheckTileCountAndUpIfNeeded(bool didBailOut) { var usedTileNumbers = UsedTileList.Select(s => s.TileNumber); var allKeys = UsedTileList.Select(s => new UsedTileDictionaryKey(s.PuzzleIndex, s.TileNumber, s.Degrees)).ToList(); //var otherTileKeys = Initiator.TotalRotationTileList.Where(s => !usedTileNumbers.Contains(s.TileNumber)); var uppedAboveTiles = false; if (AmountOfCheckedSolutions % 480 == 0) { } if (!didBailOut) { } var dynamicNotMaxedOut = DynamicCheckedTileDictionary.Where(s => s.Value < AmountOfMaximumTriesPerTile).ToList(); //var dynamicLowerThanChecked = DynamicCheckedTileDictionary.Where(a => a.Value <= CheckedTileDictionary[a.Key] && a.Value != AmountOfMaximumTriesPerTile).ToList(); //if (dynamicLowerThanChecked.Any()) //{ //} if (didBailOut) { //if (!uppedAboveTiles) { var lastTile = UsedTileList.OrderBy(s => s.PuzzleIndex).LastOrDefault(); var keysAboveCurrent = allKeys.Where(s => PuzzleIndexCounter[s.PuzzleIndex] <= lastTile.PuzzleDepthCounter).ToList(); var relevantLayer = PuzzleIndexCounter[lastTile.PuzzleIndex]; var oriDepthAmount = OriginalDepthCounter[relevantLayer]; if (keysAboveCurrent.Any()) { foreach (var keyAbove in keysAboveCurrent) { if (CheckedTileDictionary[keyAbove] + oriDepthAmount <= AmountOfMaximumTriesPerTile) { CheckedTileDictionary[keyAbove] += oriDepthAmount; } } uppedAboveTiles = true; } //var usedTileKeysBelowThisLayer = CheckedTileDictionary.Keys.Where(s => s.PuzzleIndex > key.PuzzleIndex && !usedTileNumbers.Contains(s.TileNumber)); var usedTileKeysBelowThisLayer = DynamicCheckedTileDictionary.Keys.Where(s => s.PuzzleIndex > lastTile.PuzzleIndex && !usedTileNumbers.Contains(s.TileNumber)).ToList(); var firstLayerAfterBailOut = relevantLayer + 1; foreach (var otKey in usedTileKeysBelowThisLayer) { var otKeyLayer = PuzzleIndexCounter[otKey.PuzzleIndex]; if (DynamicCheckedTileDictionary[otKey] + OriginalDepthCounter[firstLayerAfterBailOut] <= AmountOfMaximumTriesPerTile) { if (otKey.PuzzleIndex == 5 && otKey.TileNumber == 2 && otKey.Degrees == 0 && DynamicCheckedTileDictionary[otKey] >= 440) { } DynamicCheckedTileDictionary[otKey] += OriginalDepthCounter[firstLayerAfterBailOut]; } else { Debug.WriteLine(String.Format("We bailout out, but did NOT up the below layer! {0}", otKey.ToString())); //Lets fill it up towards max DynamicCheckedTileDictionary[otKey] = AmountOfMaximumTriesPerTile; } CheckedTileDictionary[otKey] += OriginalDepthCounter[firstLayerAfterBailOut]; } var keysAboveCurrentlayer = allKeys.Where(s => PuzzleIndexCounter[s.PuzzleIndex] != lastTile.PuzzleDepthCounter); var tileNumbersAboveCurrentLayer = keysAboveCurrentlayer.Select(s => s.TileNumber); var tilesInSameLayer = CheckedTileDictionary.Keys.Where(s => s.PuzzleIndex == lastTile.PuzzleIndex && !tileNumbersAboveCurrentLayer.Contains(s.TileNumber)); var key = new UsedTileDictionaryKey(lastTile.PuzzleIndex, lastTile.TileNumber, lastTile.Degrees); var allSameAmount = tilesInSameLayer.All(a => CheckedTileDictionary[a] == CheckedTileDictionary[key] && CheckedTileDictionary[a] != AmountOfMaximumTriesPerTile); if (allSameAmount) { foreach (var tsl in tilesInSameLayer) { if (DynamicCheckedTileDictionary[tsl] + oriDepthAmount <= AmountOfMaximumTriesPerTile) { DynamicCheckedTileDictionary[tsl] += oriDepthAmount; } } } foreach (var kkey in keysAboveCurrent) { var rTile = UsedTileList.FirstOrDefault(s => s.PuzzleIndex == kkey.PuzzleIndex && s.TileNumber == kkey.TileNumber && s.Degrees == kkey.Degrees); //See if one of the used keys (starting at the end) has been used too much if (Initiator.CheckedTileDictionary[kkey] >= DynamicCheckedTileDictionary[kkey]) { RemoveAndResetPuzzleTile(rTile); //break; } } //RemoveAndResetPuzzleTile(lastTile); } var dynamicLowerThanChecked = DynamicCheckedTileDictionary.Where(a => a.Value <= CheckedTileDictionary[a.Key] && a.Value != AmountOfMaximumTriesPerTile).ToList(); if (dynamicLowerThanChecked.Any()) { } return; } foreach (var key in allKeys.OrderByDescending(s => s.PuzzleIndex)) { var relevantTile = UsedTileList.FirstOrDefault(s => s.TileNumber == key.TileNumber && s.Degrees == key.Degrees); var relevantLayer = PuzzleIndexCounter[key.PuzzleIndex]; var oriDepthAmount = OriginalDepthCounter[relevantLayer]; var dynamicKeyCount = DynamicCheckedTileDictionary[key]; //else { //Only up if there is an actual checked solution if (allKeys.Count == SubmittedPuzzleTileCount) { if (CheckedTileDictionary[key] + 1 <= dynamicKeyCount) { CheckedTileDictionary[key]++; } //if (CheckedTileDictionary[key] > 0) //{ // var keysAboveCurrentlayer = allKeys.Where(s => PuzzleIndexCounter[s.PuzzleIndex] != relevantTile.PuzzleDepthCounter); // var tileNumbersAboveCurrentLayer = keysAboveCurrentlayer.Select(s => s.TileNumber); // var tilesInSameLayer = CheckedTileDictionary.Keys.Where(s => s.PuzzleIndex == key.PuzzleIndex && !tileNumbersAboveCurrentLayer.Contains(s.TileNumber)); // var allSameAmount = tilesInSameLayer.All(a => CheckedTileDictionary[a] == CheckedTileDictionary[key]); // if (allSameAmount) // { // if (DynamicCheckedTileDictionary[key] + oriDepthAmount <= AmountOfMaximumTriesPerTile) // { // foreach (var otKey in tilesInSameLayer) // { // DynamicCheckedTileDictionary[new UsedTileDictionaryKey(key.PuzzleIndex, otKey.TileNumber, otKey.Degrees)] += oriDepthAmount; // } // } // } //} //foreach (var akey in allKeys.OrderByDescending(s => s.PuzzleIndex)) //{ //var relevantTile = UsedTileList.FirstOrDefault(s => //s.TileNumber == akey.TileNumber && s.Degrees == akey.Degrees); //See if one of the used keys (starting at the end) has been used too much if (Initiator.CheckedTileDictionary[key] >= DynamicCheckedTileDictionary[key]) { //UsedPuzzleTilesIndices.Remove(key.PuzzleIndex); //UsedTileList.Remove(relevantTile); //UsedTileDictionary[key.TileNumber] = false; //var keysAboveCurrentlayer = allKeys.Where(s => PuzzleIndexCounter[s.PuzzleIndex] != relevantTile.PuzzleDepthCounter); //var tileNumbersAboveCurrentLayer = keysAboveCurrentlayer.Select(s => s.TileNumber); //var tilesInSameLayer = CheckedTileDictionary.Keys.Where(s => s.PuzzleIndex == key.PuzzleIndex && !tileNumbersAboveCurrentLayer.Contains(s.TileNumber)); //We should only += the DynamicDepthCounter in the above layers. var aboveLayer = OriginalDepthCounter.FirstOrDefault(s => s.Key > relevantTile.PuzzleDepthCounter); if (aboveLayer.Key != 0) { var usedTileNumbersAboveThisLayer = allKeys.Where(s => s.PuzzleIndex < DepthToIndex[aboveLayer.Key]).Select(a => a.TileNumber).ToList(); var relevantIndex = DepthToIndex[aboveLayer.Key]; var everyKeyExceptUsedOnes = DynamicCheckedTileDictionary.Where(s => s.Key.PuzzleIndex == relevantIndex && !usedTileNumbersAboveThisLayer.Contains(s.Key.TileNumber)).ToList(); foreach (var keyEx in everyKeyExceptUsedOnes) { //Update the count for the next round DynamicCheckedTileDictionary[keyEx.Key] += OriginalDepthCounter[aboveLayer.Key]; } } RemoveAndResetPuzzleTile(relevantTile); //relevantTile.PuzzleDepthCounter = 0; //relevantTile.PuzzleIndex = -1; } //} } } var dynamicLowerThanChecked = DynamicCheckedTileDictionary.Where(a => a.Value <= CheckedTileDictionary[a.Key] && a.Value != AmountOfMaximumTriesPerTile).ToList(); if (dynamicLowerThanChecked.Any()) { } //See if one of the used keys (starting at the end) has been used too much if (Initiator.CheckedTileDictionary[key] >= dynamicKeyCount && didBailOut) { RemoveAndResetPuzzleTile(relevantTile); break; } } }