Ejemplo n.º 1
0
        /// <summary>
        /// The second puzzle.
        /// </summary>
        /// <param name="input">The AOCInput object for getting the input.</param>
        /// <returns>The answer.</returns>
        public override object Puzzle2(AOCInput input)
        {
            int[] range = input.GetInput("-").Select(x => int.Parse(x)).ToArray();

            int count = 0;

            for (int i = range[0]; i < range[1]; ++i)
            {
                string password = i.ToString();

                bool ascending = password.Zip(password.Skip(1), (a, b) => a <= b).All(x => x);
                bool same      = false;

                for (int j = 0; j < password.Length - 1; ++j)
                {
                    if (password[j] == password[j + 1])
                    {
                        if ((j + 2 >= password.Length || password[j] != password[j + 2]) &&
                            (j - 1 < 0 || password[j] != password[j - 1]))
                        {
                            same = true;
                            break;
                        }
                    }
                }

                if (same && ascending)
                {
                    count++;
                }
            }

            return(count);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// The first puzzle.
        /// </summary>
        /// <param name="input">The AOCInput object for getting the input.</param>
        /// <returns>The answer.</returns>
        public override object Puzzle1(AOCInput input)
        {
            int[] software = input.GetInput(",").Select(x => int.Parse(x)).ToArray();

            IntcodeComputer computer;

            int[] sequence     = new int[] { 0, 1, 2, 3, 4 };
            var   permutations = GetPermutations <int>(sequence, 5);

            long maxOutput   = 0;
            int  maxSequence = 0;

            for (int i = 0; i < permutations.Count(); ++i)
            {
                long output = 0;

                for (int j = 0; j < 5; ++j)
                {
                    computer = new IntcodeComputer((long[])software.Clone(), false);
                    computer.Run(permutations.ElementAt(i).ElementAt(j), output);
                    output = computer.Outputs.Last();
                }

                if (output > maxOutput)
                {
                    maxOutput   = output;
                    maxSequence = i;
                }
            }

            return(maxOutput);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// The second puzzle.
        /// </summary>
        /// <param name="input">The AOCInput object for getting the input.</param>
        /// <returns>The answer.</returns>
        public override object Puzzle2(AOCInput input)
        {
            long[] program = input.GetInput(",").Select(x => long.Parse(x)).ToArray();

            IntcodeComputer computer = new IntcodeComputer(program);
            computer.Run(5);

            return computer.Outputs[0];
        }
Ejemplo n.º 4
0
        /// <summary>
        /// The second puzzle.
        /// </summary>
        /// <param name="input">The AOCInput object for getting the input.</param>
        /// <returns>The answer.</returns>
        public override object Puzzle2(AOCInput input)
        {
            long[] software = input.GetInput(",").Select(x => long.Parse(x)).ToArray();

            IntcodeComputer computer = new IntcodeComputer(software, false);

            computer.Run(2);

            return(computer.Outputs.Last());
        }
Ejemplo n.º 5
0
        /// <summary>
        /// The first puzzle.
        /// </summary>
        /// <param name="input">The AOCInput object for getting the input.</param>
        /// <returns>The answer.</returns>
        public override object Puzzle1(AOCInput input)
        {
            int[] program = input.GetInput(",").Select(x => int.Parse(x)).ToArray();

            program[1] = 12;
            program[2] = 2;

            IntcodeComputer computer = new IntcodeComputer(program);

            return(computer.Run());
        }
Ejemplo n.º 6
0
        /// <summary>
        /// The second puzzle.
        /// </summary>
        /// <param name="input">The AOCInput object for getting the input.</param>
        /// <returns>The answer.</returns>
        public override object Puzzle2(AOCInput input)
        {
            int[] software = input.GetInput(",").Select(x => int.Parse(x)).ToArray();

            IntcodeComputer[] computer = new IntcodeComputer[5];

            int[] sequence     = new int[] { 5, 6, 7, 8, 9 };
            var   permutations = GetPermutations <int>(sequence, 5);

            long maxOutput   = 0;
            int  maxSequence = 0;

            for (int i = 0; i < permutations.Count(); ++i)
            {
                long output = 0;

                // Initialize computers.
                for (int j = 0; j < computer.Length; ++j)
                {
                    computer[j] = new IntcodeComputer((long[])software.Clone(), false);
                    computer[j].Run(permutations.ElementAt(i).ElementAt(j), output);
                    output = computer[j].Outputs.Last();
                }

                int curComp = 0;

                // Computer feedback loop.
                while (true)
                {
                    if (computer[curComp].Halted)
                    {
                        break;
                    }

                    computer[curComp].Run(output);
                    output = computer[curComp].Outputs.Last();

                    curComp = (curComp + 1) % 5;
                }

                // Check if output is highest.
                if (output > maxOutput)
                {
                    maxOutput   = output;
                    maxSequence = i;
                }
            }

            return(maxOutput);
        }
Ejemplo n.º 7
0
        /// <summary>
        /// The second puzzle.
        /// </summary>
        /// <param name="input">The AOCInput object for getting the input.</param>
        /// <returns>The answer.</returns>
        public override object Puzzle2(AOCInput input)
        {
            int[] data = input.GetInput().ToCharArray().Select(x => int.Parse(x.ToString())).ToArray();

            int width  = 25;
            int height = 6;

            int[][] layers = new int[data.Length / (width * height)][];

            // Copy layer data.
            for (int i = 0; i < layers.Length; ++i)
            {
                layers[i] = new int[width * height];
                Array.Copy(data, i * (width * height), layers[i], 0, width * height);
            }

            int[] image = new int[width * height];

            // Get image data by combining the layers.
            for (int i = 0; i < width * height; ++i)
            {
                for (int j = 0; j < layers.Length; ++j)
                {
                    if (layers[j][i] != 2)
                    {
                        image[i] = layers[j][i];
                        break;
                    }
                }
            }

            // Visualize the image in the console.
            for (int i = 0; i < height; ++i)
            {
                for (int j = 0; j < width; ++j)
                {
                    Console.Write(image[j + width * i] == 0 ? " " : "X");
                }

                Console.WriteLine();
            }

            return("See console ouptut.");
        }
Ejemplo n.º 8
0
        /// <summary>
        /// The first puzzle.
        /// </summary>
        /// <param name="input">The AOCInput object for getting the input.</param>
        /// <returns>The answer.</returns>
        public override object Puzzle1(AOCInput input)
        {
            int[] range = input.GetInput("-").Select(x => int.Parse(x)).ToArray();

            int count = 0;

            for (int i = range[0]; i < range[1]; ++i)
            {
                string password = i.ToString();

                bool ascending = password.Zip(password.Skip(1), (a, b) => a <= b).All(x => x);
                bool same      = password.Zip(password.Skip(1), (a, b) => a == b).Any(x => x);

                if (same && ascending)
                {
                    count++;
                }
            }

            return(count);
        }
Ejemplo n.º 9
0
        /// <summary>
        /// The first puzzle.
        /// </summary>
        /// <param name="input">The AOCInput object for getting the input.</param>
        /// <returns>The answer.</returns>
        public override object Puzzle1(AOCInput input)
        {
            int[] data = input.GetInput().ToCharArray().Select(x => int.Parse(x.ToString())).ToArray();

            int width  = 25;
            int height = 6;

            int[][] layers = new int[data.Length / (width * height)][];

            // Copy layer data.
            for (int i = 0; i < layers.Length; ++i)
            {
                layers[i] = new int[width * height];
                Array.Copy(data, i * (width * height), layers[i], 0, width * height);
            }

            // Get layer with least zeroes.
            int[] leastZeroes = layers.OrderBy(image => image.Count(x => x == 0)).First();

            return(leastZeroes.Count(x => x == 1) * leastZeroes.Count(x => x == 2));
        }
Ejemplo n.º 10
0
        /// <summary>
        /// The second puzzle.
        /// </summary>
        /// <param name="input">The AOCInput object for getting the input.</param>
        /// <returns>The answer.</returns>
        public override object Puzzle2(AOCInput input)
        {
            int[] program = input.GetInput(",").Select(x => int.Parse(x)).ToArray();

            for (int noun = 0; noun < 99; ++noun)
            {
                for (int verb = 0; verb < 99; ++verb)
                {
                    program[1] = noun;
                    program[2] = verb;

                    IntcodeComputer computer = new IntcodeComputer((int[])program.Clone());
                    int             output   = computer.Run();

                    if (output == 19690720)
                    {
                        return(100 * noun + verb);
                    }
                }
            }

            return("Program failed!");
        }
Ejemplo n.º 11
0
    private void CreateTaskForDay(int currentDay)
    {
        var input = AOCInput.GetInput(currentDay);

        if (currentDay == 1)
        {
            CreateTask(() => Day1Main.Part1(input), () => Day1Main.Part2(input));
            Day1Main.StartPart1ComputeShader(input);
        }
        else if (currentDay == 2)
        {
            CreateTask(() => Day2Main.Part1(input), () => Day2Main.Part2(input));
        }
        else if (currentDay == 3)
        {
            CreateTask(() => Day3Main.Part1(input) + "", () => Day3Main.Part2(input) + "");
        }
        else if (currentDay == 4)
        {
            CreateTask(() => Day4Main.Part1(input) + "", () => Day4Main.Part2(input) + "");
        }
        else if (currentDay == 5)
        {
            CreateTask(() => Day5Main.Part1(input) + "", () => Day5Main.Part2(input) + "");
        }
        else if (currentDay == 6)
        {
            CreateTask(() => Day6Main.Part1(input) + "", () => Day6Main.Part2(input) + "");
        }
        else if (currentDay == 7)
        {
            CreateTask(() => Day7Main.Part1(input) + "", () => Day7Main.Part2(input) + "");
        }
        else if (currentDay == 8)
        {
            CreateTask(() => Day8Main.Part1(input) + "", () => Day8Main.Part2(input) + "");
        }
        else if (currentDay == 9)
        {
            CreateTask(() => Day9Main.Part1(input) + "", () => Day9Main.Part2(input) + "");
        }
        else if (currentDay == 10)
        {
            CreateTask(() => Day10Main.Part1(input) + "", () => Day10Main.Part2(input) + "");
        }
        else if (currentDay == 11)
        {
            CreateTask(() => Day11Main.Part1(input) + "", () => Day11Main.Part2(input) + "");
        }
        else if (currentDay == 12)
        {
            CreateTask(() => Day12Main.Part1(input) + "", () => Day12Main.Part2(input) + "");
        }
        else if (currentDay == 13)
        {
            CreateTask(() => Day13Main.Part1(input) + "", () => Day13Main.Part2(input) + "");
        }
        else if (currentDay == 14)
        {
            CreateTask(() => Day14Main.Part1(input) + "", () => Day14Main.Part2(input) + "");
        }
        else if (currentDay == 15)
        {
            CreateTask(() => Day15Main.Part1(input) + "", () => Day15Main.Part2(input) + "");
        }
    }