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}"); } }
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}"); } }
public override InstructionResult Execute(IntcodeComputer computer, long[] buffer) { try { bool equal = Parameters[0].GetValue(ref computer) == Parameters[1].GetValue(ref computer); var dest = Parameters[2].GetPosition(ref computer); computer.CurrentMemoryState()[dest] = equal ? 1 : 0; return(new InstructionResult(true, "EqualsInstruction passed.")); } catch (Exception e) { return(new InstructionResult(false, e.Message)); } }
public override InstructionResult Execute(IntcodeComputer computer, long[] buffer) { try { var memory = computer.CurrentMemoryState(); if (Parameters[0].GetValue(ref computer) == 0) { _next = (int)Parameters[1].GetValue(ref computer); } return(new InstructionResult(true, "JumpIfFalseInstruction passed.")); } catch (Exception e) { return(new InstructionResult(false, e.Message)); } }