public static void BFrotorsANDgroundset(int[,] order, int[,] grdSet, string cryb, string msg) { // uses brute force on rotor order and ground settings // order set and ground setting set are created previously (order array and grdSet array) MachineRun ma; // plug board is set to: no plugs used string[] plugs = { null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null }; Rotor rl, rm, rr; StreamWriter file = new StreamWriter("C:\\EnigmaTests\\Stopwatch\\BFrotorsANDgroundset\\BFrotorANDgroundset_B_orderANDgrdSet_" + cryb + ".txt"); file.WriteLine("\ncryb: " + cryb); file.WriteLine("\n msg: " + msg + "\r\n"); file.Flush(); Stopwatch timer = new Stopwatch(); timer.Start(); string line = ""; int count = 0; // count for all possible calculations for (int ord = 1; ord < order.GetLength(0); ord++) // order set row { Console.WriteLine("running code for row: " + ord); for (int grd = 1; grd < grdSet.GetLength(0); grd++) // grdSet set row { count++; //Console.WriteLine(order[orderRow, 1] + ":" + order[orderRow, 2] + ":" + order[orderRow, 3] + ":" + gpl + ":" + gpm + ":" + gpr); rl = new Rotor(order[ord, 1], grdSet[grd, 1], 0); // left rotor , A -> Z, ring setting a. rm = new Rotor(order[ord, 2], grdSet[grd, 2], 0); // middle rotor , A -> Z, ring setting a. rr = new Rotor(order[ord, 3], grdSet[grd, 3], 0); // right rotor , A -> Z, ring setting a. // mirror position is set to "B" and entry rotor is set to "O" ma = new MachineRun("B", rl, rm, rr, "O", plugs); string decMsg = ma.EncryptDecrypt(msg); if (decMsg.Contains(cryb.ToUpper())) { line = "FOUND at rotor order: " + order[ord, 1] + order[ord, 2] + order[ord, 3] + " at GS: " + grdSet[grd, 1] + "." + grdSet[grd, 2] + "." + grdSet[grd, 3]; line += Environment.NewLine + "decoded: " + decMsg; //Console.WriteLine(line); file.WriteLine(line); file.Flush(); } } } file.WriteLine("\r\n\r\n" + count); timer.Stop(); TimeSpan ts = timer.Elapsed; file.WriteLine("running time at " + ts.Days + ":" + ts.Hours + ":" + ts.Minutes + ":" + ts.Seconds + "." + ts.Milliseconds / 10); file.Flush(); file.Close(); }
public static string EncryptParallelMachineRotorGroundRing(int rsL, int rsM, int rsR, int gsL, int gsM, int gsR, int ringL, int ringM, int ringR, string msg) { // encrypts the message for each rotor order for all possible settings of rind and ground // plug board is set to: no plugs used //string[] plugs = { "H", "X", "E", "Y", "L", "Z", "O", "W", "A", "B", "C", "D", "F", "G", "I", "J", "K", "M", "N", "P", "Q", "R", null, null, null, null }; string[] plugs = { "A", "V", "B", "S", "C", "G", "D", "L", "F", "U", "H", "Z", "I", "N", "K", "M", "O", "W", "R", "X", null, null, null, null, null, null }; //string[] plugs = { null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null }; // mirror position is set to "B" and entry rotor is set to "O" MachineRun ma = new MachineRun("B", new Rotor(rsL, gsL, ringL), new Rotor(rsM, gsM, ringM), new Rotor(rsR, gsR, ringR), "O", plugs); string decMsg = ma.EncryptDecrypt(msg); return(decMsg); // +" |" + rsL + rsM + rsR + "|" + gsL + "." + gsM + "." + gsR + "|" + ringL + "." + ringM + "." + ringR; }
public static Tuple <int, int> MachineContribution(int request, MachineRun machine) { var totalProcessed = machine.CompletedOutput + machine.FailedOutput; if (totalProcessed <= request) { return(new Tuple <int, int>(machine.CompletedOutput, machine.FailedOutput)); } else { var ratioUsed = (decimal)request / totalProcessed; return(new Tuple <int, int>((int)Math.Ceiling(machine.CompletedOutput * ratioUsed), (int)Math.Floor(machine.FailedOutput * ratioUsed))); } }
private static Tuple <int, int> OrderProcessResult(int request, MachineRun machine) { var totalProcessed = machine.CompletedOutput + machine.FailedOutput; if (totalProcessed < request) { return(new Tuple <int, int>(machine.CompletedOutput, machine.FailedOutput)); } else { var ratioUsed = (decimal)request / totalProcessed; return(new Tuple <int, int>((int)Math.Ceiling(machine.CompletedOutput * ratioUsed), (int)Math.Floor(machine.FailedOutput * ratioUsed))); } }
public static void MachineRotorGroundPlug(int rsl, int rsm, int rsr, int gsl, int gsm, int gsr, string[] plugs, string cryb, string msg, StreamWriter fileOut) { // mirror position is set to "B" and entry rotor is set to "O" MachineRun ma = new MachineRun("B", new Rotor(rsl, gsl, 0), new Rotor(rsm, gsm, 0), new Rotor(rsr, gsr, 0), "O", plugs); string decMsg = ma.EncryptDecrypt(msg); //Console.WriteLine(rsl + ":" + rsm + ":" + rsr + "|" + gsl + ":" + gsm + ":" + gsr + "|"); if (decMsg.Equals(cryb.ToUpper())) { string line = "FOUND at rotor order: " + rsl + rsm + rsr + " at GS: " + gsl + "." + gsm + "." + gsr + "|" + PrintPlugs(plugs); line += Environment.NewLine + "decoded: " + decMsg; Console.WriteLine(line); //PrintPlugs(plugs); fileOut.WriteLine(line); fileOut.Flush(); } }
public static void MachineRotorGround(int rsl, int rsm, int rsr, int gsl, int gsm, int gsr, string cryb, string msg, StreamWriter file) { //Console.WriteLine("working task ..."+rsl+":"+rsm+":"+rsr+"|"+gsl+"."+gsm+"."+gsr); // plug board is set to: no plugs used string[] plugs = { null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null }; // mirror position is set to "B" and entry rotor is set to "O" MachineRun ma = new MachineRun("B", new Rotor(rsl, gsl, 0), new Rotor(rsm, gsm, 0), new Rotor(rsr, gsr, 0), "O", plugs); string decMsg = ma.EncryptDecrypt(msg); if (decMsg.Contains(cryb.ToUpper())) { string line = "FOUND at rotor order: " + rsl + rsm + rsr + " at GS: " + gsl + "." + gsm + "." + gsr; line += Environment.NewLine + "decoded: " + decMsg; Console.WriteLine(line); file.WriteLine(line); file.Flush(); } }
public static MachineRun RunMachine(MachineRun machine) { var averageFailureRate = Setting.GetSetting(machine.SettingsList, Settings.AverageFailureRate); var failureRateVariability = Setting.GetSetting(machine.SettingsList, Settings.FailureRateVariability); var averageProductionTime = Setting.GetSetting(machine.SettingsList, Settings.AverageProductionTime); var productionTimeVariability = Setting.GetSetting(machine.SettingsList, Settings.ProductionTimeVariability); var machineFailureRate = Setting.GetSetting(machine.SettingsList, Settings.MachineFailureRate); var adjFailureRate = averageFailureRate; if (machine.Machine.Broken) { adjFailureRate = averageFailureRate * 10000; } //produce widgets var baseProduction = ActionCheck.GenerateInt((int)(averageProductionTime * 2), (int)productionTimeVariability); var baseFailure = ActionCheck.GenerateFloat(adjFailureRate * 2, failureRateVariability) / 2; baseFailure = baseFailure > 1 ? 1 : baseFailure < 0 ? 0 : baseFailure; machine.CompletedOutput = (int)Math.Ceiling(baseProduction * (1 - baseFailure)); machine.FailedOutput = baseProduction - machine.CompletedOutput; //check to see if machine broke var machineAge = (DateTime.UtcNow - machine.Machine.CreateDate).TotalHours / 100; var failureChance1 = ActionCheck.GenerateFloat(machineFailureRate, (float)machineAge); var failureChance2 = ActionCheck.GenerateFloat(machineFailureRate, (float)machineAge); var failureChance3 = ActionCheck.GenerateFloat(machineFailureRate, (float)machineAge); if (!machine.MachineBroken && !ActionCheck.Check(failureChance1) && !ActionCheck.Check(failureChance2) && !ActionCheck.Check(failureChance3)) { machine.MachineBroken = true; } return(machine); }
public static void MachineRotorGroundRing(int rsL, int rsM, int rsR, int gsL, int gsM, int gsR, int ringL, int ringM, int ringR, string cryb, string msg, StreamWriter file) { //Console.WriteLine("working task ..."+rsL+":"+rsM+":"+rsR+"|"+gsL+"."+gsM+"."+gsR+"|"+ringL+"."+ringM+"."+ringR); // plug board is set to: no plugs used //string[] plugs = { "H", "X", "E", "Y", "L", "Z", "O", "W", "A", "B", "C", "D", "F", "G", "I", "J", "K", "M", "N", "P", "Q", "R", null, null, null, null }; string[] plugs = { null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null }; // mirror position is set to "B" and entry rotor is set to "O" MachineRun ma = new MachineRun("B", new Rotor(rsL, gsL, ringL), new Rotor(rsM, gsM, ringM), new Rotor(rsR, gsR, ringR), "O", plugs); string decMsg = ma.EncryptDecrypt(msg); //Console.WriteLine("msg: "+decMsg); if (decMsg.Contains(cryb.ToUpper())) { totalFound++; string line = "" + rsL + rsM + rsR + "|" + gsL + "." + gsM + "." + gsR + "|" + ringL + "." + ringM + "." + ringR; //line += Environment.NewLine + "decoded: " + decMsg; ////Console.WriteLine(line); file.WriteLine(line); file.Flush(); } }
public static void BFrotorsEveryTwo(int[,] order, string cryb, string msg) { // brute force through order of rotors and their ground settings NOT including ring settings and NOT including the plugboard // uses the B mirror // runs two enigmas consecutively StreamWriter file = new StreamWriter("C:\\EnigmaTests\\Stopwatch\\BFrotors\\BFrotorsEveryTwo_B_orderOf8" + cryb + ".txt"); // plug board is set to: no plugs used string[] plugs = { null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null }; file.WriteLine("\ncryb: " + cryb); file.WriteLine("\n msg: " + msg + "\r\n"); file.Flush(); Stopwatch timer = new Stopwatch(); timer.Start(); int count = 0; // count for all possible calculations string line = ""; // for output to the file string decMsg_1 = ""; string decMsg_2 = ""; Rotor rl_1, rm_1, rr_1, rl_2, rm_2, rr_2; MachineRun ma_1, ma_2; for (int orderRow = 1; orderRow < order.GetLength(0); orderRow += 2) { //Console.WriteLine("Checking row: " + orderRow); for (int gpl = 0; gpl <= 25; gpl++) // ground position of left rotor { for (int gpm = 0; gpm <= 25; gpm++) // ground position of middle rotor { for (int gpr = 0; gpr <= 25; gpr++) // ground position right rotor { rl_1 = new Rotor(order[orderRow, 1], gpl, 0); // left rotor , A -> Z, ring setting a. rm_1 = new Rotor(order[orderRow, 2], gpm, 0); // middle rotor , A -> Z, ring setting a. rr_1 = new Rotor(order[orderRow, 3], gpr, 0); // right rotor , A -> Z, ring setting a. rl_2 = new Rotor(order[orderRow + 1, 1], gpl, 0); // left rotor , A -> Z, ring setting a. rm_2 = new Rotor(order[orderRow + 1, 2], gpm, 0); // middle rotor , A -> Z, ring setting a. rr_2 = new Rotor(order[orderRow + 1, 3], gpr, 0); // right rotor , A -> Z, ring setting a. // mirror position is set to "B" and entry rotor is set to "O" ma_1 = new MachineRun("B", rl_1, rm_1, rr_1, "O", plugs); ma_2 = new MachineRun("B", rl_2, rm_2, rr_2, "O", plugs); decMsg_1 = ma_1.EncryptDecrypt(msg); count++; decMsg_2 = ma_2.EncryptDecrypt(msg); count++; //Console.WriteLine(order[orderRow, 1]+":"+ order[orderRow, 2]+":"+order[orderRow, 3]+":"+gpl+":"+gpm+":"+gpr); //Console.WriteLine(order[orderRow+1, 1] + ":" + order[orderRow+1, 2] + ":" + order[orderRow+1, 3] + ":" + gpl + ":" + gpm + ":" + gpr); if (decMsg_1.Contains(cryb.ToUpper())) { line = "FOUND at rotor order: " + order[orderRow, 1] + order[orderRow, 2] + order[orderRow, 3] + " at GS: " + gpl + "." + gpm + "." + gpr; line += Environment.NewLine + "decoded: " + decMsg_1; //Console.WriteLine(line); file.WriteLine(line); file.Flush(); } if (decMsg_2.Contains(cryb.ToUpper())) { line = "FOUND at rotor order: " + order[orderRow + 1, 1] + order[orderRow + 1, 2] + order[orderRow + 1, 3] + " at GS: " + gpl + "." + gpm + "." + gpr; line += Environment.NewLine + "decoded: " + decMsg_2; //Console.WriteLine(line); file.WriteLine(line); file.Flush(); } } } } } file.WriteLine("\r\n\r\n" + count); timer.Stop(); TimeSpan ts = timer.Elapsed; file.WriteLine("running time at " + ts.Days + ":" + ts.Hours + ":" + ts.Minutes + ":" + ts.Seconds + "." + ts.Milliseconds / 10); file.Flush(); file.Close(); }
public static async Task RunOrchestrator( [OrchestrationTrigger] DurableOrchestrationContext context, [ServiceBus("machines", Connection = "ServiceBusConnection")] ICollector <string> machineOutput, [ServiceBus("logs", Connection = "ServiceBusConnection")] ICollector <string> logOutput, [EventHub("mainlogs", Connection = "EventHubConnectionAppSetting")] IAsyncCollector <string> outputEvents, ILogger log) { var settingsList = new List <Settings> { Settings.AverageProductionTime, Settings.ProductionTimeVariability, Settings.AverageFailureRate, Settings.FailureRateVariability, Settings.MachineFailureRate }; try { using (IDbConnection conn = new SqlConnection(Environment.GetEnvironmentVariable("SqlConnection"))) { var settingsRepo = new SettingRepository(conn); var machineRepo = new MachineRepository(conn); var settings = settingsRepo.GetSettings(settingsList).Result; var machines = machineRepo.GetActiveMachines().Result; var results = new List <MachineRun>(); foreach (var machine in machines) { var run = new MachineRun(machine, settings.ToList()); var result = RunMachine(run); results.Add(result); /* if (result.MachineBroken) * { * await context.CallActivityAsync("BreakMachine", result.Machine); * machineOutput.Add(JsonConvert.SerializeObject(result.Machine, * new JsonSerializerSettings * { * ContractResolver = new CamelCasePropertyNamesContractResolver() * })); * logOutput.Add($"{TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, TimeZoneInfo.FindSystemTimeZoneById("US Eastern Standard Time")).ToLongTimeString()} Machine {result.Machine.Id} broken."); * }*/ } var runLog = await context.CallActivityAsync <MachineRunResults>("ProcessResults", results); if (runLog != null) { runLog.MachineRuns = results; var logResults = machineRepo.CreateRunResults(runLog).Result; outputEvents.AddAsync(JsonConvert.SerializeObject(runLog, new JsonSerializerSettings { ContractResolver = new CamelCasePropertyNamesContractResolver() })).RunSynchronously(); } } } catch (Exception e) { log.LogError(e.Message); } }