Exemple #1
0
        public override (string message, object answer) SolvePart1()
        {
            _input = InputTest;
            _intCode.Execute();
            int output = _outputs.SingleOrDefault(o => o != 0);

            if (output != 0)
            {
                return("Final test output: ", output);
            }
            throw new Exception("At least one system failed, check outputs for more info");
        }
Exemple #2
0
            public void Execute(IEnumerable <long> instructions)
            {
                Reset();

                var intCode = new IntCode();

                while (true)
                {
                    intCode.SetInputs(GetPanel());

                    if (intCode.IsPaused)
                    {
                        intCode.Resume();
                    }
                    else
                    {
                        intCode.Execute(instructions);
                    }

                    var lastIndex  = intCode.Outputs.Count - 1;
                    var panelColor = (int)intCode.Outputs[lastIndex - 1];
                    var clockwise  = Convert.ToBoolean((int)intCode.Outputs[lastIndex]);

                    SetPanel(panelColor);
                    Rotate(clockwise);
                    MoveForward();

                    if (!intCode.IsPaused)
                    {
                        break;
                    }
                }
            }
Exemple #3
0
        public void ImmediateInstructionTest()
        {
            int[] program = { 1101, 100, -1, 4, 0 };
            var   outputs = IntCode.Execute(program, new[] { 10 });

            program.Should().BeEquivalentTo(1101, 100, -1, 4, 99);
        }
Exemple #4
0
        public void InputOutputTest()
        {
            int[] program = { 3, 0, 4, 0, 99 };
            var   outputs = IntCode.Execute(program, new[] { 10 });

            outputs.Should().ContainSingle().Which.Should().Be(10);
        }
Exemple #5
0
        long RunIntCode(int noun, int verb)
        {
            var instructions = Instructions.ToList();

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

            var intCode = new IntCode();

            return(intCode.Execute(instructions));
        }
        public override string ProblemTwo()
        {
            var test = new IntCode();

            test.SetInputs(2);
            test.Execute(Instructions);

            var result = test.Outputs.Last();

            return(result.ToString());
        }
        public override string ProblemTwo()
        {
            var intCode = new IntCode();

            intCode.SetInputs(5);
            intCode.Execute(Instructions);

            var result = intCode.Outputs.Last();

            return(result.ToString());
        }
Exemple #8
0
        public override string ProblemOne()
        {
            var intCode = new IntCode(4096);

            intCode.Execute(Instructions);

            var tiles  = GetTiles(intCode.Outputs.ToList());
            var result = tiles.Count(x => x.TileType == TileType.Block);

            return(result.ToString());
        }
        int CheckThrusters(string signalPhase)
        {
            var intCode = new IntCode();
            var output  = 0;

            for (int i = 0; i < signalPhase.Length; i++)
            {
                var signal = int.Parse(signalPhase[i].ToString());
                intCode.SetInputs(signal, output);
                intCode.Execute(Instructions);
                output = (int)intCode.Outputs.Last();
            }

            return(output);
        }
Exemple #10
0
        long PlayGame()
        {
            var intCode      = new IntCode(4096);
            var instructions = Instructions.ToList();

            instructions[0] = 2;

            var score = 0L;

            while (true)
            {
                if (intCode.IsPaused)
                {
                    intCode.Resume();
                }
                else
                {
                    intCode.Execute(instructions);
                }

                var tiles = GetTiles(intCode.Outputs.ToList());
                intCode.ClearOutput();

                var paddle    = (Tile?)tiles.FirstOrDefault(x => x.TileType == TileType.Paddle);
                var ball      = (Tile?)tiles.FirstOrDefault(x => x.TileType == TileType.Ball);
                var scoreInfo = (Tile?)tiles.FirstOrDefault(x => x.IsScore);
                if (scoreInfo.HasValue)
                {
                    score = scoreInfo.Value.Score;
                }

                if (!intCode.IsPaused)
                {
                    break;
                }

                var nextInput = paddle.HasValue && ball.HasValue
                    ? GetInput(paddle.Value, ball.Value) : 0;

                intCode.SetInputs(nextInput);
            }

            return(score);
        }
Exemple #11
0
        public override (string message, object answer) SolvePart1()
        {
            _intCode.Execute(Noun, Verb);

            return($"Output of {Noun}/{Verb}: ", _intCode[0]);
        }
Exemple #12
0
        public void Day5ProgramsTests(int[] code, int input, int expected, string because = "")
        {
            var outputs = IntCode.Execute(code, input);

            outputs.Should().ContainSingle().Which.Should().Be(expected, because);
        }
Exemple #13
0
 public void Day2ProgramsTests(int[] code, int[] expected)
 {
     IntCode.Execute(code);
     code.Should().BeEquivalentTo(expected);
 }