Example #1
0
        public int NearestWireIntersectDist()
        {
            var minDist = int.MaxValue;
            var count   = 0;
            var curr    = new GridLoc();

            foreach (var move in Moves(_wire2))
            {
                for (int i = 0; i < move.Distance; i++)
                {
                    curr = curr.Move(move.Direction);
                    count++;

                    if (_grid1.ContainsKey(curr))
                    {
                        var dist = _grid1[curr] + count;
                        minDist = Math.Min(minDist, dist);
                    }
                }
            }
            return(minDist);
        }
Example #2
0
        public int NearestManhattanIntersectDist()
        {
            // Manhattan distance is simply the sume of differences between points for their
            // x and y coordinates.
            var minDist = int.MaxValue;
            var curr    = new GridLoc();

            foreach (var move in Moves(_wire2))
            {
                for (int i = 0; i < move.Distance; i++)
                {
                    curr = curr.Move(move.Direction);

                    if (_grid1.ContainsKey(curr))
                    {
                        var dist = Math.Abs(curr.X) + Math.Abs(curr.Y);
                        minDist = Math.Min(minDist, dist);
                    }
                }
            }
            return(minDist);
        }
Example #3
0
        private IDictionary <GridLoc, int> BuildGrid(string steps)
        {
            var curr   = new GridLoc();
            var result = new Dictionary <GridLoc, int>();
            int moves  = 0;

            result.Add(curr, moves++);
            foreach (var move in Moves(steps))
            {
                for (int i = 0; i < move.Distance; i++)
                {
                    curr = curr.Move(move.Direction);
                    if (!result.ContainsKey(curr))
                    {
                        result.Add(curr, moves++);
                    }
                    else
                    {
                        moves++;
                    }
                }
            }
            return(result);
        }