Exemplo n.º 1
0
        public static int GetDistanceToClosestIntersectionUsingSegments(List <string> movesLine1, List <string> movesLine2, out int minDistance, out Point nearestJoin, out List <Point> intersectionList)
        {
            var origin = new Point(0, 0);

            minDistance = int.MaxValue;
            nearestJoin = Point.Center;

            var segmentsLine1 = Day3Utils.GetSegments(new Point(0, 0), movesLine1);
            var segmentsLine2 = Day3Utils.GetSegments(new Point(0, 0), movesLine2);

            intersectionList = segmentsLine1
                               .SelectMany(l1 => segmentsLine2.Select(l1.GetIntersection))
                               .Where(p => p.X != 0 && p.Y != 0).ToList()
                               .ToList();


            foreach (var point in intersectionList)
            {
                var distance = Math.Abs(point.X - origin.X) + Math.Abs(point.Y - origin.Y);
                if (distance < minDistance)
                {
                    minDistance = distance;
                    nearestJoin = point;
                }
            }

            return(minDistance);
        }
Exemplo n.º 2
0
        public static int GetDistanceToClosestIntersectionUsingAllPoints(List <string> commands1, List <string> commands2, out int minDistance, out Point nearestJoin, out List <Point> joinList, out List <Point> positionsLine1, out List <Point> positionsLine2)
        {
            var origin = new Point(0, 0);

            minDistance = int.MaxValue;
            nearestJoin = Point.Center;

            positionsLine1 = Day3Utils.GetPath(origin, commands1);
            positionsLine2 = Day3Utils.GetPath(origin, commands2);

            var d2 = positionsLine2.ToHashSet();

            joinList = new List <Point>();
            Point tmp;

            for (int i = 0; i < positionsLine1.Count; i++)
            {
                tmp = positionsLine1[i];
                if (d2.Contains(tmp) && tmp.X != 0 && tmp.Y != 0)
                {
                    joinList.Add(tmp);
                }
            }

            foreach (var point in joinList)
            {
                var distance = Math.Abs(point.X - origin.X) + Math.Abs(point.Y - origin.Y);
                if (distance < minDistance)
                {
                    minDistance = distance;
                    nearestJoin = point;
                }
            }

            return(minDistance);
        }