Пример #1
0
        public void TestSubsetIteratorThatCrashed()
        {
            SubsetIterator <int> it = new SubsetIterator <int>(2, new List <int> {
                1, 4, 7
            });

            Assert.IsTrue(it.MoveNext());
            AssertListEquality(new List <int> {
                1, 4
            }, it.Current);

            Assert.IsTrue(it.MoveNext());
            AssertListEquality(new List <int> {
                1, 7
            }, it.Current);

            Assert.IsTrue(it.MoveNext());
            AssertListEquality(new List <int> {
                4, 7
            }, it.Current);

            Assert.IsFalse(it.MoveNext());
        }
Пример #2
0
        public void TestSubsetIterator()
        {
            SubsetIterator <int> it = new SubsetIterator <int>(2, new List <int> {
                1, 6, 7, 9
            });

            Assert.IsTrue(it.MoveNext());
            AssertListEquality(new List <int> {
                1, 6
            }, it.Current);

            Assert.IsTrue(it.MoveNext());
            AssertListEquality(new List <int> {
                1, 7
            }, it.Current);

            Assert.IsTrue(it.MoveNext());
            AssertListEquality(new List <int> {
                1, 9
            }, it.Current);

            Assert.IsTrue(it.MoveNext());
            AssertListEquality(new List <int> {
                6, 7
            }, it.Current);

            Assert.IsTrue(it.MoveNext());
            AssertListEquality(new List <int> {
                6, 9
            }, it.Current);

            Assert.IsTrue(it.MoveNext());
            AssertListEquality(new List <int> {
                7, 9
            }, it.Current);

            Assert.IsFalse(it.MoveNext());
        }
Пример #3
0
        public void TestBiggerSubsetIterator()
        {
            SubsetIterator <int> it = new SubsetIterator <int>(3, new List <int> {
                1, 3, 4, 6, 7, 9
            });

            Assert.IsTrue(it.MoveNext());
            AssertListEquality(new List <int> {
                1, 3, 4
            }, it.Current);

            Assert.IsTrue(it.MoveNext());
            AssertListEquality(new List <int> {
                1, 3, 6
            }, it.Current);

            Assert.IsTrue(it.MoveNext());
            AssertListEquality(new List <int> {
                1, 3, 7
            }, it.Current);

            Assert.IsTrue(it.MoveNext());
            AssertListEquality(new List <int> {
                1, 3, 9
            }, it.Current);

            Assert.IsTrue(it.MoveNext());
            AssertListEquality(new List <int> {
                1, 4, 6
            }, it.Current);

            Assert.IsTrue(it.MoveNext());
            AssertListEquality(new List <int> {
                1, 4, 7
            }, it.Current);

            Assert.IsTrue(it.MoveNext());
            AssertListEquality(new List <int> {
                1, 4, 9
            }, it.Current);

            Assert.IsTrue(it.MoveNext());
            AssertListEquality(new List <int> {
                1, 6, 7
            }, it.Current);

            Assert.IsTrue(it.MoveNext());
            AssertListEquality(new List <int> {
                1, 6, 9
            }, it.Current);

            Assert.IsTrue(it.MoveNext());
            AssertListEquality(new List <int> {
                1, 7, 9
            }, it.Current);

            Assert.IsTrue(it.MoveNext());
            AssertListEquality(new List <int> {
                3, 4, 6
            }, it.Current);

            Assert.IsTrue(it.MoveNext());
            AssertListEquality(new List <int> {
                3, 4, 7
            }, it.Current);

            Assert.IsTrue(it.MoveNext());
            AssertListEquality(new List <int> {
                3, 4, 9
            }, it.Current);

            Assert.IsTrue(it.MoveNext());
            AssertListEquality(new List <int> {
                3, 6, 7
            }, it.Current);

            Assert.IsTrue(it.MoveNext());
            AssertListEquality(new List <int> {
                3, 6, 9
            }, it.Current);

            Assert.IsTrue(it.MoveNext());
            AssertListEquality(new List <int> {
                3, 7, 9
            }, it.Current);

            Assert.IsTrue(it.MoveNext());
            AssertListEquality(new List <int> {
                4, 6, 7
            }, it.Current);

            Assert.IsTrue(it.MoveNext());
            AssertListEquality(new List <int> {
                4, 6, 9
            }, it.Current);

            Assert.IsTrue(it.MoveNext());
            AssertListEquality(new List <int> {
                4, 7, 9
            }, it.Current);

            Assert.IsTrue(it.MoveNext());
            AssertListEquality(new List <int> {
                6, 7, 9
            }, it.Current);

            Assert.IsFalse(it.MoveNext());
        }
Пример #4
0
        protected bool SolveForCells(int groupSize, ref Board board, CellData cellData)
        {
            bool ruleSucceeded = false;

            var unsolvedValues = cellData.GetUnsolvedValues();
            var unsolvedCells  = cellData.GetUnsolvedCells();

            if (groupSize >= unsolvedCells.Count)
            {
                return(false);
            }

            var valueSetGenerator = new SubsetIterator <int>(groupSize, unsolvedValues);

            while (valueSetGenerator.MoveNext())
            {
                List <int> setOfValues = valueSetGenerator.Current;

                HashSet <KeyValuePair <int, List <int> > > cellsWithValues = new HashSet <KeyValuePair <int, List <int> > >();

                foreach (int value in setOfValues)
                {
                    cellsWithValues.AddRange(unsolvedCells.Where(kv => kv.Value.Contains(value)).ToList());
                }

                if (cellsWithValues.Count == groupSize)
                {
                    foreach (KeyValuePair <int, List <int> > cell in cellsWithValues)
                    {
                        foreach (int value in unsolvedValues)
                        {
                            if (!setOfValues.Contains(value) && cell.Value.Contains(value))
                            {
                                board.RemoveValueFromCell(cell.Key, value);
                                ruleSucceeded = true;
                            }
                        }
                    }
                }
            }

            var cellSetGenerator = new SubsetIterator <KeyValuePair <int, List <int> > >(groupSize, unsolvedCells.ToList());

            while (cellSetGenerator.MoveNext())
            {
                List <KeyValuePair <int, List <int> > > setOfCells = cellSetGenerator.Current;
                HashSet <int> valuesInCells = new HashSet <int>();

                foreach (KeyValuePair <int, List <int> > cell in setOfCells)
                {
                    valuesInCells.AddRange(cell.Value);
                }

                if (valuesInCells.Count == groupSize)
                {
                    foreach (KeyValuePair <int, List <int> > cell in unsolvedCells)
                    {
                        if (!setOfCells.Select(c => c.Key).Contains(cell.Key))
                        {
                            foreach (int valueInCell in valuesInCells)
                            {
                                if (cell.Value.Contains(valueInCell))
                                {
                                    board.RemoveValueFromCell(cell.Key, valueInCell);
                                    ruleSucceeded = true;
                                }
                            }
                        }
                    }
                }
            }

            return(ruleSucceeded);
        }