Ejemplo n.º 1
0
        private static void Main()
        {
            var input = System.IO.File.ReadAllLines("Input.txt");

            Console.WriteLine($"Day 3 :: Part 1 - {Part1.Calculate(input)}");
            Console.WriteLine($"Day 3 :: Part 2 - {Part2.Calculate(input)}");
        }
Ejemplo n.º 2
0
        static void Main(string[] args)
        {
            Part2 part = new Part2();

            int x    = 0;
            int y    = 0;
            int step = 0;

            string textFile = @"C:\Users\krispy\source\repos\advent-of-code-2019\Day3\input.txt";

            string[] wires = File.ReadAllLines(textFile);

            string[] wire1 = wires[0].Split(',');
            string[] wire2 = wires[1].Split(',');

            // This is the origin
            Coordinate coordinate = new Coordinate(x, y, "A", step);

            foreach (string instruction in wire1)
            {
                coordinate = part.GenerateCoordinates(coordinate.X, coordinate.Y, instruction, coordinate.WireName, coordinate.Step);
                x          = coordinate.X;
                y          = coordinate.Y;
                step       = part.coordinates[coordinate];
            }


            // Reset coordinates for second wire

            coordinate = new Coordinate(0, 0, "B", 0);
            foreach (string instruction in wire2)
            {
                coordinate = part.GenerateCoordinates(coordinate.X, coordinate.Y, instruction, coordinate.WireName, coordinate.Step);
                x          = coordinate.X;
                y          = coordinate.Y;
                step       = coordinate.Step;
            }
            Console.WriteLine("Generated {0} Coordinates for Wire A", part.coordinates.Count);
            Console.WriteLine("Found {0} crossings", part.crossings.Count);

            int        smallestDistance   = int.MaxValue;
            Coordinate smallestCoordinate = new Coordinate(0, 0, "A", 0);

            foreach (Coordinate c in part.crossings)
            {
                int distance = c.CalculateDistanceFrom0();
                if (distance < smallestDistance)
                {
                    smallestDistance   = distance;
                    smallestCoordinate = c;
                }
            }

            // For Part 1
            Console.WriteLine("Smallest Distance: {0} \n Coordinates <{1},{2}>", smallestDistance, smallestCoordinate.X, smallestCoordinate.Y);

            int leastSteps = int.MaxValue;
            int OutSteps;

            foreach (Coordinate c in part.crossings)
            {
                bool foundCoordinate = part.coordinates.TryGetValue(c, out OutSteps);
                if (foundCoordinate && OutSteps + c.Step < leastSteps)
                {
                    leastSteps = OutSteps + c.Step;
                }
            }

            // For Part 2
            Console.WriteLine("Least amount of steps: {0}", leastSteps);

            Console.ReadKey();
        }