Пример #1
0
        public override string P2()
        {
            FastList <int> packages = new(Input.Select(line => int.Parse(line)).OrderByDescending(i => i).ToArray());

            int target = packages.Sum() / 4;

            List <int[]> sets = new();

            CollectSets(packages, target, new(), sets);
            FastList <int[]> allSets = new(sets.OrderBy(s => s.Length).ToArray());

            int   shortestFound = int.MaxValue;
            ulong minQE         = int.MaxValue;

            allSets.ForEachBreakable(group1 =>
            {
                if (group1.Length > shortestFound)
                {
                    return(false);
                }

                // Remove all sets that overlap
                FastList <int[]> subset1 = allSets.Without(set => Overlap(group1, set));

                bool searching = true;

                // For each sets in the non-overlapping group... (group2)
                subset1.ForEachBreakable(group2 =>
                {
                    // Remove all sets that overlap
                    FastList <int[]> subset2 = subset1.Without(set => Overlap(group2, set));

                    subset2.ForEachBreakable(group3 =>
                    {
                        FastList <int[]> final = subset2.Without(set => Overlap(group3, set));

                        if (final.Length > 0)
                        {
                            //Console.WriteLine($"{string.Join(" ", group1)} = {Product(group1)}");
                            shortestFound = group1.Length;
                            minQE         = Math.Min(minQE, Product(group1));
                            searching     = false;
                        }

                        return(searching);
                    });

                    return(searching);
                });

                return(true);
            });

            return(minQE.ToString());
        }