public void Execute(IInstructionExecutor valueExecutor, IInstructionExecutor moveChipsExecutor, IInstructable factory, IEnumerable <string> instructions) { var valueInstructions = instructions.Where(a => valueExecutor.CanExecute(factory, a)); foreach (var valueInstruction in valueInstructions) { valueExecutor.Execute(factory, valueInstruction); } var remaining = instructions.Except(valueInstructions); while (remaining.Any()) { // -Initially, bot 1 starts with a value-3 chip, and bot 2 starts with a value-2 chip and a value-5 chip. //- Because bot 2 has two microchips, it gives its lower one(2) to bot 1 and its higher one (5) to bot 0. //- Then, bot 1 has two microchips; it puts the value-2 chip in output 1 and gives the value-3 chip to bot 0. //- Finally, bot 0 has two microchips; it puts the 3 in output 2 and the 5 in output 0 var nextToExecute = NextInstruction(remaining, i => moveChipsExecutor.CanExecute(factory, i)); moveChipsExecutor.Execute(factory, nextToExecute); remaining = Remove(remaining, nextToExecute); } }
public Robot(IInstructionExecutor instructionExecutor, LinkedList <Instructions> instructions, PositionState positionState) { _instructions = instructions; _instructionExecutor = instructionExecutor; _positionState = positionState; }
public static void EnableDiagnostics <G>(string code, IInstructionExecutor <G> instructionExecutor, ExecutionState <G> executionState, bool forceInterrupts = false) where G : IGroupState <G>, new() { if (instructionExecutor.Interrupts.Count == 0) { instructionExecutor.Interrupts.Add(new StepInterrupt <G>()); instructionExecutor.Interrupted += (sender, args) => OnInterrupt(code, sender, args); } ScanInstructions(executionState.Instructions, forceInterrupts); }
private static void ExecuteInstructionRSqrt(IInstructionExecutor <GroupState> executor, ExecutionState <GroupState> executionState, object[] payload, StackList <ValuePointer <GroupState> > stackRegister, StackList <StackValuePointer <GroupState> > stackPointers) { IValue <GroupState> value = (IValue <GroupState>)stackRegister.Last().Value; double doubleVal = Math.Sqrt(Convert.ToDouble(value.GetData())); stackRegister.SetLast(new ValuePointer <GroupState> { Value = executor.ValueProvider.GetReducedValue(doubleVal) }); }
private static void ExecuteInstructionRPower(IInstructionExecutor <GroupState> executor, ExecutionState <GroupState> executionState, object[] payload, StackList <ValuePointer <GroupState> > stackRegister, StackList <StackValuePointer <GroupState> > stackPointers) { IValue <GroupState> secondValue = (IValue <GroupState>)stackRegister.TakeLast().Value; IValue <GroupState> firstValue = (IValue <GroupState>)stackRegister.Last().Value; IValuable <GroupState> result = executor.ValueProvider.GetReducedValue(Math.Pow(Convert.ToDouble(firstValue.GetData()), Convert.ToDouble(secondValue.GetData()))); stackRegister.SetLast(new ValuePointer <GroupState> { Value = result }); }
private static void ExecuteInstructionRFactorial(IInstructionExecutor <GroupState> executor, ExecutionState <GroupState> executionState, object[] payload, StackList <ValuePointer <GroupState> > stackRegister, StackList <StackValuePointer <GroupState> > stackPointers) { IValue <GroupState> value = (IValue <GroupState>)stackRegister.Last().Value; int intVal = Convert.ToInt32(value.GetData()); long resultVal = 1; for (int i = intVal; i > 0; i--) { resultVal *= i; } stackRegister.SetLast(new ValuePointer <GroupState> { Value = executor.ValueProvider.GetReducedValue(resultVal) }); }
public static void Problem10() { //var instructions = new[] //{ // "value 5 goes to bot 2", // "bot 2 gives low to bot 1 and high to bot 0", // "value 3 goes to bot 1", // "bot 1 gives low to output 1 and high to bot 0", // "bot 0 gives low to output 2 and high to output 0", // "value 2 goes to bot 2" //}; var instructions = FileStringReader.Read("P10.txt"); var factory = new Factory(); var executors = new IInstructionExecutor[] { new LoadBotInstructionExecutor(), new MoveChipsInstructionExecutor( (label, values) => { if (values.Contains(61) && values.Contains(17)) { Console.WriteLine("Bot {0} is comparing values {1} and {2}", label, values.First(), values.Last()); } }) }; new Foreman().Execute(executors.First(), executors.Last(), factory, instructions); //foreach (var bot in factory.Bots) //{ // Console.WriteLine("Bot {0} contains values [{1}]", bot, factory.BotValues(bot).Format()); //} foreach (var bin in factory.OutputBins.OrderBy(a => a)) { Console.WriteLine("Bin {0} contains values [{1}]", bin, factory.OutputBinValues(bin).Format()); } }
public static void Execute <G>(this Instruction <G> instruction, IInstructionExecutor <G> executor, ExecutionState <G> executionState, StackList <ValuePointer <G> > stackRegister, StackList <StackValuePointer <G> > stackPointers) where G : IGroupState <G>, new() { instruction.ExecutionBody(executor, executionState, instruction.Payload, stackRegister, stackPointers); }