Beispiel #1
0
    string RunPart2(InputAnswer puzzleData)
    {
        var phaseValues = new long[] { 5, 6, 7, 8, 9 };
        var answer      = 0L;

        foreach (var phase in GetPhases(phaseValues, puzzleData.Phase))
        {
            var ampA = new IntCode(puzzleData.Code);
            var ampB = new IntCode(puzzleData.Code);
            var ampC = new IntCode(puzzleData.Code);
            var ampD = new IntCode(puzzleData.Code);
            var ampE = new IntCode(puzzleData.Code);

            var outputValue = 0L;

            ampA.Output += (s, e) => outputValue = e.OutputValue;
            ampB.Output += (s, e) => outputValue = e.OutputValue;
            ampC.Output += (s, e) => outputValue = e.OutputValue;
            ampD.Output += (s, e) => outputValue = e.OutputValue;
            ampE.Output += (s, e) => outputValue = e.OutputValue;

            ampA.AddInput(phase[0]);
            ampB.AddInput(phase[1]);
            ampC.AddInput(phase[2]);
            ampD.AddInput(phase[3]);
            ampE.AddInput(phase[4]);

            while (true)
            {
                ampA.AddInput(outputValue);
                ampA.Run();

                ampB.AddInput(outputValue);
                ampB.Run();

                ampC.AddInput(outputValue);
                ampC.Run();

                ampD.AddInput(outputValue);
                ampD.Run();

                ampE.AddInput(outputValue);
                ampE.Run();
                if (ampE.State == IntCodeState.Finished)
                {
                    break;
                }
            }

            if (outputValue > answer)
            {
                answer = outputValue;
            }
        }

        return(Helper.GetPuzzleResultText($"Highest signal that can be sent to the thrusters: {answer}", answer, puzzleData.ExpectedAnswer));
    }
Beispiel #2
0
    string RunPart2(InputAnswer puzzleData)
    {
        TileGrid.Clear();
        outputCache.Clear();
        Score = 0;

        var arcade = new IntCode(puzzleData.Code);

        arcade.Output += ArcadeOutput;

        var ballPosition   = GetTilePositions(TileType.Ball);
        var PaddlePosition = GetTilePositions(TileType.Paddle);

        arcade.Poke(0, 2); // Put in 2 quarters

        while (arcade.State != IntCodeState.Finished)
        {
            arcade.Run();
            if (arcade.State == IntCodeState.NeedsInput)
            {
                var diff       = ballPosition.First().X - PaddlePosition.First().X;
                var inputValue = (diff == 0) ? 0 : diff / Math.Abs(diff);
                arcade.AddInput(inputValue);
            }
        }

        return(Helper.GetPuzzleResultText($"Final Score: {Score}", Score, puzzleData.ExpectedAnswer));
    }
Beispiel #3
0
    string RunBOOST(InputAnswer puzzleData, long initalInput)
    {
        var answer = default(string);

        var computer = new IntCode(puzzleData.Code);

        computer.Output += (s, e) => answer = e.OutputValue.ToString();

        computer.AddInput(initalInput);
        computer.Run();

        return(Helper.GetPuzzleResultText($"BOOST key-code: {answer}", answer, puzzleData.ExpectedAnswer));
    }
Beispiel #4
0
    string RunPart2(InputAnswer puzzleData)
    {
        var answer   = 0L;
        var computer = new IntCode(puzzleData.Code);

        computer.Output += (s, e) => { answer = e.OutputValue; };

        computer.Run();
        if (computer.State == IntCodeState.NeedsInput)
        {
            computer.AddInput(5);
            computer.Run();
        }

        return(Helper.GetPuzzleResultText($"The diagnostic Code is: {answer}", answer, puzzleData.ExpectedAnswer));
    }
Beispiel #5
0
 public void Start()
 {
     while (cpu.State != IntCodeState.Finished)
     {
         cpu.Run();
         if (cpu.State == IntCodeState.NeedsInput)
         {
             var args = new ScanHullColorEventArgs(myLocation);
             ScanHull(this, args);
             if (args.HullColor == HullColor.Undefined)
             {
                 throw new ApplicationException("Hull Paint color was not read!");
             }
             cpu.AddInput((long)args.HullColor);
         }
     }
 }
Beispiel #6
0
    string RunPart1(InputAnswer puzzleData)
    {
        var phaseValues = new long[] { 0, 1, 2, 3, 4 };
        var answer      = 0L;

        foreach (var phase in GetPhases(phaseValues, puzzleData.Phase))
        {
            var ampA = new IntCode(puzzleData.Code);
            var ampB = new IntCode(puzzleData.Code);
            var ampC = new IntCode(puzzleData.Code);
            var ampD = new IntCode(puzzleData.Code);
            var ampE = new IntCode(puzzleData.Code);

            var outputValue = 0L;

            ampA.Output += (s, e) => outputValue = e.OutputValue;
            ampB.Output += (s, e) => outputValue = e.OutputValue;
            ampC.Output += (s, e) => outputValue = e.OutputValue;
            ampD.Output += (s, e) => outputValue = e.OutputValue;
            ampE.Output += (s, e) => outputValue = e.OutputValue;

            ampA.AddInput(phase[0], outputValue);
            ampA.Run();

            ampB.AddInput(phase[1], outputValue);
            ampB.Run();

            ampC.AddInput(phase[2], outputValue);
            ampC.Run();

            ampD.AddInput(phase[3], outputValue);
            ampD.Run();

            ampE.AddInput(phase[4], outputValue);
            ampE.Run();

            if (outputValue > answer)
            {
                answer = outputValue;
            }
        }

        return(Helper.GetPuzzleResultText($"Highest signal that can be sent to the thrusters: {answer}", answer, puzzleData.ExpectedAnswer));
    }
Beispiel #7
0
    string RunPart1(InputAnswer puzzleData, long inputValue, long?overrideExpectedAnswer = null)
    {
        if (overrideExpectedAnswer.HasValue)
        {
            puzzleData.ExpectedAnswer = overrideExpectedAnswer.Value;
        }

        var answer   = 0L;
        var computer = new IntCode(puzzleData.Code);

        computer.Output += (s, e) => { answer = e.OutputValue; };

        computer.Run();
        if (computer.State == IntCodeState.NeedsInput)
        {
            computer.AddInput(inputValue);
            computer.Run();
        }

        return(Helper.GetPuzzleResultText($"The diagnostic Code is: {answer}", answer, puzzleData.ExpectedAnswer));
    }
Beispiel #8
0
    string RunPart2Example3(long inputValue)
    {
        var sb        = new StringBuilder();
        var inputList = InputHelper.LoadInputFile(5, "example3")
                        .Select(l => l.Split(',').Select(v => v.ToInt64()).ToList());

        sb.AppendLine($"Input: {inputValue}");
        foreach (var input in inputList)
        {
            var computer = new IntCode(input);
            computer.Output += (s, e) => sb.AppendLine($"Output: {e.OutputValue}");

            computer.Run();
            if (computer.State == IntCodeState.NeedsInput)
            {
                computer.AddInput(inputValue);
                computer.Run();
            }
        }
        sb.AppendLine();

        return(sb.ToString());
    }
Beispiel #9
0
 public void AddInput(Direction direction)
 {
     cpu.AddInput((long)direction);
     MoveDirection = direction;
 }