示例#1
0
        public static void Run(string[] input)
        {
            D7Graph g    = new D7Graph(input);
            string  root = part01(g);

            Console.WriteLine("Part 1: " + root);

            Console.WriteLine(get_unbalanced(g, g.children("ugml")));
        }
示例#2
0
        static bool children_unbalanced(D7Graph g, string root)
        {
            HashSet <int> ws = new HashSet <int>();

            foreach (string c in g.children(root))
            {
                ws.Add(g.weight(c));
            }

            return(ws.Count > 1);
        }
示例#3
0
        static string part01(D7Graph g)
        {
            foreach (string n in g.nodes.Keys)
            {
                if (g.reachable(n) == g.nodes.Count)
                {
                    return(n);
                }
            }

            return("??");
        }
示例#4
0
        static int part02(D7Graph g, string root)
        {
            bool found = false;

            while (!found)
            {
                string[] targets = g.children(root);

                string unbalanced = get_unbalanced(g, targets);

                if (unbalanced == null)
                {
                    found = true;
                }
                else
                {
                    root = unbalanced;
                }
            }
        }
示例#5
0
        static string get_unbalanced(D7Graph g, string[] cs)
        {
            var q = cs.GroupBy(
                str => g.weight(str),
                str => str,
                (w, n) => new
            {
                Key   = w,
                Count = n.Count(),
                Res   = n.Min()
            });

            foreach (var res in q)
            {
                if (res.Count == 1)
                {
                    return(res.Res);
                }
            }

            return(null);
        }