Пример #1
0
        public Day22Grid BuildNetwork(string[] instructions)
        {
            var result = new Day22Grid();

            var network = instructions.Select(BuildNode).ToList();

            result.Nodes = network;
            var pairs = ViablePairs(null, result);

            var adjacentPairs = pairs.Where(tuple =>
            {
                var first  = tuple.Item1;
                var second = tuple.Item2;

                //If one dimension varies we have constrain the other.
                return((first.X == second.X && (first.Y == second.Y + 1 || first.Y == second.Y - 1)) ||
                       (first.Y == second.Y && (first.X == second.X + 1 || first.X == second.X - 1)));
            }).ToList();

            result.AdjacentPairs = adjacentPairs;

            return(result);
        }
Пример #2
0
        public List <Tuple <Day22Node, Day22Node> > ViablePairs(string[] input = null, Day22Grid network = null)
        {
            //Node A is not empty (its Used is not zero).
            //Nodes A and B are not the same node.
            //The data on node A(its Used) would fit on node B(its Avail).
            //How many viable pairs of nodes are there?
            if (input == null)
            {
                input = FileReader.ReadFile("day 22.txt").Skip(2).ToArray();
            }
            if (network == null)
            {
                network = BuildNetwork(input);
            }

            var pairs = new List <Tuple <Day22Node, Day22Node> >();

            foreach (var a in network.Nodes)
            {
                foreach (var b in network.Nodes)
                {
                    if (a == b)
                    {
                        continue;
                    }
                    if (a.Used == 0)
                    {
                        continue;
                    }
                    if (a.Used > b.Space)
                    {
                        continue;
                    }
                    pairs.Add(new Tuple <Day22Node, Day22Node>(a, b));
                    a.Connections++;
                    b.Connections++;
                }
            }

            return(pairs);
        }