예제 #1
0
        public static int FindWeight(BaseProgram b)
        {
            if (b.programs == null || b.programs.Count == 0)
            {
                return(0);
            }

            var childWeights = b.programs.Select(x => x.totalweight);

            Console.WriteLine(b.weight + ", " + string.Join(",", childWeights));
            return(b.totalweight);
        }
예제 #2
0
        public static string Calculate2()
        {
            Console.WriteLine("Day7 part 1");
            string result       = null;
            var    lines        = File.ReadAllLines("..\\..\\Input\\Day7.txt");
            var    pd           = new Dictionary <string, BaseProgram>();
            var    basePrograms = new List <BaseProgram>();

            foreach (var line in lines)
            {
                var           words    = line.Split();
                var           r        = words[0].Trim();
                int           weight   = int.Parse(words[1].Replace("(", string.Empty).Replace(")", string.Empty));
                List <string> children = new List <string>();
                if (line.Contains("->"))
                {
                    children = line.Split('>')[1].Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Select(x => x.Trim()).ToList();
                }
                BaseProgram p;
                if (pd.ContainsKey(r))
                {
                    p        = pd[r];
                    p.weight = weight;
                }
                else
                {
                    p = new BaseProgram()
                    {
                        name = r, programs = new List <BaseProgram>(), weight = weight
                    };
                    pd.Add(r, p);
                }

                foreach (var c in children)
                {
                    BaseProgram child = null;
                    if (pd.ContainsKey(c))
                    {
                        child = pd[c];
                    }
                    else
                    {
                        child = new BaseProgram()
                        {
                            name = c, programs = new List <BaseProgram>(), weight = -1
                        };
                        pd.Add(c, child);
                    }
                    p.programs.Add(child);
                }
            }
            var root = pd.Values.Except(pd.Values.SelectMany(x => x.programs)).First();

            int targetWeight = 0;

            while (true)
            {
                var groups = root.programs.GroupBy(x => x.totalweight).OrderBy(gp => gp.Count());
                if (groups.Count() > 1)
                {
                    root         = groups.First().First();
                    targetWeight = groups.Last().First().totalweight;
                }
                else
                {
                    var difference = root.totalweight - targetWeight;
                    result = (root.weight - difference).ToString();
                    break;
                }
            }
            return(result);
        }