CrossTransformation TryFill(string dictWord, ReadOnlySpan <char> word, ICrossDictionary dict, bool puzzle) { var trans = new CrossTransformation(dictWord); int instSum = 0; for (int i = 0; i < word.Length; i++) { if (_pattern[i] == '.') { if (AdjacentPatterns[i] != null) { int adjIndex; if (_isHorizontal) { adjIndex = _startY - AdjacentPatterns[i].StartY; } else { adjIndex = _startX - AdjacentPatterns[i].StartX; } char c = AdjacentPatterns[i].Pattern[adjIndex]; if (c == '.') { char[] adjacent = AdjacentPatterns[i].Pattern; adjacent[adjIndex] = word[i]; int newInstCount = dict.GetMatchCount(adjacent); adjacent[adjIndex] = '.'; // clear the adj index if (newInstCount == 0) { return(null); } instSum += newInstCount; trans.AddChangeInst(i, AdjacentPatterns[i].InstantiationCount, newInstCount); trans.AddChange(i, adjIndex, word[i]); } else if (puzzle || c != word[i]) { return(null); } } trans.AddChange(-1, i, word[i]); } } trans.AddChangeInst(-1, _instantiationCount, (int)Constants.Unbounded); trans.SumInst = instSum; // set the sum instantiation count return(trans); }
public void Preprocess(ICrossDictionary aDict) { _horizontalPatterns = new List <CrossPattern>(); _startWords.Sort(new YXStartWordComparer()); // first create horizontal patterns int wordIdx = 0; for (int y = 0; y < _sizeY; y++) { int nextX = 0; while (wordIdx < _startWords.Count) { var sw = _startWords[wordIdx]; // Console.WriteLine("StartWord x:{0} y:{1} idx:{2}/cnt:{3}",sw.StartX,sw.StartY,wordIdx,_startWords.Count); if (sw.StartY == y) { if (sw.StartX - nextX >= MinPatternLength) { var cp = new CrossPattern(nextX, y, sw.StartX - nextX, true); // Console.WriteLine("SW pattern startX: {0} startY: {1} len: {2}",cp.StartX, cp.StartY, cp.Length); _horizontalPatterns.Add(cp); } nextX = sw.StartX + 1; wordIdx++; } else { break; } } if (_sizeX - nextX >= MinPatternLength) { var cp = new CrossPattern(nextX, y, _sizeX - nextX, true); // Console.WriteLine("EL pattern startX: {0} startY: {1} len: {2}",cp.StartX, cp.StartY, cp.Length); _horizontalPatterns.Add(cp); } } _verticalPatterns = new List <CrossPattern>(); _startWords.Sort(new XYStartWordComparer()); // then create vertical patterns wordIdx = 0; for (int x = 0; x < _sizeX; x++) { int nextY = 0; while (wordIdx < _startWords.Count) { var sw = _startWords[wordIdx]; // Console.WriteLine("StartWord x:{0} y:{1} idx:{2}/cnt:{3}",sw.StartX,sw.StartY,wordIdx,_startWords.Count); if (sw.StartX == x) { if (sw.StartY - nextY >= MinPatternLength) { var cp = new CrossPattern(x, nextY, sw.StartY - nextY, false); // Console.WriteLine("SW patternY startX: {0} startY: {1} len: {2}",cp.StartX, cp.StartY, cp.Length); _verticalPatterns.Add(cp); } nextY = sw.StartY + 1; wordIdx++; } else { break; } } if (_sizeY - nextY >= MinPatternLength) { var cp = new CrossPattern(x, nextY, _sizeY - nextY, false); // Console.WriteLine("EL patternY startX: {0} startY: {1} len: {2}",cp.StartX, cp.StartY, cp.Length); _verticalPatterns.Add(cp); } } BindAdjacentPatterns(); // calculate instantiation count var wordLengthCount = new int[aDict.MaxWordLength + 1]; for (int i = 1; i <= aDict.MaxWordLength; i++) { wordLengthCount[i] = aDict.GetWordOfLengthCount(i); } int patternCount = GetPatternCount(); for (int i = 0; i < patternCount; i++) { var pattern = GetCrossPattern(i); if (pattern.Pattern == null) { // empty pattern.InstantiationCount = wordLengthCount[pattern.Length]; pattern.Pattern = Enumerable.Repeat('.', pattern.Length).ToArray(); } else { // already set some letters pattern.InstantiationCount = aDict.GetMatchCount(pattern.Pattern); } } }
CrossTransformation TryFill(string word, ICrossDictionary dict, bool puzzle) { var trans = new CrossTransformation(word); int instSum = 0; for (int i = 0; i < word.Length; i++) { if (_pattern[i] == '.') { if (AdjacentPatterns[i] != null) { int adjIndex; if (_isHorizontal) adjIndex = _startY - AdjacentPatterns[i].StartY; else adjIndex = _startX - AdjacentPatterns[i].StartX; char c = AdjacentPatterns[i].Pattern[adjIndex]; if (c == '.') { char[] adjacent = AdjacentPatterns[i].Pattern; adjacent[adjIndex] = word[i]; int newInstCount = dict.GetMatchCount(adjacent); adjacent[adjIndex] = '.'; if (newInstCount == 0) return null; instSum += newInstCount; trans.AddChangeInst(i, AdjacentPatterns[i].InstantiationCount, newInstCount); trans.AddChange(i, adjIndex, word[i]); } else if (puzzle || c != word[i]) { return null; } } trans.AddChange(-1, i, word[i]); } } trans.AddChangeInst(-1, _instantiationCount, (int)Constants.Unbounded); trans.SumInst = instSum; return trans; }