예제 #1
0
        public static long Part1Solution(long[] input)
        {
            IntCodeProgram intCodeProgram = new IntCodeProgram(input);

            string[] commands = new string[]
            {
                "NOT A T\n",
                "OR T J\n",
                "NOT B T\n",
                "OR T J\n",
                "NOT C T\n",
                "OR T J\n",
                "AND D J\n",
                "WALK\n"
            };

            foreach (var command in commands)
            {
                foreach (var character in command)
                {
                    intCodeProgram.Input.Enqueue(character);
                }
            }

            intCodeProgram.Run();

            while (intCodeProgram.Output.Count > 1)
            {
                Console.Write((char)intCodeProgram.Output.Dequeue());
            }
            return(intCodeProgram.Output.Dequeue());
        }
예제 #2
0
        public static int Part1Solution(long[] input)
        {
            /*
             * Items needed:
             *  - Easter egg
             *  - Mug
             *  - Sand
             *  - Space heater
             */

            IntCodeProgram intCodeProgram = new IntCodeProgram(input);

            while (true)
            {
                string command = Console.ReadLine() + "\n";
                foreach (var character in command)
                {
                    intCodeProgram.Input.Enqueue(character);
                }

                var halt = intCodeProgram.Run();

                while (intCodeProgram.Output.Count > 0)
                {
                    Console.Write((char)intCodeProgram.Output.Dequeue());
                }
            }

            return(0);
        }
예제 #3
0
        public static long Part1Solution(long[] input)
        {
            var program = new IntCodeProgram(input);

            program.Run();
            return(program.GetFirstPosition());
        }
예제 #4
0
        public void Should_ComputeCorrectSignal(int[] expected, int[] data)
        {
            var program = IntCodeProgram.New();

            program.Compute(data);
            Assert.Equal(expected, program.Output.Select(val => (int)val).ToArray());
        }
예제 #5
0
        public static long Part1Solution(long[] input)
        {
            IntCodeProgram[] amplifiers   = new IntCodeProgram[5];
            long             highestScore = Normal(input, amplifiers);

            return(highestScore);
        }
예제 #6
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));
    }
예제 #7
0
    private IntCodeProgram Equals(IntCodeProgram program)
    {
        var result = (program.ParameterAValue == program.ParameterBValue) ? 1 : 0;

        program[program.ParameterCPointer] = result;
        return(new IntCodeProgram(program, program.Pointer + 4, program.RelativeBaseOffset));
    }
예제 #8
0
    public static void ConvertWhileRunning(IntCodeCompiler intCodeCompiler, IntCodeProgram program, string filename, bool useExecutorActionForMain)
    {
        string output   = "";
        int    maxSteps = 100;

        while (!program.IsDone || maxSteps-- > 0)
        {
            var instruction = program.OpCodeAtPointer;
            var opcode      = instruction % 100;
            if (opcode == 99)
            {
                output += "Halt";
                break;
            }
            else if (opcode > intCodeCompiler.Instructions.Length || intCodeCompiler.Instructions[opcode] == null)
            {
                output += $"! Missing Instruction {opcode}!\n";
                break;
            }
            else
            {
                output += InstructionToCode(program, instruction, opcode, program.Pointer);
                program = intCodeCompiler.ComputeStep(program);
            }
        }

        if (useExecutorActionForMain)
        {
            AOCExecutor.ActionForMain.Enqueue(() => AOCInput.WriteToFile(filename, output));
        }
        else
        {
            AOCInput.WriteToFile(filename, output);
        }
    }
예제 #9
0
    public static void Convert(IntCodeProgram program, string filename)
    {
        var    compiler = new IntCodeCompiler();
        string output   = "";
        long   pointer  = 0;

        while (pointer < program.MemoryLength)
        {
            var instruction = program[pointer];
            var opcode      = instruction % 100;
            if (opcode == 99)
            {
                output += "Halt";
                break;
            }
            else if (opcode > compiler.Instructions.Length || compiler.Instructions[opcode] == null)
            {
                output += "! Missing Instruction !\n";
                break;
            }
            else
            {
                output  += InstructionToCode(program, instruction, opcode, pointer);
                pointer += compiler.InstructionsSkips[opcode];
            }
        }
        if (Application.isPlaying)
        {
            AOCExecutor.ActionForMain.Enqueue(() => AOCInput.WriteToFile(filename, output));
        }
        else
        {
            AOCInput.WriteToFile(filename, output);
        }
    }
예제 #10
0
        private static int FeedbackSet(long[] input, IntCodeProgram[] amplifiers, List <int> phaseSettingSequence)
        {
            for (int i = 0; i < amplifiers.Length; i++)
            {
                amplifiers[i] = new IntCodeProgram(input, phaseSettingSequence[i]);
            }
            amplifiers[0].Input.Enqueue(0);

            Queue <IntCodeProgram> amplifiersQueue = new Queue <IntCodeProgram>(amplifiers);
            Queue <long>           nextInput       = new Queue <long>();

            while (amplifiersQueue.Count > 0)
            {
                IntCodeProgram amp = amplifiersQueue.Dequeue();
                OutputToInput(nextInput, amp.Input);
                Halt haltType = amp.Run();
                OutputToInput(amp.Output, nextInput);

                if (haltType == Halt.NeedInput)
                {
                    amplifiersQueue.Enqueue(amp);
                }
            }

            return((int)nextInput.Single());
        }
예제 #11
0
    private IntCodeProgram Input(IntCodeProgram program)
    {
        var input = NextInput();

        program[program.ParameterAPointer] = input;
        return(new IntCodeProgram(program.Memory, program.Pointer + 2, program.RelativeBaseOffset));
    }
예제 #12
0
    public static long[] ComputeStep(long[] memory, long startingPointer = 0)
    {
        var program  = new IntCodeProgram(memory, startingPointer);
        var compiler = new IntCodeCompiler();

        return(compiler.ComputeStep(program));
    }
예제 #13
0
        public static long Part2Solution(long[] input)
        {
            long currentScore      = 0;
            long currentBlockTiles = -1;

            input[0] = 2;
            List <GridPosition> positions      = new List <GridPosition>();
            IntCodeProgram      intCodeProgram = new IntCodeProgram(input);

            GridPosition horizontalPaddlePosition = new GridPosition(0, 0, Tile.HorizontalPaddle);
            GridPosition ballPosition             = new GridPosition(0, 0, Tile.Ball);

            while (currentBlockTiles != 0)
            {
                intCodeProgram.Run();

                (currentScore, ballPosition, horizontalPaddlePosition) = ProcessIntCodeOutput(positions, intCodeProgram);

                ProvideInput(intCodeProgram, ballPosition, horizontalPaddlePosition);

                currentBlockTiles = positions.Count(pos => pos.Tile == Tile.Block);
            }

            return(currentScore);
        }
예제 #14
0
        public static long Part2Solution(long[] originalNumbersArray)
        {
            var memoryNumbersArray = new long[originalNumbersArray.Length];

            bool found = false;
            long noun  = 0;
            long verb  = 0;

            for (var i = 0; i <= 99; i++)
            {
                for (var j = 0; j <= 99; j++)
                {
                    originalNumbersArray.CopyTo(memoryNumbersArray, 0);
                    memoryNumbersArray[1] = i;
                    memoryNumbersArray[2] = j;
                    IntCodeProgram intProgram = new IntCodeProgram(memoryNumbersArray);
                    intProgram.Run();

                    if (intProgram.GetFirstPosition() == 19690720)
                    {
                        noun  = i;
                        verb  = j;
                        found = true;
                        break;
                    }
                }

                if (found)
                {
                    break;
                }
            }

            return((100 * noun) + verb);
        }
예제 #15
0
        public static long Part1Solution(long[] input)
        {
            var program = new IntCodeProgram(input, 1);

            program.Run();
            return(program.Output.Last());
        }
예제 #16
0
    public static int Part2(string inputText)
    {
        var program = new IntCodeProgram(InputParser.ListOfLongs(inputText, ','), 0, 0);
        var robot   = new EHPR(program);

        robot.Painting.Add(new Vector2Int(0, 0), new List <long>(new long[] { 1 }));
        robot.Run();

        AOCExecutor.ActionForMain.Enqueue(() => WriteToFilePainting(robot.Painting, "Day11_Part2.txt"));
        var bounds = MathUtils.FindBound(robot.Painting.Keys.ToList());
        int xOff   = -bounds.xMin;
        int yOff   = -bounds.yMin;

        Debug.Log(bounds);
        var image = new int[bounds.size.y + 1, bounds.size.x + 1];

        foreach (var pt in robot.Painting)
        {
            if (pt.Key.x < bounds.min.x || pt.Key.x > bounds.max.x || pt.Key.y < bounds.min.y || pt.Key.y > bounds.max.y)
            {
                Debug.Log($"Contains pas {pt.Key}");
            }
            if (pt.Value.Count == 0)
            {
                continue;
            }
            var value = pt.Value.Last();
            image[bounds.size.y - (pt.Key.y + yOff), pt.Key.x + xOff] = (int)value;
        }

        AOCExecutor.ActionForMain.Enqueue(() => Day8Main.ImageToTexturee(image, bounds.size.x + 1, bounds.size.y + 1, AOCUI.Instance.Part1ComputeOutput));

        return(2);
    }
예제 #17
0
    public static int Part1(string inputText)
    {
        var program = new IntCodeProgram(InputParser.ListOfLongs(inputText, ','), 0, 0);
        var robot   = new RepairDroid(program);

        robot.FindOxygenSystem();
        return(1);
    }
예제 #18
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);
    }
예제 #19
0
 public IntCodeProgram Compute(IntCodeProgram program)
 {
     Paused = false;
     while (!program.IsDone && !Paused)
     {
         program = ComputeStep(program);
     }
     return(program);
 }
예제 #20
0
        public static int Part1Solution(long[] input)
        {
            IntCodeProgram  intCodeProgram = new IntCodeProgram(input);
            List <Position> positions      = new List <Position>();

            intCodeProgram.Run();
            FillPositions(intCodeProgram.Output, positions);
            return(CalculateIntersections(positions));
        }
예제 #21
0
    public static int Part1(string inputText)
    {
        var program = new IntCodeProgram(InputParser.ListOfLongs(inputText, ','), 0, 0);
        var robot   = new EHPR(program);

        robot.Run();

        return(robot.Painting.Count);
    }
예제 #22
0
        public static long Part1Solution(long[] input)
        {
            List <GridPosition> positions      = new List <GridPosition>();
            IntCodeProgram      intCodeProgram = new IntCodeProgram(input);

            intCodeProgram.Run();

            ProcessIntCodeOutput(positions, intCodeProgram);
            return(positions.Count(position => position.Tile == Tile.Block));
        }
예제 #23
0
        public void Should_Output16DigitNumber()
        {
            var data    = new int[] { 1102, 34915192, 34915192, 7, 4, 7, 99, 0 };
            var program = IntCodeProgram.New();

            program.Compute(IntCodeData.FromIntArray(data));
            var number = program.Output.Single();

            Assert.Equal(16, number.ToString().Length);
        }
예제 #24
0
        public void Should_OutputLargeNumberInTheMiddle()
        {
            var data    = new BigInteger[] { 104, 1125899906842624, 99 };
            var program = IntCodeProgram.New();

            program.Compute(IntCodeData.FromBigIntArray(data));
            var number = program.Output.Single();

            Assert.Equal(IntCodeValue.FromBigInteger(data[1]), number);
        }
예제 #25
0
 private static void InitPrograms(this List <IntCodeProgram> intCodePrograms, long[] input)
 {
     for (var i = 0; i < 50; i++)
     {
         var intCodeProgram = new IntCodeProgram(input);
         intCodeProgram.Input.Enqueue(i);
         intCodeProgram.Run();
         intCodePrograms.Add(intCodeProgram);
     }
 }
예제 #26
0
        public static int Part2Solution(long[] input)
        {
            int             paintedAtLeastOnce = 0;
            List <GridMark> gridMarks          = new List <GridMark>();
            var             currentPosition    = new GridMark(0, 0, Colors.Black);
            var             currentDirection   = Direction.Up;

            IntCodeProgram intCodeProgram = new IntCodeProgram(input);

            while (true)
            {
                if (gridMarks.Count == 0)
                {
                    currentPosition.Color = Colors.White;
                }
                else
                {
                    currentPosition.Color = GetPositionColor(currentPosition, gridMarks);
                }

                if (currentPosition.Color == Colors.Black)
                {
                    intCodeProgram.Input.Enqueue(0);
                }
                else
                {
                    intCodeProgram.Input.Enqueue(1);
                }

                var type = intCodeProgram.Run();

                if (type == Halt.Terminated)
                {
                    Print(gridMarks);
                    return(paintedAtLeastOnce);
                }

                currentPosition.Color = (Colors)intCodeProgram.Output.Dequeue();
                long directionToTurn = intCodeProgram.Output.Dequeue();

                GridMark existingMark = gridMarks.SingleOrDefault(pos => pos.X == currentPosition.X && pos.Y == currentPosition.Y);
                if (existingMark != null)
                {
                    gridMarks.Remove(existingMark);
                }
                else
                {
                    paintedAtLeastOnce++;
                }
                gridMarks.Add(new GridMark(currentPosition.X, currentPosition.Y, currentPosition.Color));

                currentDirection = SetDirection(currentDirection, directionToTurn);
                currentPosition  = SetPosition(currentPosition, currentDirection);
            }
        }
예제 #27
0
        public void Part1()
        {
            var program = new IntCodeProgram(InputParser.ListOfLongs(Day11Input, ','), 0, 0);
            var robot   = new EHPR(program);

            robot.Run();

            WriteToFilePainting(robot.Painting, "D11_Part1.txt");

            Assert.AreEqual(1, robot.Painting.Count);
        }
예제 #28
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);
    }
예제 #29
0
 private IntCodeProgram JumpIfFalse(IntCodeProgram program)
 {
     if (program.ParameterAValue == 0)
     {
         return(new IntCodeProgram(program, program.ParameterBValue, program.RelativeBaseOffset));
     }
     else
     {
         return(new IntCodeProgram(program, program.Pointer + 3, program.RelativeBaseOffset));
     }
 }
예제 #30
0
        public void Part2()
        {
            var program = new IntCodeProgram(InputParser.ListOfLongs(Day11Input, ','), 0, 0);
            var robot   = new EHPR(program);

            robot.Painting.Add(new Vector2Int(0, 0), new List <long>(new long[] { 1 }));
            robot.Run();

            WriteToFilePainting(robot.Painting, "D11_Part2.txt");

            Assert.AreEqual(1, robot.Painting.Count);
        }