Exemplo n.º 1
0
            public long GetPosition(ref IntcodeComputer computer)
            {
                if (Adress > computer.CurrentMemoryState().Length)
                {
                    computer.ResizeMemory(Adress);
                }
                if (RelativeBase + computer.CurrentMemoryState()[Adress] > computer.CurrentMemoryState().Length)
                {
                    computer.ResizeMemory((int)(RelativeBase + computer.CurrentMemoryState()[Adress]));
                }

                if (Mode == ParameterMode.Position)
                {
                    var pos = computer.CurrentMemoryState()[Adress];
                    if (pos >= computer.CurrentMemoryState().Length)
                    {
                        computer.ResizeMemory((int)pos);
                    }
                    return(pos);
                }
                else if (Mode == ParameterMode.Relative)
                {
                    var pos = RelativeBase + computer.CurrentMemoryState()[Adress];
                    if (pos >= computer.CurrentMemoryState().Length)
                    {
                        computer.ResizeMemory((int)pos);
                    }
                    return(pos);
                }
                else
                {
                    throw new NotImplementedException($"Parameter Mode must be {ParameterMode.Position}");
                }
            }
Exemplo n.º 2
0
            public long GetValue(ref IntcodeComputer computer)
            {
                if (Adress > computer.CurrentMemoryState().Length)
                {
                    computer.ResizeMemory(Adress);
                }

                if (Mode == ParameterMode.Immediate)
                {
                    return(computer.CurrentMemoryState()[Adress]);
                }
                else if (Mode == ParameterMode.Position || Mode == ParameterMode.Relative)
                {
                    var adress = GetPosition(ref computer);
                    return(computer.CurrentMemoryState()[adress]);
                }
                else
                {
                    throw new NotImplementedException($"Unexpected Parameter Mode: {Mode}");
                }
            }