/// <summary> /// Runs the specified program. /// </summary> /// <param name="instructions">The program instructions.</param> /// <returns> /// A <see cref="Cpu"/> containing the value of each CPU register at the end of the program. /// </returns> private static Cpu RunProgram(ICollection <string> instructions) { var program = instructions .Select((p) => new Instruction(p)) .ToList(); var cpu = new Cpu(); foreach (Instruction instruction in program) { int conditionRegisterValue = cpu.GetValueOrDefault(instruction.ConditionRegister); if (!Evaluate(conditionRegisterValue, instruction.ConditionComparand, instruction.ConditionOperator)) { continue; } if (!cpu.ContainsKey(instruction.TargetRegister)) { cpu[instruction.TargetRegister] = 0; } if (instruction.TargetOperator) { cpu[instruction.TargetRegister] += instruction.OperatorValue; } else { cpu[instruction.TargetRegister] -= instruction.OperatorValue; } cpu.HighestValue = Math.Max(cpu.HighestValue, cpu.Values.DefaultIfEmpty().Max()); } return(cpu); }