Esempio n. 1
0
        private IEnumerable <Intersection> ParseWire(int id, string[] wireSegments)
        {
            List <Intersection> intersections = new List <Intersection>();
            Point from         = Point.Origin; // Start @(0,0)
            var   wireDistance = 0;

            foreach (var segment in wireSegments)
            {
                var length = int.Parse(segment.Substring(1));
                var to     = (segment[0]) switch
                {
                    'U' => new Point(from.x, from.y + length),
                    'D' => new Point(from.x, from.y - length),
                    'L' => new Point(from.x - length, from.y),
                    'R' => new Point(from.x + length, from.y),
                    _ => throw new ArgumentOutOfRangeException(nameof(segment)),
                };
                var seg = new WireSegment(id, from, to, wireDistance);
                wireDistance += length;                                                     // Add segment length to wire distance
                intersections.AddRange(segments
                                       .Where(s => s.wire != seg.wire && s.Intersects(seg)) // The same wire cannot intersect itself
                                       .Select(s => new Intersection(s, seg, s.IntersectsAt(seg))));
                segments.Add(seg);
                from = to;
            }
            return(intersections);
        }
Esempio n. 2
0
        public Point IntersectsAt(WireSegment other)
        {
            var y = from.y == to.y ? from.y : other.from.y; // use y from horizontal segment
            var x = from.x == to.x ? from.x : other.from.x; // use x from vertical segment

            return(new Point(x, y));
        }
Esempio n. 3
0
 public bool Intersects(WireSegment other)
 {
     if (from.Equals(Point.Origin) && other.from.Equals(Point.Origin))
     {
         return(false); // Do not count origin
     }
     return(!(Right < other.Left || Left > other.Right || Bottom > other.Top || Top < other.Bottom));
 }
Esempio n. 4
0
 public Intersection(WireSegment seg1, WireSegment seg2, Point intersection)
 {
     this.seg1         = seg1;
     this.seg2         = seg2;
     this.intersection = intersection;
 }