Пример #1
0
        static void P1()
        {
            var lines = Utilities.GetStringFromFile("Day15.txt").SplitLongArrayFromString(',');

            var vm = new IntCodeVM(new IntCodeVMConfiguration()
            {
            });

            vm.WriteProgram(lines);
            grid[25, 25] = 'S';

            Draw(grid);



            vm.WriteInput((long)Facing.north);

            //vm.WriteMemory(0, 1);
            while (vm.RunProgram() == HALTTYPE.HALT_WAITING)
            {
                processOutputs(vm);

                if (complete)
                {
                    break;
                }

                //what type of step was taken?
                switch (stepType)
                {
                case StepType.forward:
                    //was step forward successful?  if not turn left
                    if (!stepSuccess)
                    {
                        TurnDirection(Direction.left);
                    }
                    //attempt to turn right now.
                    stepType = StepType.right;
                    vm.WriteInput((long)GetTurnDirection(Direction.right));
                    break;

                case StepType.right:
                    //if step was successful change facing and try to turn right again
                    if (stepSuccess)
                    {
                        TurnDirection(Direction.right);
                        stepType = StepType.right;
                        vm.WriteInput((long)GetTurnDirection(Direction.right));
                    }
                    //step unsuccessful, continue forward.
                    else
                    {
                        vm.WriteInput((long)currentFacing);
                        stepType = StepType.forward;
                    }
                    break;

                default:
                    break;
                }

                //Draw(grid);
            }

            Draw(grid);

            int[] target = new int[] { 0, 0 };

            for (int i = 0; i < grid.GetLength(0); i++)
            {
                for (int j = 0; j < grid.GetLength(1); j++)
                {
                    if (grid[i, j] == 'O')
                    {
                        target = new int[] { i, j };
                    }
                }
            }

            var s = new seeker(target, 0, grid, new int[] { 25, 25 }, Facing.north);

            var result = s.Seek();

            Console.WriteLine($"Min Steps to get to Oxygen Control: {result.Value}");
        }