static void Main(string[] args) { var container = Containers.ContainerBuilder.BuildContainer(); IInputProvider inputProvider = null; InputProviderSource source = InputProviderSource.Demo; var inputProviderFactory = container.Resolve <IInputProviderFactory>(); do { Console.WriteLine("Please specify the source or input:"); Console.WriteLine("1: Console"); Console.WriteLine("2: Demo"); Console.WriteLine("----------"); Console.WriteLine("0: Quit"); var selection = Console.ReadLine(); try { var index = int.Parse(selection ?? DefaultSelection); if (index == QuitValue) { return; // exiting from the application } source = (InputProviderSource)index; inputProvider = inputProviderFactory.GetInputProvider(source); if (inputProvider == null) { throw new IndexOutOfRangeException("Invalid input"); } } catch (Exception ex) when(ex is FormatException || ex is IndexOutOfRangeException) { Console.WriteLine($"Error {ex.Message}. Please enter a number 1, 2 or 0 to quit"); } } while (inputProvider == null); var input = inputProvider.GetInput(); if (source == InputProviderSource.Demo && input != null) { Console.WriteLine(input); } var calculator = container.Resolve <IWordsCalculator>(); var results = calculator.Run(input); var outputBuilder = container.Resolve <IOutputBuilder>(); outputBuilder.BuildOutput(results, Console.Out); Console.WriteLine(); Console.WriteLine("Done. Press any key to exit"); Console.ReadKey(); }
public static string Compile(IInputProvider inputProvider, CultureInfo cultureInfo = null, ICompiler[] compilers = null) { if (cultureInfo == null) { cultureInfo = CultureInfo.InvariantCulture; } Compiler.cultureInfo = cultureInfo; compilers = compilers ?? AllCompilers; return(compilers.Aggregate(inputProvider.GetInput(), (input, compiler) => compiler.Compile(input, cultureInfo))); }
public void MainLoop() { string input = ""; decimal x; string symbol; decimal y; input = _inputProvider.GetInput(); _parseProvider.ParseInput(input, out x, out symbol, out y); decimal sum = _mathProvider.Calculate(symbol, x, y); _outputProvider.OutPut(sum); }
private byte[] GetDataForOutput() { return(_Input.GetInput()); }
public IntcodeProgramStatus RunProgram() { IntcodeProgramStatus status = IntcodeProgramStatus.Running; while (true) { LogDebugMessage($"Pos: {_position.ToString("0000")}, Cmd: {_program[_position]}"); var parsedCommand = ParseCommand(_program[_position]); var opcode = parsedCommand[0]; if (opcode == 1) { // Add param1 + param2, store in address pointed to by param3 var val1 = GetParameterValue(_position + 1, 1, parsedCommand); var val2 = GetParameterValue(_position + 2, 2, parsedCommand); var val3 = GetParameterWritePosition(_position + 3, 3, parsedCommand); SetMemoryValue(val3, val1 + val2); _position += 4; } else if (opcode == 2) { // Multiply param1 * param2, store in address pointed to by param3 var val1 = GetParameterValue(_position + 1, 1, parsedCommand); var val2 = GetParameterValue(_position + 2, 2, parsedCommand); var val3 = GetParameterWritePosition(_position + 3, 3, parsedCommand); SetMemoryValue(val3, val1 * val2); _position += 4; } else if (opcode == 3) { // Take user input, and store in the parameter location // If the input provider doesn't have any input, // then pause the program and return awaiting input status if (!_inputProvider.HasInput()) { status = IntcodeProgramStatus.AwaitingInput; break; } BigInteger input = _inputProvider.GetInput(); var val1 = GetParameterWritePosition(_position + 1, 1, parsedCommand); SetMemoryValue(val1, input); _position += 2; } else if (opcode == 4) { // Output a value var val1 = GetParameterValue(_position + 1, 1, parsedCommand); _outputListener.SendOutput(val1); _position += 2; } else if (opcode == 5) { // Opcode 5 is jump-if-true: if the first parameter is // non-zero, it sets the instruction pointer to the value // from the second parameter. Otherwise, it does nothing. var val1 = GetParameterValue(_position + 1, 1, parsedCommand); var val2 = GetParameterValue(_position + 2, 2, parsedCommand); if (val1 != 0) { _position = GetMemoryAddress(val2); } else { _position += 3; } } else if (opcode == 6) { // Opcode 6 is jump-if-false: if the first parameter is // zero, it sets the instruction pointer to the value from // the second parameter. Otherwise, it does nothing. var val1 = GetParameterValue(_position + 1, 1, parsedCommand); var val2 = GetParameterValue(_position + 2, 2, parsedCommand); if (val1 == 0) { _position = GetMemoryAddress(val2); } else { _position += 3; } } else if (opcode == 7) { // Opcode 7 is less than: if the first parameter is less // than the second parameter, it stores 1 in the position // given by the third parameter. // Otherwise, it stores 0. var val1 = GetParameterValue(_position + 1, 1, parsedCommand); var val2 = GetParameterValue(_position + 2, 2, parsedCommand); var val3 = GetParameterWritePosition(_position + 3, 3, parsedCommand); var valToStore = val1 < val2 ? 1 : 0; SetMemoryValue(val3, valToStore); _position += 4; } else if (opcode == 8) { // Opcode 8 is equals: if the first parameter is equal to // the second parameter, it stores 1 in the position given // by the third parameter. // Otherwise, it stores 0. var val1 = GetParameterValue(_position + 1, 1, parsedCommand); var val2 = GetParameterValue(_position + 2, 2, parsedCommand); var val3 = GetParameterWritePosition(_position + 3, 3, parsedCommand); var valToStore = val1 == val2 ? 1 : 0; SetMemoryValue(val3, valToStore); _position += 4; } else if (opcode == 9) { // Opcode 9 adjusts the relative base by the value of its // only parameter. The relative base increases (or // decreases, if the value is negative) by the value of the // parameter. // For example, if the relative base is 2000, then after // the instruction 109,19, the relative base would be 2019. // If the next instruction were 204,-34, then the value at // address 1985 would be output. var val1 = GetParameterValue(_position + 1, 1, parsedCommand); _relativeBase += GetMemoryAddress(val1); _position += 2; } else if (opcode == 99) { status = IntcodeProgramStatus.Completed; break; } else if (opcode != 99) { throw new Exception($"Invalid opcode {_program[_position]} at position {_position}"); } } return(status); }
private void ExecuteInstruction(Instruction inst) { int operand1; int operand2; switch (inst.Opcode) { case Opcode.Add: operand1 = ReadValue(inst.A); operand2 = ReadValue(inst.B); Program[inst.C.Value] = operand1 + operand2; IP += inst.Length; break; case Opcode.Multiply: operand1 = ReadValue(inst.A); operand2 = ReadValue(inst.B); Program[inst.C.Value] = operand1 * operand2; IP += inst.Length; break; case Opcode.Input: operand1 = int.Parse(_inputProvider.GetInput()); Program[inst.A.Value] = operand1; IP += inst.Length; break; case Opcode.Output: operand1 = ReadValue(inst.A); _outputSink.Output(operand1); IP += inst.Length; break; case Opcode.JumpIfTrue: operand1 = ReadValue(inst.A); if (operand1 != 0) { IP = ReadValue(inst.B); } else { IP += inst.Length; } break; case Opcode.JumpIfFalse: operand1 = ReadValue(inst.A); if (operand1 == 0) { IP = ReadValue(inst.B); } else { IP += inst.Length; } break; case Opcode.LessThan: operand1 = ReadValue(inst.A); operand2 = ReadValue(inst.B); if (operand1 < operand2) { Program[inst.C.Value] = 1; } else { Program[inst.C.Value] = 0; } IP += inst.Length; break; case Opcode.Equals: operand1 = ReadValue(inst.A); operand2 = ReadValue(inst.B); if (operand1 == operand2) { Program[inst.C.Value] = 1; } else { Program[inst.C.Value] = 0; } IP += inst.Length; break; } }
private void Update() { InputChanged?.Invoke(currentInputProvider.GetInput()); }
public override int Execute() { ParameterOneValue = _inputProvider.GetInput(); return(_instructionPointer + 2); }