예제 #1
0
        public static int RunPart2(string[] wire1, string[] wire2)
        {
            var start  = new Point();
            var lines1 = new List <Line>();

            foreach (var command in wire1)
            {
                var end = CrossedWires.Move(start, command);
                lines1.Add(new Line(start, end));
                start = end;
            }

            start = new Point();
            var lines2    = new List <Line>();
            var distances = new List <int>();
            var points    = new List <Point>();

            foreach (var command in wire2)
            {
                var end   = CrossedWires.Move(start, command);
                var line2 = new Line(start, end);
                foreach (var line1 in lines1)
                {
                    try
                    {
                        var point = line1.IntersectWith(line2);
                        if (CrossedWires.IsInPoints(points, point))
                        {
                            continue;
                        }

                        points.Add(point);
                        distances.Add(
                            CrossedWires.GetDistanceFromOrigin(point, lines2) +
                            CrossedWires.GetDistanceFromOrigin(point, lines1, line1)
                            );
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e);
                    }
                }

                lines2.Add(line2);
                start = end;
            }

            distances.Remove(0);
            return(distances.Min());
        }
예제 #2
0
        public static int RunPart1(string[] wire1, string[] wire2)
        {
            var start = new Point();
            var lines = new List <Line>();

            foreach (var command in wire1)
            {
                var end = CrossedWires.Move(start, command);
                lines.Add(new Line(start, end));
                start = end;
            }

            start = new Point();
            var distances = new List <int>();

            foreach (var command in wire2)
            {
                var end     = CrossedWires.Move(start, command);
                var tmpLine = new Line(start, end);
                foreach (var line in lines)
                {
                    try
                    {
                        var point = line.IntersectWith(tmpLine);

                        distances.Add(point.GetDistanceFromOrigin());
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e);
                    }
                }

                start = end;
            }

            distances.Remove(0);

            return(distances.Min());
        }