Exemple #1
0
        public void Add(Position4D p4)
        {
            if (_positions.None())
            {
                for (int i = 0; i < 4; i++)
                {
                    _min[i] = p4.Coordinates[i];
                    _max[i] = p4.Coordinates[i];
                }
            }
            else
            {
                for (int i = 0; i < 4; i++)
                {
                    if (_min[i] > p4.Coordinates[i])
                    {
                        _min[i] = p4.Coordinates[i];
                    }
                    if (_max[i] < p4.Coordinates[i])
                    {
                        _max[i] = p4.Coordinates[i];
                    }
                }
            }

            _positions.Add(p4);
        }
Exemple #2
0
        public int Distance(Position4D other)
        {
            int distance = 0;

            for (int i = 0; i < 4; i++)
            {
                distance += Math.Abs(Coordinates[i] - other.Coordinates[i]);
            }

            return(distance);
        }
Exemple #3
0
        public IEnumerable <Position4D> ParseInput(string filename)
        {
            foreach (var line in File.ReadAllLines(filename))
            {
                if (string.IsNullOrEmpty(line))
                {
                    continue;
                }
                var p4 = new Position4D();

                var coords = line.Split(",", StringSplitOptions.RemoveEmptyEntries).Select(x => x.Trim()).ToList();
                for (int i = 0; i < 4; i++)
                {
                    p4.Coordinates[i] = int.Parse(coords[i]);
                }

                yield return(p4);
            }
        }
Exemple #4
0
        public bool ShouldConnect(Position4D p4)
        {
            int offset = 3;

            bool allMinInRange = true;
            bool allMaxInRange = true;

            for (int i = 0; i < 4; i++)
            {
                if (_min[i] - offset > p4.Coordinates[i])
                {
                    allMinInRange = false;
                }
                if (_max[i] + offset < p4.Coordinates[i])
                {
                    allMinInRange = false;
                }
            }

            // Outside bounding box
            if (!allMaxInRange || !allMinInRange)
            {
                return(false);
            }

            foreach (var p in _positions)
            {
                // Found one position close enough, connect
                if (p4.Distance(p) <= offset)
                {
                    return(true);
                }
            }

            // No existing position was close enough
            return(false);
        }