Advertisement
MBrendecke

The Morpheus Tutorials - Coding Challange 7

Aug 18th, 2018
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.80 KB | None | 0 0
  1. /*
  2.  * ############################################
  3.  * ###      The Morpheus Tutorials          ###
  4.  * ###      Lösung:   Coding Challenge 7    ###
  5.  * ###      Version:  2018.08.27-0110       ###
  6.  * ############################################
  7.  */
  8.  
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Diagnostics;
  12. using System.Linq;
  13. using System.Net.Http;
  14. using System.Text;
  15.  
  16. using Newtonsoft.Json;
  17. using Newtonsoft.Json.Linq;
  18.  
  19. using static System.Console;
  20.  
  21. namespace Challenge_7 {
  22.  
  23.   internal class Program {
  24.     private static readonly HttpClient client = new HttpClient();
  25.  
  26.     private const string CHALLENGE_URL = "https://cc.the-morpheus.de/challenges/7/";
  27.     private const string SOLUTIONS_URL = "https://cc.the-morpheus.de/solutions/7/";
  28.  
  29.     private static void Messung(int durchläufe, Action action) {
  30.       Stopwatch sw = new Stopwatch();
  31.       Stopwatch gesamt = new Stopwatch();
  32.       List<TimeSpan> zeiten = new List<TimeSpan>();
  33.  
  34.       {
  35.         gesamt.Start();
  36.         for (int i = 1; i <= durchläufe; i++) {
  37.           Title = $"Durchlauf #{i:0000}";
  38.           sw.Restart();
  39.           action();
  40.           sw.Stop();
  41.           zeiten.Add(sw.Elapsed);
  42.         }
  43.         gesamt.Stop();
  44.       }
  45.  
  46.       string format(long time) => $"{TimeSpan.FromTicks(time):mm\\:ss\\:ffffff}";
  47.       WriteLine($"Min:        {format(zeiten.Min(t => t.Ticks))}");
  48.       WriteLine($"Avg:        {format(Convert.ToInt64(zeiten.Average(t => t.Ticks)))}");
  49.       WriteLine($"Max:        {format(zeiten.Max(t => t.Ticks))}");
  50.       WriteLine($"Gesamt:     {format(gesamt.Elapsed.Ticks)}");
  51.       WriteLine($"Durchläufe: {durchläufe}");
  52.     }
  53.  
  54.     private static int[] SolveWithDictionary(int[] arr, int target) {
  55.       Dictionary<int, int> dict = new Dictionary<int, int>();
  56.       int possibleProblemTarget = (target % 2 == 0) ? target / 2 : -1;
  57.  
  58.       for (int i = 0; i < arr.Length; i++) {
  59.         int ai = arr[i];
  60.         int diff = target - ai;
  61.  
  62.         if (diff < 0) {
  63.           continue;
  64.         }
  65.  
  66.         if (diff == possibleProblemTarget) {
  67.           return new[] { i, i };
  68.         }
  69.  
  70.         if (dict.TryGetValue(diff, out int dd)) {
  71.           if (arr[dd] + ai == target) {
  72.             return new int[] { dd, i };
  73.           }
  74.         }
  75.  
  76.         if (!dict.ContainsKey(ai)) {
  77.           dict.Add(ai, i);
  78.         }
  79.       }
  80.  
  81.       return new[] { -1, -1 };
  82.     }
  83.  
  84.     private static void Solve(Func<int[], int, int[]> solver, int runs) {
  85.       string jsonGet = client.GetAsync(CHALLENGE_URL).Result.Content.ReadAsStringAsync().Result;
  86.  
  87.       dynamic obj = JsonConvert.DeserializeObject(jsonGet);
  88.       int target = obj.k;
  89.       int[] list = ((JArray)obj.list).ToObject<IEnumerable<int>>().ToArray();
  90.  
  91.       int[] solution = null;
  92.       Messung(runs, () => solution = solver(list, target));
  93.  
  94.       string jsonResponse(int[] s) => $"{{\"token\": [{string.Join(", ", s)}]}}";
  95.  
  96.       string response = client.PostAsync(SOLUTIONS_URL,
  97.       new StringContent(jsonResponse(solution), Encoding.UTF8, "application/json")).Result.Content.ReadAsStringAsync().Result;
  98.  
  99.       if (response != "Success") {
  100.         Clear();
  101.         WriteLine("<- - - E R R O R - - ->");
  102.         WriteLine($"K: {target}");
  103.         WriteLine($"List:\n[{string.Join(", ", list)}]");
  104.         WriteLine($"My Solution: [{string.Join(", ", solution)}]");
  105.         WriteLine($"Status: {response}");
  106.         WriteLine("<- - - E R R O R - - ->");
  107.       }
  108.     }
  109.  
  110.     private static void Main(string[] args) {
  111.       try {
  112.         do {
  113.           const int RUNS = 20000;
  114.           WriteLine("Dictionary:");
  115.           Solve(SolveWithDictionary, RUNS);
  116.         } while (ReadKey().Key != ConsoleKey.Escape);
  117.       } catch (Exception ex) {
  118.         WriteLine(ex.StackTrace);
  119.         ReadKey();
  120.       }
  121.     }
  122.   }
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement