Example #1
0
        private static void RunPart1()
        {
            long highest             = 0;
            var  phaseSettingOptions = GetFirstPhaseSettingOptions();

            foreach (var settingOption in phaseSettingOptions)
            {
                var nextInput = 0L;
                foreach (var p in Enumerable.Range(0, 5))
                {
                    var input = new List <long> {
                        settingOption[p], nextInput
                    };
                    var machine = new IntcodeMachine(GetInput(), input);
                    machine.GetStateAfterRun(out List <long> output, out bool paused);
                    nextInput = output[0];
                }

                if (nextInput > highest)
                {
                    highest = nextInput;
                }
            }
            Console.WriteLine("Part 1 highest value: " + highest);
        }
Example #2
0
 public static void Run()
 {
     try
     {
         var nouns = Enumerable.Range(0, 100);
         var verbs = Enumerable.Range(0, 100);
         foreach (int noun in nouns)
         {
             foreach (int verb in verbs)
             {
                 var machine = new IntcodeMachine(new List <long>(GetNumbers()));
                 machine.Modify(1, noun);
                 machine.Modify(2, verb);
                 var stateAfterRun = machine.GetStateAfterRun(out List <long> output, out bool paused);
                 if (stateAfterRun[0] == 19690720)
                 {
                     throw new Exception($"Found it! Noun: {noun}, Verb: {verb}");
                 }
             }
         }
         Console.WriteLine("Finished writing program.");
     }
     catch (Exception e)
     {
         Console.WriteLine(e.Message);
     }
 }
Example #3
0
        public void IntcodeProgramTest(string intcodeProgram, string expectedResultProgram)
        {
            IntcodeMachine machine = new IntcodeMachine(intcodeProgram);

            machine.Execute();

            Assert.AreEqual(expectedResultProgram, machine.GetIntcodeProgram());
        }
Example #4
0
        public static void Run()
        {
            var input = new List <long>(GetInput());
            var range = Enumerable.Repeat(0, 1000000).Select(x => (long)x).ToList();

            input.AddRange(range); // 😬
            var machine = new IntcodeMachine(input, new List <long> {
                2
            });

            machine.GetStateAfterRun(out List <long> output, out bool paused);
            output.ForEach(x => Console.WriteLine(x));
        }
Example #5
0
        static void day9()
        {
            long[]         inputs  = getInputs("./day9/input.txt")[0].Split(',').Select(x => long.Parse(x)).ToArray();
            IntcodeMachine machine = new IntcodeMachine(inputs);

            machine.input.Enqueue(1);
            machine.runToEnd();
            machine.printOutput(); // Part 1. Answer: 2171728567

            machine = new IntcodeMachine(inputs);
            machine.input.Enqueue(2);
            machine.runToEnd();
            machine.printOutput(); // Part2. Answer: 49815
        }
Example #6
0
        static void day5()
        {
            long[]         inputs  = getInputs("./day5/input.txt")[0].Split(',').Select(x => long.Parse(x)).ToArray();
            IntcodeMachine machine = new IntcodeMachine(inputs);

            machine.input.Enqueue(1);
            machine.runToEnd();
            machine.printOutput(); // Part 1. Answer: 7988899

            machine = new IntcodeMachine(inputs);
            machine.input.Enqueue(5);
            machine.runToEnd();
            machine.printOutput(); // Part 2. Answer: 13758663
        }
Example #7
0
        public static void Run()
        {
            var programInput = new List <long>(GetInput());
            var machine      = new IntcodeMachine(programInput, input: new List <long> {
                1
            });

            machine.GetStateAfterRun(out List <long> output1, out bool paused1);
            Console.WriteLine("Part 1: " + output1.Last());

            programInput = new List <long>(GetInput());
            machine      = new IntcodeMachine(programInput, input: new List <long> {
                5
            });
            machine.GetStateAfterRun(out List <long> output2, out bool paused2);
            Console.WriteLine("Part 2: " + output2.Last());
        }
Example #8
0
    public override string Run()
    {
        var input      = Tools.GetFileContents("dec5");
        var inputArray = IntcodeMachine.GetInputArray(input);
        var shifter    = new IntcodeMachine(inputArray);

        shifter.QueueInput(1);
        var result = shifter.ExecuteProgram();

        sb.AppendLine($"\n\tPart 1: {result.ToString()}");

        inputArray = IntcodeMachine.GetInputArray(input);
        shifter.LoadProgram(inputArray);
        shifter.QueueInput(5);
        result = shifter.ExecuteProgram();
        sb.AppendLine($"\n\tPart 2: {result.ToString()}");

        return(sb.ToString());
    }
Example #9
0
    public override string Run()
    {
        var input          = Tools.GetFileContents("dec7");
        var defaultProgram = IntcodeMachine.GetInputArray(input);

        var thrusterSeqsP1 = generateAllPermutations(new List <int> {
            0, 1, 2, 3, 4
        });
        var thrusterSeqsP2 = generateAllPermutations(new List <int> {
            5, 6, 7, 8, 9
        });

        var outputP1 = RunAmplifiers(thrusterSeqsP1, (int[])defaultProgram.Clone());
        var outputP2 = RunAmplifiers(thrusterSeqsP2, (int[])defaultProgram.Clone());

        sb.AppendLine($"\n\tPart 1: {outputP1.ToString()}");
        sb.AppendLine($"\n\tPart 2: {outputP2.ToString()}");
        return(sb.ToString());
    }
Example #10
0
    public override string Run()
    {
        var input        = Tools.GetFileContents("dec2");
        var inputArray   = IntcodeMachine.GetInputArray(input);
        var replacements = new Dictionary <int, int> {
            { 1, 12 }, { 2, 2 }
        };

        inputArray = IntcodeMachine.ReplaceValues(inputArray, replacements);
        var intcode = new IntcodeMachine(inputArray);
        var output  = intcode.Alarm();

        sb.AppendLine($"\n\tPart 1: {output.ToString()}");

        inputArray = IntcodeMachine.GetInputArray(input);

        var output2 = intcode.FindVerbNouns(19690720, inputArray);
        var answer2 = output2.Key * 100 + output2.Value;

        sb.AppendLine($"\tPart 2: {answer2.ToString()}");

        return(sb.ToString());
    }
Example #11
0
        static void day2()
        {
            long[] inputs = getInputs("./day2/input.txt")[0].Split(',').Select(x => long.Parse(x)).ToArray();

            inputs[1] = 12;
            inputs[2] = 2;
            IntcodeMachine machine = new IntcodeMachine(inputs);

            machine.runToEnd();
            Console.WriteLine($"Part 1: {machine.memory[0]}"); // Part 1. Answer: 7210630

            bool found = false;

            for (int i = 0; i < 100; i++)
            {
                for (int j = 0; j < 100; j++)
                {
                    inputs[1] = i;
                    inputs[2] = j;
                    machine   = new IntcodeMachine(inputs);
                    machine.runToEnd();
                    long output = machine.memory[0];
                    if (output == 19690720)
                    {
                        Console.WriteLine("Answer found:");
                        Console.WriteLine(i + " " + j);
                        Console.WriteLine((100 * i) + j); // Part 2. Answer: 3892
                        found = true;
                        break;
                    }
                }
                if (found)
                {
                    break;
                }
            }
        }