Exemple #1
0
        string Pipe(string input, bool part2)
        {
            var progs  = new List <Prog>();
            var inputs = input.Split('\n');

            foreach (var i in inputs)
            {
                var id   = Convert.ToInt32(i.Substring(0, i.IndexOf(' ')));
                var link = i.Substring(i.IndexOf('>') + 1).Trim().Split(',').Select(x => Convert.ToInt32(x)).ToList();
                var p    = new Prog(id, link);
                progs.Add(p);
            }

            var  groups = 0;
            bool part1  = true;

            while (part2 && progs.Count > 0 || part1)
            {
                var linked = new List <Prog>()
                {
                    progs[0]
                };
                var next = new List <int>();
                next.AddRange(progs[0].Links);
                while (next.Count > 0)
                {
                    var id = next[0];
                    try
                    {
                        var currentProgram = progs.Where(x => x.ID == id).SingleOrDefault();
                        if (!linked.Contains(currentProgram))
                        {
                            linked.Add(currentProgram);
                            next.AddRange(currentProgram.Links);
                        }
                        next.RemoveAt(0);
                        if (part2)
                        {
                            progs.Remove(currentProgram);
                        }
                    }
                    catch
                    {
                        next.RemoveAt(0);
                    }
                }
                if (!part2)
                {
                    groups = linked.Count;
                }
                else
                {
                    groups += 1;
                }
                part1 = false;
            }

            return(groups.ToString());
        }
Exemple #2
0
        /* Recrursively searches for the last prog node in a branch
         * where the total weight doesn't match the total weight of its
         * sibling nodes.
         */
        private Prog FindBadWeight(Prog root)
        {
            if (root.Children.Count == 0)
            {
                return(root);
            }
            root.Children.Sort((a, b) => a.TotalWeight - b.TotalWeight);

            Prog badChild = null;

            if (root.Children[0].TotalWeight != root.Children[1].TotalWeight)
            {
                badChild = root.Children[0];
            }
            else if (root.Children[^ 1].TotalWeight != root.Children[^ 2].TotalWeight)
Exemple #3
0
        public override object Task2()
        {
            Prog root = FillTree(rootName, null);

            Prog badProg = FindBadWeight(root);

            foreach (Prog p in badProg.Parent.Children)
            {
                if (p == badProg)
                {
                    continue;
                }
                int weightDelta = badProg.TotalWeight - p.TotalWeight;
                return(badProg.Weight - weightDelta);
            }
            return(null);
        }