Пример #1
0
        public int TryToFixVmAndReturnCorrectAccumulator()
        {
            if (IsInfinite())
            {
                for (var i = 0; i < _instructions.Length; i++)
                {
                    var arr = new Instruction[_instructions.Length];
                    _instructions.CopyTo(arr, 0);
                    arr[i] = _instructions[i].CommandType switch
                    {
                        Instruction.Command.Nop => new Instruction(Instruction.Command.Jmp, _instructions[i].Value),
                        Instruction.Command.Jmp => new Instruction(Instruction.Command.Nop, _instructions[i].Value),
                        Instruction.Command.Acc => null,
                        _ => throw new ArgumentOutOfRangeException()
                    };
                    if (arr[i] == null)
                    {
                        continue;
                    }
                    var vm = new Vm(arr);
                    if (!vm.IsInfinite())
                    {
                        vm.CleanUp();
                        return(vm.ReturnAccumulator());
                    }
                }

                throw new SystemException();
            }
            else
            {
                return(ReturnAccumulator());
            }
        }
Пример #2
0
        static void Main(string[] args)
        {
            var vm = new Vm(File.ReadLines("input.txt").Select(Instruction.Parse));

            Console.WriteLine(vm.ReturnAccumulator());
            vm.CleanUp();
            Console.WriteLine(vm.TryToFixVmAndReturnCorrectAccumulator());
        }