Example #1
0
    public static long Part2(string inputText)
    {
        var intcode  = InputParser.ListOfLongs(inputText, ',');
        var compiler = new IntCodeCompiler(0, true);

        intcode[0] = 2;
        var program = new IntCodeProgram(intcode, 0);


        var  level          = new TileType[100, 100];
        int  ballPosition   = 0;
        int  paddlePosition = 0;
        int  blockRemaining = 261;
        int  maxBreak       = 25000;
        long score          = 0;

        while (--maxBreak > 0 && !program.IsDone && blockRemaining > 0)
        {
            paddlePosition = FindXOfTileTyle(TileType.Paddle, level);
            ballPosition   = FindXOfTileTyle(TileType.Ball, level);

            if (paddlePosition < ballPosition)
            {
                compiler.SetInputs(new long[] { 1 });
            }
            else if (paddlePosition > ballPosition)
            {
                compiler.SetInputs(new long[] { -1 });
            }
            else
            {
                compiler.SetInputs(new long[] { 0 });
            }

            compiler.Clear();
            program = compiler.Compute(program);
            program = compiler.Compute(program);
            program = compiler.Compute(program);

            if (compiler.OutputValues[0] == -1 && compiler.OutputValues[1] == 0)
            {
                blockRemaining--;
                score = compiler.OutputValues[2];
                Debug.Log($"{blockRemaining} block remainning. Score : {score}");
            }
            else
            {
                var x        = compiler.OutputValues[0];
                var y        = compiler.OutputValues[1];
                var tileType = (int)compiler.OutputValues[2];
                level[y, x] = IdToTileType(tileType);
            }
        }

        Debug.Log($"maxBreak:{maxBreak},  ");

        return(score);
    }
Example #2
0
            public void Part1_KeepRelativeBase()
            {
                var compiler = new IntCodeCompiler(new long[] { 1 });
                var program  = compiler.Compute(new IntCodeProgram(new long[] { 1101, 2, 3, 3, 99 }, 0, 1));

                Assert.AreEqual(1, program.RelativeBaseOffset, "The relative Offset should be stay to 1 after an Addition");

                program = compiler.Compute(new IntCodeProgram(new long[] { 1102, 2, 3, 3, 99 }, 0, 1));
                Assert.AreEqual(1, program.RelativeBaseOffset, "The relative Offset should be stay to 1 after an Multiply");

                program = compiler.Compute(new IntCodeProgram(new long[] { 3, 0, 99 }, 0, 1));
                Assert.AreEqual(1, program.RelativeBaseOffset, "The relative Offset should be stay to 1 after an Input");

                program = compiler.Compute(new IntCodeProgram(new long[] { 4, 0, 99 }, 0, 1));
                Assert.AreEqual(1, program.RelativeBaseOffset, "The relative Offset should be stay to 1 after an ouput");

                program = compiler.Compute(new IntCodeProgram(new long[] { 1105, 1, 3, 99 }, 0, 1));
                Assert.AreEqual(1, program.RelativeBaseOffset, "The relative Offset should be stay to 1 after an JumpIfTrue");

                program = compiler.Compute(new IntCodeProgram(new long[] { 1106, 1, 3, 99 }, 0, 1));
                Assert.AreEqual(1, program.RelativeBaseOffset, "The relative Offset should be stay to 1 after an JumpIfFalse");

                program = compiler.Compute(new IntCodeProgram(new long[] { 1107, 1, 3, 0, 99 }, 0, 1));
                Assert.AreEqual(1, program.RelativeBaseOffset, "The relative Offset should be stay to 1 after an LessThen");

                program = compiler.Compute(new IntCodeProgram(new long[] { 1108, 1, 3, 0, 99 }, 0, 1));
                Assert.AreEqual(1, program.RelativeBaseOffset, "The relative Offset should be stay to 1 after an Equal");
            }
Example #3
0
            public void Part1_RelativeMultipleTime()
            {
                var compiler = new IntCodeCompiler();
                var program  = compiler.Compute(new IntCodeProgram(new long[] { 109, 1, 109, 1, 109, 1, 109, 1, 99 }, 0, 0));

                Assert.AreEqual(4, program.RelativeBaseOffset, "The relative Offset should be set to 1");
            }
Example #4
0
            public void Exemple5()
            {
                var input    = new long[] { 1, 1, 1, 4, 99, 5, 6, 0, 99 };
                var expected = new long[] { 30, 1, 1, 4, 2, 5, 6, 0, 99 };

                TestUtility.AreEqual(expected, IntCodeCompiler.Compute(input));
            }
Example #5
0
    private void Move(Direction direction)
    {
        Compiler.Clear();
        Compiler.SetInputs((long)direction);
        Program = Compiler.Compute(Program);
        var output      = Compiler.OutputValue;
        var forwardTile = GetForward(CurrentPosition, direction);

        ExploredMap[forwardTile.y, forwardTile.x] = true;
        if (output == 0)
        {
            Map[forwardTile.y, forwardTile.x] = TileType.Wall;
        }
        else if (output == 1)
        {
            Map[forwardTile.y, forwardTile.x] = TileType.Empty;
            CurrentPosition = forwardTile;
        }
        else
        {
            Map[forwardTile.y, forwardTile.x] = TileType.OxygenSystem;
            CurrentPosition = forwardTile;
        }

        var map = (TileType[, ])Map.Clone();

        AOCExecutor.ActionForMainOnPerUpdate.Enqueue(() => RepairDroid.MapToTexturee(map, AOCUI.Instance.Part1ComputeOutput));
    }
Example #6
0
    public static long[] Compute(long[] memory, long startingPointer = 0)
    {
        var program  = new IntCodeProgram(memory, startingPointer);
        var compiler = new IntCodeCompiler();

        return(compiler.Compute(program));
    }
Example #7
0
            public void Part1_16DigitNumber()
            {
                var intcode  = new long[] { 1102, 34915192, 34915192, 7, 4, 7, 99, 0 };
                var compiler = new IntCodeCompiler();

                compiler.Compute(new IntCodeProgram(intcode, 0));
                Assert.AreEqual(34915192L * 34915192L, compiler.OutputValue);
            }
Example #8
0
            public void Part1_Relative2()
            {
                var compiler = new IntCodeCompiler();
                var program  = compiler.Compute(new IntCodeProgram(new long[] { 204, 0, 109, -1, 204, 5, 99 }, 0, 2));

                Assert.AreEqual(1, program.RelativeBaseOffset, "The relative Offset should be set to 1");
                Assert.AreEqual(10999, compiler.OutputValue);
            }
Example #9
0
            public void Part1_RelativeExampleComplete()
            {
                var compiler = new IntCodeCompiler();
                var program  = compiler.Compute(new IntCodeProgram(new long[] { 109, 1, 204, 1, 99 }, 0, 0));

                Assert.AreEqual(1, program.RelativeBaseOffset, "The relative Offset should be set to 1");
                Assert.AreEqual(204, compiler.OutputValue);
            }
Example #10
0
            public void Part1_RelativeExample()
            {
                var compiler = new IntCodeCompiler();
                var program  = compiler.Compute(new IntCodeProgram(new long[] { 104, 1, 104, 2, 104, 3, 104, 4, 99 }, 0, 0));

                Assert.AreEqual(false, compiler.Paused, "Should Not be paused");
                Assert.AreEqual(1234, compiler.OutputValue, "" + compiler.OutputAasListIntStr);
            }
Example #11
0
            public void Part1_LargestNumber()
            {
                var intcode  = new long[] { 104, 1125899906842624, 99 };
                var compiler = new IntCodeCompiler();

                compiler.Compute(new IntCodeProgram(intcode, 0));
                Assert.AreEqual(1125899906842624, compiler.OutputValue);
            }
Example #12
0
            public void Part1_QuineSelfReplicating()
            {
                var intcode  = new long[] { 109, 1, 204, -1, 1001, 100, 1, 100, 1008, 100, 16, 101, 1006, 101, 0, 99 };
                var compiler = new IntCodeCompiler();

                compiler.Compute(new IntCodeProgram(intcode.ToArray(), 0));
                IntCodeToBasic.ConvertWhileRunning(intcode.ToArray(), "Day9_Part1_QuineSelfReplicating.txt", false);
                TestUtility.AreEqual(intcode, compiler.OutputValues.ToArray());
            }
Example #13
0
            public void Part1_OutsideMemory()
            {
                var compiler = new IntCodeCompiler(new long[] { 69 });
                var program  = compiler.Compute(new IntCodeProgram(new long[] { 03, 5, 04, 5, 99 }, 0, 2));

                Assert.AreEqual(2, program.RelativeBaseOffset, "The relative Offset should be set to 0");
                TestUtility.AreEqual(new long[] { 03, 5, 04, 5, 99, 69 }, program.Memory.ToArray());
                Assert.AreEqual(69, compiler.OutputValue);
            }
Example #14
0
            public void Part1Answer()
            {
                var intcode = InputParser.ListOfLongs(Day5Input, ',');

                var compiler = new IntCodeCompiler(1);

                compiler.Compute(new IntCodeProgram(intcode, 0));

                Assert.AreEqual("30000000013978427", compiler.OutputConcat);
            }
Example #15
0
            public void Part2Answer()
            {
                var intcode = InputParser.ListOfLongs(Day5Input, ',');

                var compiler = new IntCodeCompiler(5);

                compiler.Compute(new IntCodeProgram(intcode, 0));

                Assert.AreEqual("11189491", compiler.OutputConcat);
            }
Example #16
0
    public static string Part1(string inputText)
    {
        var intcode  = InputParser.ListOfLongs(inputText, ',');
        var compiler = new IntCodeCompiler(1);
        var program  = new IntCodeProgram(intcode, 0);

        compiler.Compute(program);
        IntCodeToBasic.Convert(program, "D5Part1HumainCode.txt");

        return(compiler.OutputConcat);
    }
Example #17
0
    public static string Part2(string inputText)
    {
        var intcode = InputParser.ListOfLongs(inputText, ',');

        IntCodeToBasic.ConvertWhileRunning(new IntCodeCompiler(5), new IntCodeProgram(intcode.ToArray(), 0), "D5Part2HumainCode.txt", true);

        var compiler = new IntCodeCompiler(5);

        compiler.Compute(new IntCodeProgram(intcode, 0));

        return(compiler.OutputConcat);
    }
Example #18
0
            public void Part1()
            {
                var intcode = InputParser.ListOfLongs(Day9Input, ',');

                IntCodeToBasic.ConvertWhileRunning(new IntCodeCompiler(1), new IntCodeProgram(intcode.ToArray(), 0), "D9Part1_HumainCode.txt", false);

                var compiler = new IntCodeCompiler(1);

                compiler.Compute(new IntCodeProgram(intcode, 0));

                Assert.AreEqual(3533056970, compiler.OutputValue);
            }
Example #19
0
    public void Step()
    {
        var currentColor = 0L;

        compiler.Clear();
        if (Painting.ContainsKey(CurrentPosition))
        {
            currentColor = Painting[CurrentPosition].Last();
        }
        else
        {
            Painting.Add(CurrentPosition, new List <long>());
            currentColor = 0;
        }

        compiler.SetInputs(new long[] { currentColor });

        Program = compiler.Compute(Program);
        Program = compiler.Compute(Program);

        if (Program.IsDone)
        {
            return;
        }

        //	Debug.Log(compiler.OutputAasListIntStr);

        Painting[CurrentPosition].Add(compiler.OutputValues[0]);

        if (compiler.OutputValues[1] == 0)
        {
            CurrentDirection = LeftOf(CurrentDirection);
        }
        else
        {
            CurrentDirection = RightOf(CurrentDirection);
        }
        CurrentPosition = MoveForward(CurrentPosition, CurrentDirection);
    }
Example #20
0
            public void Part2()
            {
                var intcode = InputParser.ListOfLongs(Day9Input, ',');

                //Trop long !!
                //IntCodeToBasic.ConvertWhileRunning(new IntCodeCompiler(2), new IntCodeProgram(intcode.ToArray(), 0), "D9Part2_HumainCode.txt", false);

                var compiler = new IntCodeCompiler(2);

                compiler.Compute(new IntCodeProgram(intcode, 0));

                Assert.AreEqual(1930, compiler.OutputValue);
            }
Example #21
0
    public static long RunSequence(long[] intcode, long[] phases)
    {
        var  compiler = new IntCodeCompiler();
        long input    = 0;

        foreach (var phase in phases)
        {
            compiler.Clear();
            compiler.SetInputs(new long[] { phase, input });
            var program = new IntCodeProgram(intcode.ToArray(), 0);
            compiler.Compute(program);
            input = compiler.OutputValue;
        }

        return(input);
    }
Example #22
0
    public static int Part1(string inputText)
    {
        var intcode  = InputParser.ListOfLongs(inputText, ',');
        var compiler = new IntCodeCompiler(0);

        compiler.Compute(new IntCodeProgram(intcode, 0));

        var level   = CreateLevel(compiler.OutputValues.ToArray());
        int nbBlock = 0;

        for (int y = 0; y < level.GetLength(0); y++)
        {
            for (int x = 0; x < level.GetLength(1); x++)
            {
                if (level[y, x] == TileType.Block)
                {
                    nbBlock++;
                }
            }
        }

        return(nbBlock);
    }