コード例 #1
0
ファイル: Day17.cs プロジェクト: w200338/Advent-of-Code-2019
        /// <inheritdoc />
        public string Part2()
        {
            IntCodeVM2 vm = new IntCodeVM2(program);

            vm.SetMemory(0, 2); // put into different mode

            // input
            // L,12, L,12, R,12,
            // L,12, L,12, R,12
            // L,8, L,8, R,12, L,8, L,8,
            // L,10, R,8, R,12,
            // L,10, R,8, R,12,
            // L,12, L,12, R,12
            // L,8, L,8, R,12, L,8, L,8,
            // L,10, R,8, R,12,
            // L,12, L,12, R,12,
            // L,8, L,8, R,12, L,8, L,8
            //
            // A, A, B, C, C, A, B, C, A, B
            // A L,12, L,12, R,12
            // B L,8, L,8, R,12, L,8, L,8
            // C L,10, R,8, R,12,

            List <long> inputs = new List <long>()
            {
                65, 44, 65, 44, 66, 44, 67, 44, 67, 44, 65, 44, 66, 44, 67, 44, 65, 44, 66, 10,
                76, 44, 49, 50, 44, 76, 44, 49, 50, 44, 82, 44, 49, 50, 10,
                76, 44, 56, 44, 76, 44, 56, 44, 82, 44, 49, 50, 44, 76, 44, 56, 44, 76, 44, 56, 10,
                76, 44, 49, 48, 44, 82, 44, 56, 44, 82, 44, 49, 50, 10,
                110, 10
            };

            foreach (long l in inputs)
            {
                vm.AddInput(l);
            }

            vm.ExecuteProgram();

            List <long> outputs = vm.GetOutputs();

            int line      = 0;
            int posInLine = 0;

            List <Vector2Int> scaffold = new List <Vector2Int>();
            long total = 0;


            foreach (long c in outputs)
            {
                switch (c)
                {
                // new line
                case 10:
                    Console.WriteLine();
                    line++;
                    posInLine = 0;
                    continue;
                    break;

                // #
                case 35:
                    Console.Write("#");
                    scaffold.Add(new Vector2Int(posInLine, line));
                    break;

                // .
                case 46:
                    Console.Write(".");
                    break;

                // <
                case 60:
                    Console.Write("<");
                    break;

                // >
                case 62:
                    Console.Write(">");
                    break;

                // ^
                case 94:
                    Console.Write("^");
                    break;

                // v
                case 118:
                    Console.Write("v");
                    break;


                // x
                case 120:
                    Console.Write("x");
                    //Console.WriteLine("");
                    //return "It's dead Jim!";
                    break;


                default:
                    if (c < 255)
                    {
                        Console.Write((char)c);
                    }
                    else
                    {
                        Console.WriteLine($"\n{c}");
                    }
                    total += c;
                    break;
                }

                posInLine++;
            }

            // higher than 5624
            //var test = outputs.Skip(3121).ToList();

            return($"total {total}");
        }
コード例 #2
0
        /// <inheritdoc />
        public string Part2()
        {
            IntCodeVM2 vm = new IntCodeVM2(longs);

            vm.SetMemory(0, 2);
            vm.ExecuteProgram();

            List <int> output = vm.GetOutputs().Select(l => (int)l).ToList();

            Dictionary <Vector2Int, int> tiles = new Dictionary <Vector2Int, int>();
            int finalScore = 0;

            // draw tiles
            for (int i = 0; i < output.Count; i += 3)
            {
                // get score
                if (output[i] == -1)
                {
                    finalScore = output[i + 2];
                    continue;
                }

                // create new tiles
                tiles.Add(new Vector2Int(output[i], output[i + 1]), output[i + 2]);
            }

            // as long as breakable blocks remain
            while (tiles.Values.Count(t => t == 2) > 0)
            {
                // redraw tiles
                output = vm.GetOutputs().Select(l => (int)l).ToList();
                for (int i = 0; i < output.Count; i += 3)
                {
                    // get score
                    if (output[i] == -1)
                    {
                        finalScore = output[i + 2];
                        Console.WriteLine($"Current score {finalScore}");
                        continue;
                    }

                    Vector2Int pos = new Vector2Int(output[i], output[i + 1]);
                    if (tiles.ContainsKey(pos))
                    {
                        tiles[pos] = output[i + 2];
                    }
                    else
                    {
                        tiles.Add(pos, output[i + 2]);
                    }
                }

                // show screen
                if (humanInput)
                {
                    DrawScreen(tiles);

                    // do a move
                    switch (Console.ReadKey().Key)
                    {
                    case ConsoleKey.DownArrow:
                        vm.AddInput(0);
                        break;

                    case ConsoleKey.LeftArrow:
                        vm.AddInput(-1);
                        break;

                    case ConsoleKey.RightArrow:
                        vm.AddInput(+1);
                        break;
                    }
                }
                else
                {
                    // get paddle pos
                    Vector2Int paddle = tiles.Keys.ToList()[tiles.Values.ToList().FindIndex(i => i == 3)];

                    // get ball pos
                    Vector2Int ball = tiles.Keys.ToList()[tiles.Values.ToList().FindIndex(i => i == 4)];

                    // determine next move
                    if (ball.X > paddle.X)
                    {
                        vm.AddInput(1);
                    }
                    else if (ball.X < paddle.X)
                    {
                        vm.AddInput(-1);
                    }
                    else
                    {
                        vm.AddInput(0);
                    }
                }

                // run again
                vm.ClearOutput();
                vm.ResumeProgram();
            }



            return($"final score {finalScore}");
        }