Ejemplo n.º 1
0
        public string GetSolution()
        {
            int result = int.MaxValue;

            string[] datalines = PuzzleHelper.ReadPuzzleDataFile("Day3Part1.txt");

            var wire1 = new Wire();

            wire1.GeneratePath(datalines[0]);

            var wire2 = new Wire();

            wire2.GeneratePath(datalines[1]);

            var intersections = wire1.Path.Intersect(wire2.Path);

            foreach (var intersection in intersections)
            {
                if (intersection.X == 0 && intersection.Y == 0)
                {
                    continue;
                }

                int wire1IntersectionIndex = wire1.Path.FindIndex(x => x.X == intersection.X && x.Y == intersection.Y);
                int wire2IntersectionIndex = wire2.Path.FindIndex(x => x.X == intersection.X && x.Y == intersection.Y);
                int totalSteps             = wire1IntersectionIndex + wire2IntersectionIndex;

                if (totalSteps < result)
                {
                    result = totalSteps;
                }
            }

            return(result.ToString());
        }
Ejemplo n.º 2
0
        public string GetSolution()
        {
            string[] filelines = PuzzleHelper.ReadPuzzleDataFile("Day2Part1.txt");
            var      opcodes   = new List <int>();

            foreach (string line in filelines)
            {
                opcodes.AddRange(line.Split(",").Select(opcode => Convert.ToInt32(opcode)));
            }

            // Brute force the value
            var program = new IntcodeProgram(opcodes.ToArray());

            for (int noun = 0; noun <= 99; noun++)
            {
                for (int verb = 0; verb <= 99; verb++)
                {
                    int result = program.RunProgram(noun, verb);

                    if (result == EXPECTED_OUTPUT)
                    {
                        return((100 * noun + verb).ToString());
                    }
                    else
                    {
                        program.ResetProgramMemory();
                    }
                }
            }

            throw new InvalidOperationException("Result not found!");
        }
Ejemplo n.º 3
0
        public string GetSolution()
        {
            int result = int.MaxValue;

            string[] datalines = PuzzleHelper.ReadPuzzleDataFile("Day3Part1.txt");

            var wire1 = new Wire();

            wire1.GeneratePath(datalines[0]);

            var wire2 = new Wire();

            wire2.GeneratePath(datalines[1]);

            var intersections = wire1.Path.Intersect(wire2.Path);

            foreach (var intersection in intersections)
            {
                if (intersection.X == 0 && intersection.Y == 0)
                {
                    continue;
                }

                int length = Convert.ToInt32(Math.Abs(intersection.X) + Math.Abs(intersection.Y));
                if (result >= length)
                {
                    result = length;
                }
            }

            return(result.ToString());
        }
Ejemplo n.º 4
0
        public string GetSolution()
        {
            int totalFuelNeeded = 0;

            foreach (var line in PuzzleHelper.ReadPuzzleDataFile("Day1Part1.txt"))
            {
                int fuelNeededForModule = CalculateFuel(Convert.ToInt32(line));

                bool fuelNeededForFuelMass = true;
                int  additionalFuel        = fuelNeededForModule;
                while (fuelNeededForFuelMass)
                {
                    additionalFuel = CalculateFuel(additionalFuel);

                    if (additionalFuel <= 0)
                    {
                        fuelNeededForFuelMass = false;
                    }
                    else
                    {
                        fuelNeededForModule += additionalFuel;
                    }
                }

                totalFuelNeeded += fuelNeededForModule;
            }

            return(totalFuelNeeded.ToString());
        }
Ejemplo n.º 5
0
        public string GetSolution()
        {
            var asteroidBelt = new AsteroidBelt();

            string[] map = PuzzleHelper.ReadPuzzleDataFile("Day10Part1.txt");
            for (int lineIndex = 0; lineIndex < map.Length; lineIndex++)
            {
                char[] columns = map[lineIndex].ToCharArray();

                for (int columnIndex = 0; columnIndex < columns.Length; columnIndex++)
                {
                    if (columns[columnIndex] == '#')
                    {
                        asteroidBelt.AddAsteroid(new Vector2(columnIndex, lineIndex));
                    }
                }
            }

            KeyValuePair <Vector2, int> bestLocation    = asteroidBelt.FindSuitableMonitoringStationLocation();
            Dictionary <int, Vector2>   vaporationOrder = asteroidBelt.StartVaporization(bestLocation.Key);

            Vector2 puzzleOucome = vaporationOrder[200];

            return(((puzzleOucome.X * 100) + puzzleOucome.Y).ToString());
        }
Ejemplo n.º 6
0
        public string GetSolution()
        {
            string[] lines = PuzzleHelper.ReadPuzzleDataFile("Day8Part1.txt");

            List <Layer> layers = ParseStringToLayers(lines);
            var          layerWithLowestNumber = layers.OrderBy(layer => layer.GetNumberOfPixels(0)).First();

            return((layerWithLowestNumber.GetNumberOfPixels(1) * layerWithLowestNumber.GetNumberOfPixels(2)).ToString());
        }
Ejemplo n.º 7
0
        public string GetSolution()
        {
            int totalFuelNeeded = 0;

            foreach (var line in PuzzleHelper.ReadPuzzleDataFile("Day1Part1.txt"))
            {
                totalFuelNeeded += Convert.ToInt32(Math.Floor(Convert.ToDecimal(line) / 3)) - 2;
            }
            return(totalFuelNeeded.ToString());
        }
Ejemplo n.º 8
0
        public string GetSolution()
        {
            string[] orbits = PuzzleHelper.ReadPuzzleDataFile("Day6Part1.txt");

            var orbitalMap = new OrbitalMap();

            orbitalMap.GenerateMap(orbits);

            return(orbitalMap.GetTotalOrbits().ToString());
        }
Ejemplo n.º 9
0
        public string GetSolution()
        {
            string[] filelines = PuzzleHelper.ReadPuzzleDataFile("Day5Part1.txt");
            var      opcodes   = new List <int>();

            foreach (string line in filelines)
            {
                opcodes.AddRange(line.Split(",").Select(opcode => Convert.ToInt32(opcode)));
            }

            var instance = new IntcodeComputer.IntcodeComputer();

            return(instance.RunProgram(opcodes.ToArray(), new int[] { 5 }).ToString());
        }
Ejemplo n.º 10
0
        public string GetSolution()
        {
            string[] orbits = PuzzleHelper.ReadPuzzleDataFile("Day6Part1.txt");

            var orbitalMap = new OrbitalMap();

            orbitalMap.GenerateMap(orbits);

            var you   = orbitalMap.Planets.Single(planet => planet.Name == "YOU");
            var santa = orbitalMap.Planets.Single(planet => planet.Name == "SAN");

            int totalTransfers = orbitalMap.GetNumberOfOrbitalTransfersBetween(you, santa);

            // Don't count the jumps for you and santa
            totalTransfers -= 2;
            return(totalTransfers.ToString());
        }
Ejemplo n.º 11
0
        public string GetSolution()
        {
            string[] lines = PuzzleHelper.ReadPuzzleDataFile("Day8Part1.txt");

            List <Layer>  layers = ParseStringToLayers(lines);
            StringBuilder sb     = new StringBuilder();

            int layerWidth  = 25;
            int layerHeight = 6;

            for (var layerRowIndex = 0; layerRowIndex < layerHeight; layerRowIndex++)
            {
                sb.AppendLine();
                for (var layerColumnIndex = 0; layerColumnIndex < layerWidth; layerColumnIndex++)
                {
                    var colorFound = false;
                    foreach (var layer in layers)
                    {
                        var location = new Vector2(layerRowIndex, layerColumnIndex);
                        if (layer.GetPixelValueAt(location) == 0)
                        {
                            sb.Append("-");
                            colorFound = true;
                            break;
                        }

                        if (layer.GetPixelValueAt(location) == 1)
                        {
                            sb.Append("#");
                            colorFound = true;
                            break;
                        }
                    }

                    if (!colorFound)
                    {
                        sb.Append(" ");
                    }
                }
            }

            return(sb.ToString());
        }
Ejemplo n.º 12
0
        public string GetSolution()
        {
            var asteroidBelt = new AsteroidBelt();

            string[] map = PuzzleHelper.ReadPuzzleDataFile("Day10Part1.txt");
            for (int lineIndex = 0; lineIndex < map.Length; lineIndex++)
            {
                char[] columns = map[lineIndex].ToCharArray();

                for (int columnIndex = 0; columnIndex < columns.Length; columnIndex++)
                {
                    if (columns[columnIndex] == '#')
                    {
                        asteroidBelt.AddAsteroid(new Vector2(columnIndex, lineIndex));
                    }
                }
            }

            return(asteroidBelt.FindSuitableMonitoringStationLocation().Value.ToString());
        }
Ejemplo n.º 13
0
        public int CalculateOutputSignal(int inputSignal, IEnumerable <int> usedPhaseSettings = null)
        {
            string[] filelines   = PuzzleHelper.ReadPuzzleDataFile("Day7Part1.txt");
            var      opcodes     = new List <int>();
            int      totalOutput = 0;

            // Make sure the usedPhaseSettings array exists
            if (usedPhaseSettings == null)
            {
                usedPhaseSettings = new int[] {};
            }

            foreach (string line in filelines)
            {
                opcodes.AddRange(line.Split(",").Select(opcode => Convert.ToInt32(opcode)));
            }

            for (int phaseSetting = 0; phaseSetting <= 4; phaseSetting++)
            {
                if (usedPhaseSettings.Contains(phaseSetting))
                {
                    // Phase setting already used. Skip...
                    continue;
                }

                var computer = new IntcodeComputer.IntcodeComputer();
                int output   = computer.RunProgram(opcodes.ToList().ToArray(), new int[] { phaseSetting, inputSignal });

                if (_nextAmplifier != null)
                {
                    output = _nextAmplifier.CalculateOutputSignal(output, usedPhaseSettings.Concat(new int[] { phaseSetting }));
                }

                if (output > totalOutput)
                {
                    totalOutput = output;
                }
            }

            return(totalOutput);
        }
Ejemplo n.º 14
0
        public string GetSolution()
        {
            string[] filelines = PuzzleHelper.ReadPuzzleDataFile("Day2Part1.txt");
            var      opcodes   = new List <int>();

            foreach (string line in filelines)
            {
                opcodes.AddRange(line.Split(",").Select(opcode => Convert.ToInt32(opcode)));
            }

            // Alter some values
            opcodes[1] = 12;
            opcodes[2] = 2;

            for (int position = 0; position < opcodes.Count; position += 4)
            {
                int command             = opcodes[position];
                int firstValuePosition  = opcodes[position + 1];
                int secondValuePosition = opcodes[position + 2];
                int resultValuePosition = opcodes[position + 3];

                // Stop the program and return the value on position 0
                if (command == 99)
                {
                    return(opcodes[0].ToString());
                }

                // Add the first and second value and store the result in the correct location
                if (command == 1)
                {
                    opcodes[resultValuePosition] = opcodes[firstValuePosition] + opcodes[secondValuePosition];
                }

                if (command == 2)
                {
                    opcodes[resultValuePosition] = opcodes[firstValuePosition] * opcodes[secondValuePosition];
                }
            }

            throw new InvalidOperationException("No result found while running the Intcode program");
        }