예제 #1
0
        public long Part2()
        {
            IntcodeComputer computer = new IntcodeComputer(InputUtils.GetFileName(21));

            computer.Run();
            var initialOutput = computer.GetAllOutput().ToList();

            if (initialOutput.Any())
            {
                StringBuilder outputBuilder = new StringBuilder();
                foreach (var o in initialOutput)
                {
                    outputBuilder.Append((char)o);
                }

                Console.WriteLine(outputBuilder.ToString());
            }

            List <int>    input   = new List <int>();
            List <string> program = new List <string>();

            program.Add("OR E J");
            program.Add("OR H J");
            program.Add("AND D J");
            program.Add("OR A T");
            program.Add("AND B T");
            program.Add("AND C T");
            program.Add("NOT T T");
            program.Add("AND T J");
            program.Add("RUN");

            foreach (string s in program)
            {
                foreach (char c in s)
                {
                    input.Add((int)c);
                }
                input.Add(10);
            }

            foreach (var i in input)
            {
                computer.EnqueueInput(i);
                computer.Run();
                var output = computer.GetAllOutput().ToList();
                if (output.Any())
                {
                    return(output.Last());
                }
            }

            return(0);
        }
        public void TestCompileOutput(long input, long expected, long[] program)
        {
            IntcodeComputer ic = new IntcodeComputer(program, input);

            ic.Run();
            Assert.That(ic.Output, Is.EqualTo(expected));
        }
예제 #3
0
        public void TestInputAndOutputPairs(int[] input, int[] expectedOutput)
        {
            var computer = new IntcodeComputer(input);

            computer.Run();
            Assert.Equal(expectedOutput, computer.Heap.ToArray());
        }
예제 #4
0
        public object Solve()
        {
            var program = GetProgram();
            var input   = new BlockingCollection <long>();
            var output  = new BlockingCollection <long>();

            var computer = new IntcodeComputer();

            var runTask = Task.Run(() =>
            {
                while (!computer.IsHalted)
                {
                    computer.Run(program, input, output);
                }
            });

            long score = -1;

            var outputTask = Task.Run(() =>
            {
                var screen = new Dictionary <Point, string>();

                var buffer = new List <long>();

                while (!computer.IsHalted)
                {
                    if (!output.TryTake(out var result, TimeSpan.FromMilliseconds(1)))
                    {
                        //DrawScreen(screen);

                        var joystickInput = CalculateJoystickInput(screen);
                        input.Add(joystickInput);
                    }
예제 #5
0
        public object Solve()
        {
            var program = GetProgram();
            var input   = new BlockingCollection <long>();
            var output  = new BlockingCollection <long>();

            var computer = new IntcodeComputer();

            while (!computer.IsHalted)
            {
                computer.Run(program, input, output);
            }

            var tiles = output.ToArray();

            var blockTileCount = 0;

            for (var i = 2; i < tiles.Length; i += 3)
            {
                var tile = tiles[i];

                if (tile == 2)
                {
                    blockTileCount++;
                }
            }

            return(blockTileCount);
        }
예제 #6
0
        public void TraceMode_Should_Reach_Data_Beyond_Run_Opcodes_When_Needed()
        {
            long[] data = new long[]
            {
                1001, 8, 1, 8,
                1005, 8, 0,
                99,
                -5,
            };
            IntcodeComputer computerTested = new IntcodeComputer(data, IntcodeMode.Trace);

            string[] expected = new[]
            {
                "0000 pos_0: pos_1 = pos_1 + 1",
                "0004        if (pos_1 != 0) goto pos_0",
                "0000        pos_1 = pos_1 + 1",
                "0004        if (pos_1 != 0) goto pos_0",
                "0000        pos_1 = pos_1 + 1",
                "0004        if (pos_1 != 0) goto pos_0",
                "0000        pos_1 = pos_1 + 1",
                "0004        if (pos_1 != 0) goto pos_0",
                "0000        pos_1 = pos_1 + 1",
                "0004        if (pos_1 != 0) goto pos_0",
                "0007        stop",
                "",
                "0008 pos_1: data 0",
            };
            computerTested.Run();

            List <string> trace = computerTested.GetTrace();

            trace.Should().BeEquivalentTo(expected);
        }
예제 #7
0
        private static void Part1()
        {
            var sw = System.Diagnostics.Stopwatch.StartNew();

            IntcodeComputer computer = new IntcodeComputer(rom);
            long            max      = int.MinValue;

            foreach (var settings in GetPermutations(Enumerable.Range(0, 5), 5))
            {
                long signal = 0;
                foreach (var phase in settings)
                {
                    computer.Run(phase, signal);
                    if (computer.OutputQueue.Any())
                    {
                        signal = computer.OutputQueue.Dequeue();
                    }
                }
                max = Math.Max(max, signal);
            }
            Console.WriteLine($"Part 1: {max}");

            sw.Stop();
            System.Diagnostics.Debug.WriteLine(sw.Elapsed);
        }
예제 #8
0
        public long Run()
        {
            var result = IntcodeResult.NO_RESULT;

            while (result != IntcodeResult.HALT_TERMINATE)
            {
                result = _computer.Run();
                ParseOutput(_computer.Output);
                if (result == IntcodeResult.HALT_FORINPUT)
                {
                    var ball   = _tiles.First(x => x.Value == TileType.Ball).Key;
                    var paddle = _tiles.First(x => x.Value == TileType.HorizontalPaddle).Key;

                    var move = paddle switch
                    {
                        _ when paddle.X == ball.X => 0,
                        _ when paddle.X > ball.X => - 1,
                        _ when paddle.X < ball.X => 1,
                        _ => throw new InvalidOperationException("Da Faq?"),
                    };
                    _computer.Input.Enqueue(move);
                }
            }

            return(_score);
        }
        public void TestCompileRelativeBaseOffsetOperation(long expected, long[] program)
        {
            IntcodeComputer ic = new IntcodeComputer(program, 1);

            ic.Run();
            Assert.That(ic.Output, Is.EqualTo(expected));
        }
예제 #10
0
        public void Intcode_Should_Return_Correct_Value_Based_On_Noun_And_Verb(int noun, int verb, long result)
        {
            long[]          data = new long[] { 1, 0, 0, 3, 1, 1, 2, 3, 1, 3, 4, 3, 1, 5, 0, 3, 2, 13, 1, 19, 1, 19, 10, 23, 1, 23, 6, 27, 1, 6, 27, 31, 1, 13, 31, 35, 1, 13, 35, 39, 1, 39, 13, 43, 2, 43, 9, 47, 2, 6, 47, 51, 1, 51, 9, 55, 1, 55, 9, 59, 1, 59, 6, 63, 1, 9, 63, 67, 2, 67, 10, 71, 2, 71, 13, 75, 1, 10, 75, 79, 2, 10, 79, 83, 1, 83, 6, 87, 2, 87, 10, 91, 1, 91, 6, 95, 1, 95, 13, 99, 1, 99, 13, 103, 2, 103, 9, 107, 2, 107, 10, 111, 1, 5, 111, 115, 2, 115, 9, 119, 1, 5, 119, 123, 1, 123, 9, 127, 1, 127, 2, 131, 1, 5, 131, 0, 99, 2, 0, 14, 0 };
            IntcodeComputer i    = new IntcodeComputer(data);

            i.Run(noun, verb).Should().Be(result);
        }
예제 #11
0
        public int Part2()
        {
            long desiredOutput = 19690720;

            IntcodeComputer computer = new IntcodeComputer(InputUtils.GetFileName(2));

            for (int noun = 0; noun < 100; noun++)
            {
                for (int verb = 0; verb < 100; verb++)
                {
                    computer.Reboot();
                    computer.SetMemory(1, noun);
                    computer.SetMemory(2, verb);

                    computer.Run();

                    if (computer.GetValueAtAddress(0) == desiredOutput)
                    {
                        return(100 * noun + verb);
                    }
                }
            }

            return(0);
        }
        public void TestCompileEqualsOperation(long expected, long[] program)
        {
            IntcodeComputer ic = new IntcodeComputer(program, 1);

            ic.Run();
            Assert.That(program[0], Is.EqualTo(expected));
        }
예제 #13
0
        public void TraceMode_Should_Generate_Labelized_Events_Really_Hapenning()
        {
            long[] data = new long[]
            {
                -5,
                1001, 0, 1, 0,
                1005, 0, 1,
                99,
            };
            IntcodeComputer computerTested = new IntcodeComputer(data, IntcodeMode.Trace);

            string[] expected = new[]
            {
                "0000 pos_0: data -5",
                "",
                "0001 pos_1: pos_0 = pos_0 + 1",
                "0005        if (pos_0 != 0) goto pos_1",
                "0001        pos_0 = pos_0 + 1",
                "0005        if (pos_0 != 0) goto pos_1",
                "0001        pos_0 = pos_0 + 1",
                "0005        if (pos_0 != 0) goto pos_1",
                "0001        pos_0 = pos_0 + 1",
                "0005        if (pos_0 != 0) goto pos_1",
                "0001        pos_0 = pos_0 + 1",
                "0005        if (pos_0 != 0) goto pos_1",
                "0008        stop"
            };
            computerTested.Run();

            List <string> trace = computerTested.GetTrace();

            trace.Should().BeEquivalentTo(expected);
        }
예제 #14
0
        public void PartTwo(string input, TextWriter @out)
        {
            var memory = input.Split(',').Select(long.Parse).ToImmutableArray();

            var scratch = new long[memory.Length];

            foreach (var noun in Enumerable.Range(0, 100))
            {
                foreach (var verb in Enumerable.Range(0, 100))
                {
                    memory.CopyTo(scratch);

                    scratch[1] = noun;
                    scratch[2] = verb;

                    var computer = new IntcodeComputer(scratch.ToImmutableArray());
                    computer.Run();
                    var result = computer.Output.Dequeue();

                    if (result == 19690720)
                    {
                        @out.WriteLine($"Noun: {noun}, Verb: {verb}");
                        @out.WriteLine($"Result: 100 * {noun} + {verb} = {100 * noun + verb}");
                        goto end;
                    }
                }
            }
end:
            @out.WriteLine("Done");
        }
        public void TestCompileInputOperation()
        {
            long[]          program = new long[] { 3, 3, 99, 0 };
            IntcodeComputer ic      = new IntcodeComputer(program, 1);

            ic.Run();
            Assert.That(program[3], Is.EqualTo(1));
        }
        internal static long RunWithInput(ImmutableArray <long> program, ImmutableArray <string> inputProgram, ImmutableArray <string[]> subsets)
        {
            var computer = new IntcodeComputer(program);

            EnqueueCommand(computer, inputProgram);
            computer.Run();
            foreach (var subset in subsets)
            {
                EnqueueCommand(computer, subset);
                computer.Run();
            }
            computer.Input.Enqueue((int)'n');
            computer.Input.Enqueue(10);
            computer.Run();

            return(computer.Output.Max());
        }
        public void TestSelfCopy()
        {
            long[]          p  = new long[] { 109, 1, 204, -1, 1001, 100, 1, 100, 1008, 100, 16, 101, 1006, 101, 0, 99  };
            IntcodeComputer ic = new IntcodeComputer(p, 1);

            ic.Run();
            Assert.That(ic.Output, Is.EqualTo(99));
        }
예제 #18
0
        public int Part1()
        {
            IntcodeComputer computer = new IntcodeComputer(InputUtils.GetFileName(2));

            computer.SetMemory(1, 12);
            computer.Run();
            return((int)computer.GetValueAtAddress(0));
        }
        public void TestCompileOutputOperation()
        {
            long[]          program = new long[] { 4, 3, 99, 200 };
            IntcodeComputer ic      = new IntcodeComputer(program, 1);

            ic.Run();
            Assert.That(ic.Output, Is.EqualTo(200));
        }
        public void TestFailingCompilation()
        {
            long[]          program = new long[] { 4, 0, 4, 1, 99 };
            IntcodeComputer ic      = new IntcodeComputer(program, 1);

            ic.Run();
            Assert.That(ic.Output, Is.EqualTo(4));
        }
예제 #21
0
        public void Opcode_8_Should_Write_Zero_When_First_Param_Is_Not_Equal_To_Second()
        {
            long[]          data           = new long[] { 1108, 1, 0, 0, 99 };
            IntcodeComputer computerTested = new IntcodeComputer(data);

            long result = computerTested.Run();

            result.Should().Be(0);
        }
예제 #22
0
파일: Day9.cs 프로젝트: stames/aoc
        public long Part2()
        {
            IntcodeComputer computer = new IntcodeComputer(InputUtils.GetFileName(9));

            computer.EnqueueInput(2);
            computer.Run();

            return(computer.GetAllOutput().Last());
        }
예제 #23
0
파일: Day5.cs 프로젝트: stames/aoc
        public int Part2()
        {
            IntcodeComputer computer = new IntcodeComputer(InputUtils.GetFileName(5));

            computer.EnqueueInput(5);
            computer.Run();

            return((int)computer.GetAllOutput().Last());
        }
예제 #24
0
        public void Opcode_6_Should_Jump_To_Second_Parameter_Position_When_First_Parameter_Is_Zero()
        {
            long[]          data           = new long[] { 1, 0, 0, 0, 1106, 0, 11, 1, 0, 0, 0, 99 };
            IntcodeComputer computerTested = new IntcodeComputer(data);

            long result = computerTested.Run();

            result.Should().Be(2);
        }
예제 #25
0
        public void Opcode_6_Should_Do_Nothing_When_First_Parameter_Is_Non_Zero()
        {
            long[]          data           = new long[] { 1, 0, 0, 0, 1106, 1, 11, 1, 0, 0, 0, 99 };
            IntcodeComputer computerTested = new IntcodeComputer(data);

            long result = computerTested.Run();

            result.Should().Be(4);
        }
예제 #26
0
        public void Opcodes_Should_Allow_ParameterMode_Change_For_Simple_Commands()
        {
            long[]          data           = new long[] { 1002, 8, 3, 8, 1001, 8, 0, 0, 33 };
            IntcodeComputer computerTested = new IntcodeComputer(data);

            long result = computerTested.Run();

            result.Should().Be(99);
        }
예제 #27
0
        public void Memory_Should_Be_Larger_Than_Initial_Data()
        {
            long[]          data           = new long[] { 1, 0, 0, 9, 1, 9, 9, 0, 99 };
            IntcodeComputer computerTested = new IntcodeComputer(data);

            long result = computerTested.Run();

            result.Should().Be(4);
        }
예제 #28
0
        public void Large_Numbers_Should_Be_Okay()
        {
            long[]          data           = new long[] { 1102, 34915192, 34915192, 7, 4, 7, 99, 0 };
            IntcodeComputer computerTested = new IntcodeComputer(data);

            computerTested.Run();

            computerTested.OutputQueue.Should().Contain(1219070632396864);
        }
예제 #29
0
        public void Opcode_7_Should_Write_One_When_First_Param_Is_Less_Than_Second()
        {
            long[]          data           = new long[] { 1107, 0, 1, 0, 99 };
            IntcodeComputer computerTested = new IntcodeComputer(data);

            long result = computerTested.Run();

            result.Should().Be(1);
        }
예제 #30
0
        public void Opcode_9_Should_Offset_Relative_Base_For_Parameters_In_Relative_Mode()
        {
            long[]          data           = new long[] { 109, 2, 2201, 0, 0, 0, 99 };
            IntcodeComputer computerTested = new IntcodeComputer(data);

            long result = computerTested.Run();

            result.Should().Be(4402);
        }