public override string Run(StreamReader reader) { var data = Parse(reader); var input = new BlockingCollection <IntCodeValue> { IntCodeValue.FromInt(_input) }; var program = CreateIntCodeProgram(input); program.Compute(data); var output = program.Output; var outputCodes = output.ToArray(); if (outputCodes.SkipLast(1).Any(code => code != IntCodeValue.FromInt(0))) { throw new InvalidOperationException("Found non zero output code."); } if (!outputCodes.Any()) { return("null"); } return(outputCodes.Last().ToString()); }
public override bool Process(IIntCodeData data, int[] parameterModes, ref IntCodeValue offset) { var output = ReadData(data, data[offset++], parameterModes, 0); OutputQueue.Add(output); return(false); }
public override bool Process(IIntCodeData data, int[] parameterModes, ref IntCodeValue offset) { var parameter1 = ReadData(data, data[offset++], parameterModes, 0); _parameterComputer.AdjustBase(parameter1); return(false); }
public override bool Process(IIntCodeData data, int[] parameterModes, ref IntCodeValue offset) { var input = _input.Take(); var value = data[offset++]; WriteData(data, input, value, parameterModes, 0); return(false); }
public override bool Process(IIntCodeData data, int[] parameterModes, ref IntCodeValue offset) { var parameter1 = ReadData(data, data[offset++], parameterModes, 0); var parameter2 = ReadData(data, data[offset++], parameterModes, 1); WriteData(data, parameter1 + parameter2, data[offset++], parameterModes, 2); return(false); }
public void Should_OutputLargeNumberInTheMiddle() { var data = new BigInteger[] { 104, 1125899906842624, 99 }; var program = IntCodeProgram.New(); program.Compute(IntCodeData.FromBigIntArray(data)); var number = program.Output.Single(); Assert.Equal(IntCodeValue.FromBigInteger(data[1]), number); }
public override bool Process(IIntCodeData data, int[] parameterModes, ref IntCodeValue offset) { var parameter1 = ReadData(data, data[offset++], parameterModes, 0); var parameter2 = ReadData(data, data[offset++], parameterModes, 1); if (parameter1 == IntCodeValue.FromInt(0)) { offset = parameter2; } return(false); }
public override bool Process(IIntCodeData data, int[] parameterModes, ref IntCodeValue offset) { var parameter1 = ReadData(data, data[offset++], parameterModes, 0); var parameter2 = ReadData(data, data[offset++], parameterModes, 1); var parsedParameter3 = data[offset++]; var value = IntCodeValue.FromInt(0); if (parameter1 < parameter2) { value = IntCodeValue.FromInt(1); } WriteData(data, value, parsedParameter3, parameterModes, 2); return(false); }
public int ComputeSignal(IList <IntCodeValue> phaseSettings, int[] data) { var amplifiers = new IIntCodeProgram[phaseSettings.Count]; var firstInput = new BlockingCollection <IntCodeValue>(); for (var i = 0; i < amplifiers.Length; i++) { var input = i == 0 ? firstInput : amplifiers[i - 1].Output; input.Add(phaseSettings[i]); if (_feedbackLoop && i == amplifiers.Length - 1) { amplifiers[i] = IntCodeProgram.New(input, firstInput); } else { amplifiers[i] = IntCodeProgram.New(input); } } var tasks = amplifiers.Select(program => Task.Factory.StartNew(() => program.Compute(data.ToArray()), TaskCreationOptions.LongRunning)); firstInput.Add(IntCodeValue.FromInt(0)); Task.WhenAll(tasks).Wait(TimeoutMs); var took = amplifiers.Last().Output.TryTake(out var signal); if (!took) { throw new InvalidOperationException("No output found."); } if (amplifiers.Last().Output.Count != 0) { throw new InvalidOperationException("There's still output in the last amplifier."); } return((int)signal); }
public override string Run(StreamReader reader) { var data = Parse(reader); var input = new BlockingCollection <IntCodeValue> { IntCodeValue.FromInt(_input) }; var program = CreateIntCodeProgram(input); program.Compute(data); var output = program.Output; var outputCodes = output.ToArray(); if (outputCodes.Length != 1) { throw new InvalidOperationException("Found zero or more than one values in output: " + string.Join(",", outputCodes.Select(code => code.ToString()))); } return(outputCodes.Single().ToString()); }
public abstract bool Process(IIntCodeData data, int[] parameterModes, ref IntCodeValue offset);
protected void WriteData(IIntCodeData data, IntCodeValue valueToBeWritten, IntCodeValue parsedParameterValue, int[] parameterModes, int parameterIndex) { _parameterComputer.WriteParameter(data, valueToBeWritten, parsedParameterValue, GetParameterMode(parameterModes, parameterIndex)); }
protected IntCodeValue ReadData(IIntCodeData data, IntCodeValue parsedParameterValue, int[] parameterModes, int parameterIndex) { return(_parameterComputer.FetchParameter(data, parsedParameterValue, GetParameterMode(parameterModes, parameterIndex))); }
public override bool Process(IIntCodeData data, int[] parameterModes, ref IntCodeValue offset) { return(true); }