/* * The Morpheus Tutorials: https://www.youtube.com/channel/UCLGY6_j7kZfA1dmmjR1J_7w * Coding Challange 6: https://www.youtube.com/watch?v=sdAXkE39b9k * Version: 2018.08.11.1954 */ using System; using System.Collections.Generic; using System.Diagnostics; using System.Net.Http; using System.Text; using static System.Console; using static System.Math; namespace Challange_6 { internal class Program { private const string CHALLANGES = "https://cc.the-morpheus.de/challenges/6/"; private const string SOLUTIONS = "https://cc.the-morpheus.de/solutions/6/"; private static readonly HttpClient client = new HttpClient(); private static readonly Stopwatch watch = new Stopwatch(); private static void Main(string[] args) { try { do { string numStr = client.GetStringAsync(CHALLANGES).Result; watch.Restart(); // Start Konvertierung ulong number = MyConverter.Str2Dec(numStr); watch.Stop(); double timeToNumber = watch.Elapsed.TotalMilliseconds; watch.Restart(); string binary = MyConverter.Dec2Bin(number); watch.Stop(); // Ende Konvertierung double timeToBinary = watch.Elapsed.TotalMilliseconds; string response = client.PostAsync(SOLUTIONS, new StringContent( $@"{{ ""token"": ""{binary}"" }}", Encoding.UTF8, "application/json")).Result.Content.ReadAsStringAsync().Result; string message = $"Number: {number} ({timeToNumber}ms)\nBinary: {binary} ({timeToBinary}ms)\nResult: {response}\n"; WriteLine(message); } while (ReadKey().Key != ConsoleKey.Escape); } catch (Exception ex) { WriteLine(ex); ReadKey(); } } } internal static class MyConverter { public static ulong Str2Dec(string numStr) { ulong number = 0; ulong power = 1; for (int i = numStr.Length - 1; i >= 0; i--) { int num = numStr[i] - '0'; if (num >= 0 && num <= 9) { number += (ulong)num * power; power *= 10; } } return number; } public static string Dec2Bin(ulong number) { int count = (int)Ceiling(Log10(number) / Log10(2)); // Anzahl der Bits bestimmen char[] numbers = new char[count]; ulong temp = number; for (int i = count - 1; i >= 0; i--) { numbers[i] = (char)('0' + (temp & 1)); // Bit für Position bestimmen temp >>= 1; // Bitshift um eine Position (entspricht Division durch 2) } return new string(numbers); } } }