Beispiel #1
0
    public async Task <int> Part1()
    {
        var signal  = new SemaphoreSlim(0);
        var input   = Channel.CreateUnbounded <BigInteger>();
        var output  = new List <char>();
        var intcode = new IntcodeComputer(_input)
        {
            Input  = () => input.Reader.ReadAsync().AsTask(),
            Output = value => { output.Add((char)value); return(Task.CompletedTask); },
        };

        var commands = new[]
        {
            "east",
            "take whirled peas",
            "north",
            "west",
            "south",
            "take antenna",
            "north",
            "east",
            "south",
            "east",
            "north",
            "take prime number",
            "south",
            "west",
            "west",
            "north",
            "take fixed point",
            "north",
            "east",
            "south",
        };

        foreach (var command in commands)
        {
            foreach (var c in Encoding.ASCII.GetBytes(command))
            {
                input.Writer.TryWrite(c);
            }

            input.Writer.TryWrite('\n');
        }

        await intcode.RunAsync();

        return(Int32.Parse(Regex.Match(new string(output.ToArray()), @"(\d{7})").Value));
    }
Beispiel #2
0
        private static void Part2()
        {
            var sw = System.Diagnostics.Stopwatch.StartNew();

            IntcodeComputer computer = new IntcodeComputer(rom)
            {
                PrintOutput = true
            };

            computer.InputQueue.Enqueue(2);
            computer.Run();

            sw.Stop();
            System.Diagnostics.Debug.WriteLine(sw.Elapsed);
        }
Beispiel #3
0
        public void PartTwo(string input, TextWriter @out)
        {
            var memory = input.Split(',').Select(long.Parse)
                         .ToImmutableArray();

            var computer = new IntcodeComputer(memory);

            computer.Input.Enqueue(5);

            computer.Run();

            var output = computer.Output.Last();

            @out.WriteLine($"Computer result: {output}");
        }
Beispiel #4
0
        public void Opcodes_3_Should_Wait_For_Input_Queue_When_In_Blocking_Mode()
        {
            long[]            data           = new long[] { 3, 0, 4, 0, 99 };
            long              input          = 1234;
            Mock <TextWriter> mockTextWriter = new Mock <TextWriter>();

            Console.SetOut(mockTextWriter.Object);
            IntcodeComputer computerTested = new IntcodeComputer(data, IntcodeMode.Blocking);

            new Thread(() => computerTested.Run()).Start();

            computerTested.InputQueue.Enqueue(input);
            mockTextWriter.Verify(m => m.Write("Input: "), Times.Never);
            mockTextWriter.Verify(m => m.WriteLine("Output: " + input), Times.Once);
        }
Beispiel #5
0
        public static void LogAnswerToConsole()
        {
            var input = new List <int>()
            {
                3, 8, 1001, 8, 10, 8, 105, 1, 0, 0, 21, 38, 55, 68, 93, 118, 199, 280, 361, 442, 99999, 3, 9, 1002, 9, 2, 9, 101, 5, 9, 9, 102, 4, 9, 9, 4, 9, 99, 3, 9, 101, 3, 9, 9, 1002, 9, 3, 9, 1001, 9, 4, 9, 4, 9, 99, 3, 9, 101, 4, 9, 9, 102, 3, 9, 9, 4, 9, 99, 3, 9, 102, 2, 9, 9, 101, 4, 9, 9, 102, 2, 9, 9, 1001, 9, 4, 9, 102, 4, 9, 9, 4, 9, 99, 3, 9, 1002, 9, 2, 9, 1001, 9, 2, 9, 1002, 9, 5, 9, 1001, 9, 2, 9, 1002, 9, 4, 9, 4, 9, 99, 3, 9, 101, 1, 9, 9, 4, 9, 3, 9, 102, 2, 9, 9, 4, 9, 3, 9, 101, 1, 9, 9, 4, 9, 3, 9, 101, 1, 9, 9, 4, 9, 3, 9, 101, 1, 9, 9, 4, 9, 3, 9, 101, 2, 9, 9, 4, 9, 3, 9, 1001, 9, 1, 9, 4, 9, 3, 9, 102, 2, 9, 9, 4, 9, 3, 9, 1002, 9, 2, 9, 4, 9, 3, 9, 1002, 9, 2, 9, 4, 9, 99, 3, 9, 1001, 9, 1, 9, 4, 9, 3, 9, 1001, 9, 2, 9, 4, 9, 3, 9, 1002, 9, 2, 9, 4, 9, 3, 9, 101, 2, 9, 9, 4, 9, 3, 9, 1001, 9, 2, 9, 4, 9, 3, 9, 1001, 9, 1, 9, 4, 9, 3, 9, 102, 2, 9, 9, 4, 9, 3, 9, 1001, 9, 1, 9, 4, 9, 3, 9, 1002, 9, 2, 9, 4, 9, 3, 9, 102, 2, 9, 9, 4, 9, 99, 3, 9, 1002, 9, 2, 9, 4, 9, 3, 9, 1001, 9, 1, 9, 4, 9, 3, 9, 102, 2, 9, 9, 4, 9, 3, 9, 102, 2, 9, 9, 4, 9, 3, 9, 101, 1, 9, 9, 4, 9, 3, 9, 1001, 9, 1, 9, 4, 9, 3, 9, 101, 2, 9, 9, 4, 9, 3, 9, 102, 2, 9, 9, 4, 9, 3, 9, 101, 2, 9, 9, 4, 9, 3, 9, 1001, 9, 2, 9, 4, 9, 99, 3, 9, 1001, 9, 2, 9, 4, 9, 3, 9, 1001, 9, 2, 9, 4, 9, 3, 9, 102, 2, 9, 9, 4, 9, 3, 9, 101, 1, 9, 9, 4, 9, 3, 9, 1002, 9, 2, 9, 4, 9, 3, 9, 1002, 9, 2, 9, 4, 9, 3, 9, 1001, 9, 2, 9, 4, 9, 3, 9, 1001, 9, 2, 9, 4, 9, 3, 9, 101, 1, 9, 9, 4, 9, 3, 9, 1001, 9, 1, 9, 4, 9, 99, 3, 9, 102, 2, 9, 9, 4, 9, 3, 9, 1001, 9, 1, 9, 4, 9, 3, 9, 1001, 9, 1, 9, 4, 9, 3, 9, 1002, 9, 2, 9, 4, 9, 3, 9, 1002, 9, 2, 9, 4, 9, 3, 9, 1001, 9, 2, 9, 4, 9, 3, 9, 1002, 9, 2, 9, 4, 9, 3, 9, 102, 2, 9, 9, 4, 9, 3, 9, 102, 2, 9, 9, 4, 9, 3, 9, 101, 2, 9, 9, 4, 9, 99
            };
            //var input = new List<int>() { 3,26,1001,26,-4,26,3,27,1002,27,2,27,1,27,26,27,4,27,1001,28,-1,28,1005,28,6,99,0,0,5 };
            var intcodeComputer = new IntcodeComputer(input);

            //var output = intcodeComputer.GetMaximumAmplifierOutput();
            var output = intcodeComputer.GetLoopingAmplifierOutput();

            //var output = intcodeComputer.RunAmplifiers(new List<int> { 9, 8, 7, 6, 5 });
            Console.WriteLine(output);
        }
Beispiel #6
0
        public void Opcodes_4_Should_Also_Write_To_OutputQueue()
        {
            long[]            data           = new long[] { 3, 0, 4, 0, 99 };
            long              input          = 1234;
            Mock <TextWriter> mockTextWriter = new Mock <TextWriter>();

            Console.SetOut(mockTextWriter.Object);
            IntcodeComputer computerTested = new IntcodeComputer(data);

            computerTested.InputQueue.Enqueue(input);

            computerTested.Run();

            computerTested.OutputQueue.Enqueue(input);
        }
        private static void EnqueueCommand(IntcodeComputer computer, IEnumerable <string> command)
        {
            var ascii = ToAscii(string.Join(",", command));

            if (ascii.Count() > 20)
            {
                Console.WriteLine("Too Many Instructions!");
                Debugger.Break();
            }

            foreach (var c in ascii)
            {
                computer.Input.Enqueue(c);
            }
            computer.Input.Enqueue(10);
Beispiel #8
0
        public long[] Part1()
        {
            var results  = new List <long>();
            var computer = new IntcodeComputer(_instructions);

            computer.OnOutput += l => results.Add(l);
            if (_input != null)
            {
                computer.Input(_input.Value);
            }

            computer.StartAsync().Wait();

            return(results.ToArray());
        }
Beispiel #9
0
    public async Task <int> Part1()
    {
        var outputs = new List <int>();
        var intcode = new IntcodeComputer(_input)
        {
            Output = value => { outputs.Add((int)value); return(Task.CompletedTask); }
        };
        await intcode.RunAsync();

        return(outputs
               .Select((o, i) => (o, i))
               .GroupBy(v => v.i % 3)
               .First(g => g.Key == 2)
               .Count(v => v.o == 2));
    }
Beispiel #10
0
        public object Solve()
        {
            var highestSignal = 0;

            var phaseSettingSequences = GetPhaseSettingSequences();

            foreach (var phaseSettingSequence in phaseSettingSequences)
            {
                var computer1 = new IntcodeComputer();
                var program1  = GetProgram();
                var input1    = new Queue <int>();
                input1.Enqueue(phaseSettingSequence.Item1);
                input1.Enqueue(0);
                var output1 = computer1.Run(program1, input1);

                var computer2 = new IntcodeComputer();
                var program2  = GetProgram();
                var input2    = new Queue <int>();
                input2.Enqueue(phaseSettingSequence.Item2);
                input2.Enqueue(output1);
                var output2 = computer2.Run(program2, input2);

                var computer3 = new IntcodeComputer();
                var program3  = GetProgram();
                var input3    = new Queue <int>();
                input3.Enqueue(phaseSettingSequence.Item3);
                input3.Enqueue(output2);
                var output3 = computer3.Run(program3, input3);

                var computer4 = new IntcodeComputer();
                var program4  = GetProgram();
                var input4    = new Queue <int>();
                input4.Enqueue(phaseSettingSequence.Item4);
                input4.Enqueue(output3);
                var output4 = computer4.Run(program4, input4);

                var computer5 = new IntcodeComputer();
                var program5  = GetProgram();
                var input5    = new Queue <int>();
                input5.Enqueue(phaseSettingSequence.Item5);
                input5.Enqueue(output4);
                var output5 = computer5.Run(program5, input5);

                highestSignal = Math.Max(highestSignal, output5);
            }

            return(highestSignal);
        }
Beispiel #11
0
        public int Part2()
        {
            List <int> results = new List <int>();

            List <int> values = new List <int> {
                5, 6, 7, 8, 9
            };
            var permutations = values.Permute();

            foreach (var permutation in permutations)
            {
                IntcodeComputer a = new IntcodeComputer(InputUtils.GetFileName(7));
                IntcodeComputer b = new IntcodeComputer(InputUtils.GetFileName(7));
                IntcodeComputer c = new IntcodeComputer(InputUtils.GetFileName(7));
                IntcodeComputer d = new IntcodeComputer(InputUtils.GetFileName(7));
                IntcodeComputer e = new IntcodeComputer(InputUtils.GetFileName(7));

                a.EnqueueInput(permutation.ToList()[0]);
                b.EnqueueInput(permutation.ToList()[1]);
                c.EnqueueInput(permutation.ToList()[2]);
                d.EnqueueInput(permutation.ToList()[3]);
                e.EnqueueInput(permutation.ToList()[4]);

                long eOutput = 0;

                while (!a.IsHalted && !b.IsHalted && !c.IsHalted && !d.IsHalted && !e.IsHalted)
                {
                    a.EnqueueInput(eOutput);
                    long aOutput = a.RunUntilNextOutput();

                    b.EnqueueInput(aOutput);
                    long bOutput = b.RunUntilNextOutput();

                    c.EnqueueInput(bOutput);
                    long cOutput = c.RunUntilNextOutput();

                    d.EnqueueInput(cOutput);
                    long dOutput = d.RunUntilNextOutput();

                    e.EnqueueInput(dOutput);
                    eOutput = e.RunUntilNextOutput();
                }

                results.Add((int)eOutput);
            }

            return(results.Max());
        }
 public void Start([FromBody] StartRequest request)
 {
     long[] data = InputHelper.GetIntcodeFromFile(request.File);
     _computer = new IntcodeComputer(data, IntcodeMode.Blocking);
     if (request.File == "17")
     {
         List <string> commands = new List <string>
         {
             "A,B,B,A,C,B,C,C,B,A",
             "R,10,R,8,L,10,L,10",
             "R,8,L,6,L,6",
             "L,10,R,10,L,6",
             "y"
         };
         foreach (string command in commands)
         {
             foreach (char c in command.ToCharArray())
             {
                 _computer.InputQueue.Enqueue((long)c);
             }
             _computer.InputQueue.Enqueue(10);
         }
     }
     if (request.File == "21")
     {
         List <string> commands = new List <string>
         {
             "OR H J\n",
             "OR E J\n",
             "OR A T\n",
             "AND B T\n",
             "AND C T\n",
             "NOT T T\n",
             "AND D T\n",
             "AND T J\n",
             "RUN\n"
         };
         foreach (string command in commands)
         {
             foreach (char c in command.ToCharArray())
             {
                 _computer.InputQueue.Enqueue(c);
             }
         }
     }
     _computer.Reporter = _reporter;
     _intcodeHub.Clients.All.BroadcastMessage("intcode_console", "Started !");
 }
        public static void solve()
        {
            var input = InputLoader.loadAsString("11").Split(",");

            var program = new Dictionary <Int64, Int64>();

            for (int i = 0; i < input.Length; i++)
            {
                program[i] = Int64.Parse(input[i]);
            }

            var computer = new IntcodeComputer(program);

            var panels = new Dictionary <Tuple <int, int>, int>();

            var robotPos = Tuple.Create(0, 0);
            var robotDir = Direction.Up;

            while (!computer.isHalted())
            {
                var currentColor = 0;
                if (panels.ContainsKey(robotPos))
                {
                    currentColor = panels[robotPos];
                }

                // Compute
                var outputs = computer.execute(new int[] { currentColor });

                // Rotate
                if (outputs[1] == 0)
                {
                    robotDir = rotateLeft(robotDir);
                }
                else
                {
                    robotDir = rotateRight(robotDir);
                }

                // Paint
                panels[robotPos] = (int)outputs[0];

                // Move
                robotPos = move(robotPos, robotDir);
            }

            Console.WriteLine(panels.Count);
        }
            public override InstructionResult Execute(IntcodeComputer computer, long[] buffer)
            {
                try
                {
                    bool equal = Parameters[0].GetValue(ref computer) == Parameters[1].GetValue(ref computer);
                    var  dest  = Parameters[2].GetPosition(ref computer);

                    computer.CurrentMemoryState()[dest] = equal ? 1 : 0;

                    return(new InstructionResult(true, "EqualsInstruction passed."));
                }
                catch (Exception e)
                {
                    return(new InstructionResult(false, e.Message));
                }
            }
Beispiel #15
0
        public void Opcodes_3_Should_Read_From_Input_Queue_When_It_Is_Not_Empty()
        {
            long[]            data           = new long[] { 3, 0, 4, 0, 99 };
            long              input          = 1234;
            Mock <TextWriter> mockTextWriter = new Mock <TextWriter>();

            Console.SetOut(mockTextWriter.Object);
            IntcodeComputer computerTested = new IntcodeComputer(data);

            computerTested.InputQueue.Enqueue(input);

            computerTested.Run();

            mockTextWriter.Verify(m => m.Write("Input: "), Times.Never);
            mockTextWriter.Verify(m => m.WriteLine("Output: " + input), Times.Once);
        }
Beispiel #16
0
        public void Opcodes_Should_Allow_ParameterMode_Change_For_NextGen_Commands()
        {
            long[]            data           = new long[] { 3, 0, 104, 0, 99 };
            long              input          = 123;
            Mock <TextWriter> mockTextWriter = new Mock <TextWriter>();
            Mock <TextReader> mockTextReader = new Mock <TextReader>();

            Console.SetOut(mockTextWriter.Object);
            Console.SetIn(mockTextReader.Object);
            mockTextReader.Setup(m => m.ReadLine()).Returns(input.ToString());
            IntcodeComputer computerTested = new IntcodeComputer(data);

            computerTested.Run();

            mockTextWriter.Verify(m => m.WriteLine("Output: 0"));
        }
            public override InstructionResult Execute(IntcodeComputer computer, long[] buffer)
            {
                try
                {
                    if (Parameters[0].GetValue(ref computer) != 0)
                    {
                        _next = (int)Parameters[1].GetValue(ref computer);
                    }

                    return(new InstructionResult(true, "JumpIfTrueInstruction passed."));
                }
                catch (Exception e)
                {
                    return(new InstructionResult(false, e.Message));
                }
            }
        public void Operation3_RelativeMode()
        {
            string code       = "109,-1,203,6,104,1,99";
            string programKey = "Day9";

            IntcodeComputer computer  = new IntcodeComputer();
            ProgramIO       programIO = new ProgramIO();

            programIO.WriteLineInput("2");

            computer.AddProgram(programKey, code, programIO);

            computer.RunProgram(programKey);

            Assert.Equal("2", programIO.ReadLineOutput());
        }
Beispiel #19
0
        public void Verbose_Mode_Should_Output_Simple_Commands()
        {
            long[]            data           = new long[] { 1002, 8, 3, 8, 1001, 8, 0, 0, 33 };
            Mock <TextWriter> mockTextWriter = new Mock <TextWriter>();
            Mock <TextReader> mockTextReader = new Mock <TextReader>();

            Console.SetOut(mockTextWriter.Object);
            Console.SetIn(mockTextReader.Object);
            IntcodeComputer computerTested = new IntcodeComputer(data, IntcodeMode.Verbose);

            long result = computerTested.Run();

            mockTextWriter.Verify(m => m.WriteLine("0000: Multiply [@1 8p (=33), @2 3i (=3), @3 8p (=33)]"));
            mockTextWriter.Verify(m => m.WriteLine("0004: Add [@5 8p (=99), @6 0i (=0), @7 0p (=1002)]"));
            mockTextWriter.Verify(m => m.WriteLine("0008: Finish"));
        }
Beispiel #20
0
        private int RunProgramOnAmplifiers(int[] intCodeProgram, int[] phaseSettings)
        {
            var lastOutput = 0;

            for (var i = 0; i < 5; i++)
            {
                var memory = intCodeProgram.Clone() as int[];
                var input  = new Queue <int>();
                input.Enqueue(phaseSettings[i]);
                input.Enqueue(lastOutput);
                var computer = new IntcodeComputer(memory, input);
                computer.Run();
                lastOutput = computer.OutputBuffer.Last();
            }
            return(lastOutput);
        }
    public override object PartTwo(string[] input)
    {
        var outputs  = new List <long>();
        var computer = new IntcodeComputer(input.First().Split(",").Select(x => long.Parse(x)))
        {
            InputHandler  = () => 5,
            OutputHandler = (x) =>
            {
                Console.Out.WriteLine(x);
                outputs.Add(x);
            }
        };

        computer.RunToCompletion();
        return(outputs.Last());
    }
        public void Operation3_PositionMode_TakesInput_SetsValue()
        {
            string          program  = "3,0,99";
            IntcodeComputer computer = new IntcodeComputer();

            //StringReader reader = new StringReader("1");

            ProgramIO programIO = new ProgramIO();

            programIO.WriteLineInput("1");

            computer.AddProgram("key", program, programIO);
            computer.RunProgram("key");

            Assert.Equal(1, computer.GetProgramAddressValue("key", 0));
        }
Beispiel #23
0
        public void Quiet_Mode_Should_Not_Output_Commands()
        {
            long[]            data           = new long[] { 1002, 8, 3, 8, 1001, 8, 0, 0, 33 };
            Mock <TextWriter> mockTextWriter = new Mock <TextWriter>();
            Mock <TextReader> mockTextReader = new Mock <TextReader>();

            Console.SetOut(mockTextWriter.Object);
            Console.SetIn(mockTextReader.Object);
            IntcodeComputer computerTested = new IntcodeComputer(data);

            long result = computerTested.Run();

            mockTextWriter.Verify(m => m.WriteLine("0: Multiply [8p, 3i, 8p]"), Times.Never);
            mockTextWriter.Verify(m => m.WriteLine("4: Add [8p, 0i, 0p]"), Times.Never);
            mockTextWriter.Verify(m => m.WriteLine("8: Finish"), Times.Never);
        }
Beispiel #24
0
    public async Task <BigInteger> Part2()
    {
        var(map, bot) = await GetMapAsync();

        var dir = bot.dir;

        var cardinal = PathToEnd(map, bot.pos)
                       .GroupWhile((prev, item) => prev == item)
                       .Select(steps => (dir: steps.First(), num: steps.Count()));
        var relative = new List <(char, int)>();

        foreach (var step in cardinal)
        {
            relative.Add((ToRelative(dir, step.dir), step.num));
            dir = step.dir;
        }

        var(main, a, b, c) = GetRoutines(relative);
        var input   = Channel.CreateUnbounded <BigInteger>();
        var output  = default(BigInteger);
        var intcode = new IntcodeComputer(_input)
        {
            Input  = () => input.Reader.ReadAsync().AsTask(),
            Output = value => { output = value; return(Task.CompletedTask); },
        };

        await InputAsync(main);
        await InputAsync(a);
        await InputAsync(b);
        await InputAsync(c);
        await InputAsync("n");

        intcode.Set(0, 2);
        await intcode.RunAsync();

        return(output);

        async Task InputAsync(string value)
        {
            foreach (var c in Encoding.ASCII.GetBytes(value))
            {
                await input.Writer.WriteAsync(c);
            }

            await input.Writer.WriteAsync('\n');
        }
    }
Beispiel #25
0
        public void Opcodes_3_and_4_Read_and_Write_To_Console_When_Input_Queue_Is_Empty()
        {
            long[]            data           = new long[] { 3, 0, 4, 0, 99 };
            long              input          = 1234;
            Mock <TextWriter> mockTextWriter = new Mock <TextWriter>();
            Mock <TextReader> mockTextReader = new Mock <TextReader>();

            Console.SetOut(mockTextWriter.Object);
            Console.SetIn(mockTextReader.Object);
            mockTextReader.Setup(m => m.ReadLine()).Returns(input.ToString());
            IntcodeComputer computerTested = new IntcodeComputer(data);

            computerTested.Run();

            mockTextWriter.Verify(m => m.Write("Input: "));
            mockTextWriter.Verify(m => m.WriteLine("Output: " + input));
        }
Beispiel #26
0
        public long Run(ImmutableArray <Instruction> instructions, TextWriter?writer = null)
        {
            var computer = new IntcodeComputer(_program);

            WriteInstructions(computer, instructions);

            var result = computer.Run();

            if (result != IntcodeResult.HALT_TERMINATE)
            {
                throw new InvalidOperationException("Error!");
            }

            writer?.WriteLine(new string(computer.Output.Select(l => (char)l).ToArray()));

            return(computer.Output.Last());
        }
Beispiel #27
0
        private static void Part1()
        {
            Stopwatch sw = Stopwatch.StartNew();

            var input     = File.ReadAllText("input.txt").Split(',').Select(long.Parse).ToArray();
            var computers = new IntcodeComputer[50];

            for (int i = 0; i < computers.Length; i++)
            {
                computers[i] = new IntcodeComputer(input);
                computers[i].InputQueue.Enqueue(i);
            }

            bool running = true;

            while (running)
            {
                foreach (IntcodeComputer computer in computers)
                {
                    if (computer.AwaitingInput())
                    {
                        computer.InputQueue.Enqueue(-1);
                    }
                    computer.RunNext();
                    if (computer.OutputQueue.Count == 3)
                    {
                        long address = computer.OutputQueue.Dequeue();
                        long x       = computer.OutputQueue.Dequeue();
                        long y       = computer.OutputQueue.Dequeue();

                        if (address == 255)
                        {
                            Console.WriteLine($"Part 1: {y}");
                            running = false;
                            break;
                        }

                        computers[address].InputQueue.Enqueue(x);
                        computers[address].InputQueue.Enqueue(y);
                    }
                }
            }

            sw.Stop();
            Debug.WriteLine(sw.Elapsed);
        }
Beispiel #28
0
        public static int GetDay2Part2Answer()
        {
            // The inputs should still be provided to the program by replacing
            // the values at addresses 1 and 2, just like before. In this program,
            // the value placed in address 1 is called the noun, and the value
            // placed in address 2 is called the verb. Each of the two input
            // values will be between 0 and 99, inclusive.

            // Once the program has halted, its output is available at address 0,
            // also just like before. Each time you try a pair of inputs, make
            // sure you first reset the computer's memory to the values in the
            // program (your puzzle input) - in other words, don't reuse memory
            // from a previous attempt.
            //
            // Find the input noun and verb that cause the program to produce
            // the output 19690720. What is 100 * noun + verb?
            // (For example, if noun=12 and verb=2, the answer would be 1202.)
            // Answer: 2254
            var  initialProgram = GetDay2Input();
            int  verb           = 0;
            bool foundResult    = false;
            int  noun;

            for (noun = 0; noun <= 99; noun++)
            {
                for (verb = 0; verb <= 99; verb++)
                {
                    initialProgram[1] = noun;
                    initialProgram[2] = verb;
                    var computer = new IntcodeComputer();
                    computer.LoadProgram(initialProgram);
                    computer.RunProgram();
                    var result = computer.GetProgramCopy();
                    if (result[0] == 19690720)
                    {
                        foundResult = true;
                        break;
                    }
                }
                if (foundResult)
                {
                    break;
                }
            }
            return((100 * noun) + verb);
        }
        public static void solve()
        {
            var input = InputLoader.loadAsString("17").Split(",");

            // Wake up the robot.
            input[0] = "2";

            var computer = new IntcodeComputer(input);

            var path = getPath(computer);

            ////////////////////////////////////////////
            // Cheaped out and did the next part by hand.
            ////////////////////////////////////////////

            foreach (var entry in path)
            {
                Console.Write(entry + ",");
            }

            Console.WriteLine();

            // A - R10,L12,R6,
            // A - R10,L12,R6,
            // B - R6,R10,R12,R6,
            // C - R10,L12,L12,
            // B - R6,R10,R12,R6,
            // C - R10,L12,L12,
            // B - R6,R10,R12,R6,
            // C - R10,L12,L12,
            // B - R6,R10,R12,R6,
            // A - R10,L12,R6,

            var instructions = "A,A,B,C,B,C,B,C,B,A\nR,10,L,12,R,6\nR,6,R,10,R,12,R,6\nR,10,L,12,L,12\nn\n";

            var computerInput = new int[instructions.Length];

            for (var i = 0; i < instructions.Length; i++)
            {
                computerInput[i] = instructions[i];
            }

            var outputs = computer.execute(computerInput);

            Console.WriteLine(outputs[outputs.Length - 1]);
        }
Beispiel #30
0
        static void Main(string[] args)
        {
            // // new Instruction(1);
            // Console.WriteLine(new Instruction(11101));
            // Console.WriteLine(new Instruction(11102));
            // Console.WriteLine(new Instruction(11103));
            // Console.WriteLine(new Instruction(11104));
            // Console.WriteLine(new Instruction(11199));

            var computer = new IntcodeComputer();

            computer.Load("3,225,1,225,6,6,1100,1,238,225,104,0,1102,59,58,224,1001,224,-3422,224,4,224,102,8,223,223,101,3,224,224,1,224,223,223,1101,59,30,225,1101,53,84,224,101,-137,224,224,4,224,1002,223,8,223,101,3,224,224,1,223,224,223,1102,42,83,225,2,140,88,224,1001,224,-4891,224,4,224,1002,223,8,223,1001,224,5,224,1,223,224,223,1101,61,67,225,101,46,62,224,1001,224,-129,224,4,224,1002,223,8,223,101,5,224,224,1,223,224,223,1102,53,40,225,1001,35,35,224,1001,224,-94,224,4,224,102,8,223,223,101,6,224,224,1,223,224,223,1101,5,73,225,1002,191,52,224,1001,224,-1872,224,4,224,1002,223,8,223,1001,224,5,224,1,223,224,223,102,82,195,224,101,-738,224,224,4,224,1002,223,8,223,1001,224,2,224,1,224,223,223,1101,83,52,225,1101,36,77,225,1101,9,10,225,1,113,187,224,1001,224,-136,224,4,224,1002,223,8,223,101,2,224,224,1,224,223,223,4,223,99,0,0,0,677,0,0,0,0,0,0,0,0,0,0,0,1105,0,99999,1105,227,247,1105,1,99999,1005,227,99999,1005,0,256,1105,1,99999,1106,227,99999,1106,0,265,1105,1,99999,1006,0,99999,1006,227,274,1105,1,99999,1105,1,280,1105,1,99999,1,225,225,225,1101,294,0,0,105,1,0,1105,1,99999,1106,0,300,1105,1,99999,1,225,225,225,1101,314,0,0,106,0,0,1105,1,99999,1007,226,226,224,1002,223,2,223,1006,224,329,1001,223,1,223,1108,226,226,224,102,2,223,223,1006,224,344,101,1,223,223,1007,677,677,224,102,2,223,223,1006,224,359,101,1,223,223,1108,677,226,224,1002,223,2,223,1005,224,374,1001,223,1,223,7,677,226,224,102,2,223,223,1005,224,389,1001,223,1,223,1008,677,677,224,1002,223,2,223,1005,224,404,101,1,223,223,108,226,226,224,1002,223,2,223,1006,224,419,101,1,223,223,1008,226,677,224,1002,223,2,223,1006,224,434,1001,223,1,223,1107,677,226,224,1002,223,2,223,1005,224,449,101,1,223,223,1008,226,226,224,102,2,223,223,1005,224,464,1001,223,1,223,8,226,226,224,1002,223,2,223,1006,224,479,1001,223,1,223,107,226,677,224,102,2,223,223,1005,224,494,1001,223,1,223,7,226,226,224,102,2,223,223,1005,224,509,1001,223,1,223,107,226,226,224,102,2,223,223,1005,224,524,101,1,223,223,107,677,677,224,1002,223,2,223,1006,224,539,101,1,223,223,8,677,226,224,1002,223,2,223,1006,224,554,101,1,223,223,1107,677,677,224,1002,223,2,223,1005,224,569,101,1,223,223,108,226,677,224,1002,223,2,223,1006,224,584,101,1,223,223,7,226,677,224,1002,223,2,223,1005,224,599,1001,223,1,223,8,226,677,224,102,2,223,223,1006,224,614,1001,223,1,223,108,677,677,224,1002,223,2,223,1006,224,629,1001,223,1,223,1007,226,677,224,1002,223,2,223,1006,224,644,101,1,223,223,1108,226,677,224,102,2,223,223,1005,224,659,1001,223,1,223,1107,226,677,224,102,2,223,223,1006,224,674,1001,223,1,223,4,223,99,226");
            // computer.Load("3,3,1108,-1,8,3,4,3,99");
            // computer.Load("3,21,1008,21,8,20,1005,20,22,107,8,21,20,1006,20,31,1106,0,36,98,0,0,1002,21,125,20,4,20,1105,1,46,104,999,1105,1,46,1101,1000,1,20,4,20,1105,1,46,98,99");

            Console.WriteLine(computer.Run(5));
        }