public Instruction(int address, IntCodeCpuMemory memory) { const int opcodeSize = 1; Address = address; OperationCode = (int)memory.Read(address, ParameterMode.Immediate); Operation = (Operations)(OperationCode % 100); Size = InstructionSizeLookup(Operation); ParameterCount = ParameterCountLookup(Operation); Parameters = new List <Parameter>(ParameterCount); for (var i = 0; i < ParameterCount; i++) { int parameterCodes = OperationCode / 100; int parameterAddress = i + (address + opcodeSize); int divider = (int)Math.Pow(10, i); int parameterBit = parameterCodes / divider; int parameterModeValue = parameterBit % 10; var parameterMode = (ParameterMode)parameterModeValue; var parameter = new Parameter() { Address = parameterAddress, Value = memory[parameterAddress], Mode = parameterMode, ResolvedValue = memory.Read(parameterAddress, parameterMode) }; Parameters.Add(parameter); } }
public static BigInteger ReadOperand(this Instruction instruction, IntCodeCpuMemory memory, int index) { return(memory.Read(instruction.Parameters[index].Address, instruction.Parameters[index].Mode)); }