Exemplo n.º 1
0
    /// <summary>
    /// Gets the number of block tiles on the screen after the game is run.
    /// </summary>
    /// <param name="program">The Intcode program to run.</param>
    /// <param name="cancellationToken">The cancellation token to use.</param>
    /// <returns>
    /// The number of block tiles on the screen.
    /// </returns>
    public static async Task <(long BlockTileCount, long Score)> PlayGameAsync(
        string program,
        CancellationToken cancellationToken)
    {
        long[] instructions = IntcodeVM.ParseProgram(program);

        var vm = new IntcodeVM(instructions, 10_000)
        {
            Input = await ChannelHelpers.CreateReaderAsync(new[] { 2L }, cancellationToken),
        };

        if (!await vm.RunAsync(cancellationToken))
        {
            throw new PuzzleException("Failed to run program.");
        }

        var outputs = await vm.Output.ToListAsync(cancellationToken);

        var grid = new Dictionary <Point, int>(outputs.Count);

        long score = 0;

        for (int i = 0; i < outputs.Count; i += 3)
        {
            long x      = outputs[i];
            long y      = outputs[i + 1];
            long tileId = outputs[i + 2];

            if (x == 1 && y == 0)
            {
                score = tileId;
            }
            else
            {
                grid[new((int)x, (int)y)] = (int)tileId;
Exemplo n.º 2
0
    /// <summary>
    /// Runs the specified Intcode program.
    /// </summary>
    /// <param name="program">The Intcode program to run.</param>
    /// <param name="adjust">Whether to adjust the state for <c>1202 program alarm</c> before running.</param>
    /// <param name="cancellationToken">The optional cancellation token to use.</param>
    /// <returns>
    /// The memory values of the program once run.
    /// </returns>
    public static async Task <IReadOnlyList <long> > RunProgramAsync(
        string program,
        bool adjust = false,
        CancellationToken cancellationToken = default)
    {
        long[] instructions = IntcodeVM.ParseProgram(program);

        if (adjust)
        {
            instructions[1] = 12;
            instructions[2] = 2;
        }

        var vm = new IntcodeVM(instructions)
        {
            Input = await ChannelHelpers.CreateReaderAsync(new[] { 0L }, cancellationToken),
        };

        if (!await vm.RunAsync(cancellationToken))
        {
            throw new PuzzleException("Failed to run program.");
        }

        return(vm.Memory().ToArray());
    }
Exemplo n.º 3
0
    /// <summary>
    /// Runs the specified Intcode program as an asynchronous operation.
    /// </summary>
    /// <param name="program">The Intcode program to run.</param>
    /// <param name="input">The input to the program.</param>
    /// <param name="cancellationToken">The cancellation token to use.</param>
    /// <returns>
    /// The output of the program once run.
    /// </returns>
    internal static async Task <IReadOnlyList <long> > RunAsync(
        IEnumerable <long> program,
        long[] input,
        CancellationToken cancellationToken)
    {
        var vm = new IntcodeVM(program)
        {
            Input = await ChannelHelpers.CreateReaderAsync(input, cancellationToken),
        };

        if (!await vm.RunAsync(cancellationToken))
        {
            throw new PuzzleException("Failed to run program.");
        }

        return(await vm.Output.ToListAsync(cancellationToken));
    }
Exemplo n.º 4
0
    /// <summary>
    /// Runs the specified Intcode program.
    /// </summary>
    /// <param name="program">The Intcode program to run.</param>
    /// <param name="input">The input to the program.</param>
    /// <param name="cancellationToken">The cancellation token to use.</param>
    /// <returns>
    /// The diagnostic code output by the program.
    /// </returns>
    public static async Task <long> RunProgramAsync(
        string program,
        long input,
        CancellationToken cancellationToken)
    {
        long[] instructions = IntcodeVM.ParseProgram(program);

        var vm = new IntcodeVM(instructions)
        {
            Input = await ChannelHelpers.CreateReaderAsync(new[] { input }, cancellationToken),
        };

        if (!await vm.RunAsync(cancellationToken))
        {
            throw new PuzzleException("Failed to run program.");
        }

        var outputs = await vm.Output.ToListAsync(cancellationToken);

        return(outputs.Count == 0 ? 0 : outputs[^ 1]);