Exemplo n.º 1
0
 private List <ChronoPoint> InputsToPoints(string[] inputs)
 {
     return(inputs
            .Where(input => string.IsNullOrEmpty(input) == false)
            .Select(input => ChronoPoint.FromString(input))
            .ToList());
 }
Exemplo n.º 2
0
        private int AreaInThresold(List <ChronoPoint> points, int distanceThresold)
        {
            int minX = points.Min(p => p.X) - 1;
            int maxX = points.Max(p => p.X) + 1;
            int minY = points.Min(p => p.Y) - 1;
            int maxY = points.Max(p => p.Y) + 1;

            int         areaInRange   = 0;
            ChronoPoint samplingPoint = new ChronoPoint();

            for (int i = minX; i <= maxX; i++)
            {
                for (int j = minY; j <= maxY; j++)
                {
                    samplingPoint.X = i;
                    samplingPoint.Y = j;

                    int distanceSum = points.Sum(p => ChronoPoint.ManhattanDistance(samplingPoint, p));
                    if (distanceSum < distanceThresold)
                    {
                        areaInRange++;
                    }
                }
            }

            return(areaInRange);
        }
Exemplo n.º 3
0
        public static ChronoPoint FromString(string strPoint)
        {
            if (string.IsNullOrEmpty(strPoint))
            {
                return(null);
            }
            string[] parts = strPoint.Split(new string[] { ", ", }, StringSplitOptions.RemoveEmptyEntries);
            if (parts.Length < 2)
            {
                return(null);
            }
            ChronoPoint point = new ChronoPoint
            {
                X = Convert.ToInt32(parts[0]),
                Y = Convert.ToInt32(parts[1]),
            };

            return(point);
        }
Exemplo n.º 4
0
        public static int ManhattanDistance(ChronoPoint p0, ChronoPoint p1)
        {
            int distance = Math.Abs(p1.X - p0.X) + Math.Abs(p1.Y - p0.Y);

            return(distance);
        }
Exemplo n.º 5
0
        public string ResolvePart1(string[] inputs)
        {
            List <ChronoPoint>    points      = InputsToPoints(inputs);
            Dictionary <int, int> pointsAreas = new Dictionary <int, int>();

            for (int i = 0; i < points.Count; i++)
            {
                pointsAreas.Add(i, 0);
            }

            int minX = points.Min(p => p.X) - 1;
            int maxX = points.Max(p => p.X) + 1;
            int minY = points.Min(p => p.Y) - 1;
            int maxY = points.Max(p => p.Y) + 1;

            ChronoPoint samplingPoint = new ChronoPoint();

            for (int i = minX; i <= maxX; i++)
            {
                for (int j = minY; j <= maxY; j++)
                {
                    samplingPoint.X = i;
                    samplingPoint.Y = j;
                    bool isEdge = i == minX || i == maxX || j == minY || j == maxY;

                    int idxMin      = -1;
                    int distanceMin = int.MaxValue;
                    for (int idx = 0; idx < points.Count; idx++)
                    {
                        int distance = ChronoPoint.ManhattanDistance(samplingPoint, points[idx]);
                        if (distance == distanceMin)
                        {
                            idxMin = -1;
                        }
                        else if (distance < distanceMin)
                        {
                            distanceMin = distance;
                            idxMin      = idx;
                        }
                    }
                    if (idxMin < 0)
                    {
                        continue;
                    }

                    if (isEdge)
                    {
                        pointsAreas[idxMin] = -1;
                    }
                    else
                    {
                        int previousArea = pointsAreas[idxMin];
                        if (previousArea >= 0)
                        {
                            pointsAreas[idxMin] = previousArea + 1;
                        }
                    }
                }
            }

            int maxArea = pointsAreas.Max(p => p.Value);

            return(maxArea.ToString());
        }