Пример #1
0
        public override ulong SolvePart1()
        {
            int targetWeight = packageWeights.Sum() / 3;

            GetMinMaxGroupSize(targetWeight, out int minGroupSize, out int maxGroupSize);

            // Start finding the groups
            int group1Size = minGroupSize;

            PackageGroup bestGroup   = null;
            ulong        bestGroupQE = ulong.MaxValue;

            var packageWeightsSet = new HashSet <int>(packageWeights);
            var groupDictionary   = new FlexibleDictionary <int, HashSet <int>[]>();

            while (true)
            {
                foreach (var group1 in GetGroups(group1Size))
                {
                    var remainingWeights = new HashSet <int>(packageWeightsSet);
                    remainingWeights.ExceptWith(group1);
                    int maxGroup2Size = maxGroupSize - (packageWeightsSet.Count - remainingWeights.Count);

                    for (int group2Size = minGroupSize; group2Size < maxGroup2Size; group2Size++)
                    {
                        if (GetGroups(group2Size).Any(group => !remainingWeights.Overlaps(group)))
                        {
                            EvaluateBestGroup(new(group1), ref bestGroup, ref bestGroupQE);
                            break;
                        }
                    }
                }

                if (bestGroup is not null)
                {
                    return(bestGroupQE);
                }

                group1Size++;
            }

            IEnumerable <HashSet <int> > GetGroups(int size)
            {
                if (!groupDictionary.ContainsKey(size))
                {
                    groupDictionary[size] = FindGroups(size, targetWeight, packageWeights);
                }
                return(groupDictionary[size]);
            }
        }
Пример #2
0
        public void CloneTest()
        {
            var cloned = testDictionary.Clone();

            for (int i = 0; i < 5; i++)
            {
                Assert.AreEqual((char)('a' + i), cloned[i]);
            }
            Assert.AreEqual(testDictionary.Count, cloned.Count);

            cloned.Add(10, 'f');
            Assert.IsFalse(testDictionary.ContainsKey(10));
            Assert.IsTrue(cloned.ContainsKey(10));
        }