Beispiel #1
0
        static void Part1(string[] input)
        {
            string  wire1             = input[1];
            var     wire1instructions = wire1.Split(",").Select(x => new Instruction(x));
            LineSet wire1path         = GetLines(wire1instructions);
            string  wire2             = input[0];
            var     wire2instructions = wire2.Split(",").Select(x => new Instruction(x));
            LineSet wire2path         = GetLines(wire2instructions);
            var     intersects1       = GetIntersections(wire1path.Horizontals.Cast <LineBase>().ToList(), wire2path.Verticals.Cast <LineBase>().ToList());
            var     intersects2       = GetIntersections(wire1path.Verticals.Cast <LineBase>().ToList(), wire2path.Horizontals.Cast <LineBase>().ToList());
            int     minDistance1      = (from item in intersects1
                                         select Math.Abs(item.coordinate.x) + Math.Abs(item.coordinate.y)).Min();
            int minDistance2 = (from item in intersects2
                                select Math.Abs(item.coordinate.x) + Math.Abs(item.coordinate.y)).Min();

            Console.WriteLine(minDistance1);
            Console.WriteLine(minDistance2);
        }
Beispiel #2
0
        static void Part2(string[] input)
        {
            string     wire1             = input[1];
            var        wire1instructions = wire1.Split(",").Select(x => new Instruction(x));
            LineSet    wire1path         = GetLines(wire1instructions);
            string     wire2             = input[0];
            var        wire2instructions = wire2.Split(",").Select(x => new Instruction(x));
            LineSet    wire2path         = GetLines(wire2instructions);
            var        intersects1       = GetIntersections(wire1path.Horizontals.Cast <LineBase>().ToList(), wire2path.Verticals.Cast <LineBase>().ToList());
            var        intersects2       = GetIntersections(wire1path.Verticals.Cast <LineBase>().ToList(), wire2path.Horizontals.Cast <LineBase>().ToList());
            var        allIntersects     = intersects1.Concat(intersects2);
            List <int> wireDistances     = new List <int>();

            foreach (Intersect intersect in allIntersects)
            {
                wireDistances.Add(getTotalDistance(intersect, wire1path.Lines, wire2path.Lines));
            }
            Console.WriteLine(wireDistances.Min());
        }
Beispiel #3
0
        static LineSet GetLines(IEnumerable <Instruction> instructions)
        {
            LineSet    results = new LineSet();
            Coordinate start   = new Coordinate()
            {
                x = 0, y = 0
            };
            Coordinate end   = new Coordinate();
            int        index = 0;

            foreach (var instruction in instructions)
            {
                switch (instruction.direction)
                {
                case "U":
                    end = new Coordinate()
                    {
                        x = start.x, y = start.y + instruction.distance
                    };
                    results.Verticals.Add(new Vertical()
                    {
                        x = start.x, y1 = start.y, y2 = end.y, index = index
                    });

                    break;

                case "D":
                    end = new Coordinate()
                    {
                        x = start.x, y = start.y - instruction.distance
                    };
                    results.Verticals.Add(new Vertical()
                    {
                        x = start.x, y1 = start.y, y2 = end.y, index = index
                    });

                    break;

                case "L":
                    end = new Coordinate()
                    {
                        x = start.x - instruction.distance, y = start.y
                    };
                    results.Horizontals.Add(new Horizontal()
                    {
                        y = start.y, x1 = start.x, x2 = end.x, index = index
                    });

                    break;

                case "R":
                    end = new Coordinate()
                    {
                        x = start.x + instruction.distance, y = start.y
                    };
                    results.Horizontals.Add(new Horizontal()
                    {
                        y = start.y, x1 = start.x, x2 = end.x, index = index
                    });

                    break;

                default:
                    throw new InvalidCastException("Direction not found " + instruction.direction);
                }
                //Console.WriteLine(string.Format("{0} to {1}",start.ToString(),end.ToString()));
                index++;
                start = end;
            }
            return(results);
        }