Advertisement
MBrendecke

Challenge 8

Sep 8th, 2018
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.84 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Linq;
  5. using System.Net.Http;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using Newtonsoft.Json;
  9. using Newtonsoft.Json.Linq;
  10.  
  11. using static System.Console;
  12.  
  13. namespace Challenge_8 {
  14.  
  15.   public static class Client {
  16.     private static readonly HttpClient _CLIENT = new HttpClient();
  17.  
  18.     public static async Task<string> GetStringAsync(string url) => await _CLIENT.GetAsync(url).Result.Content.ReadAsStringAsync();
  19.  
  20.     public static async Task<string> PostAsync(string url, string data, string mediaType) => await _CLIENT.PostAsync(url, new StringContent(data, Encoding.UTF8, mediaType)).Result.Content.ReadAsStringAsync();
  21.   }
  22.  
  23.   internal class Program {
  24.     private const string CHALLENGE_URL = "https://cc.the-morpheus.de/challenges/8/";
  25.     private const string SOLUTIONS_URL = "https://cc.the-morpheus.de/solutions/8/";
  26.  
  27.     private static void Main(string[] args) {
  28.       do {
  29.         Clear();
  30.  
  31.         WriteLine("Empfange Daten...");
  32.         Stopwatch sw = Stopwatch.StartNew();
  33.         string jsonGet = Client.GetStringAsync(CHALLENGE_URL).Result;
  34.         dynamic json = JsonConvert.DeserializeObject(jsonGet);
  35.         int target = json.k;
  36.         int[] list = ((JArray)json.list).ToObject<IEnumerable<int>>().ToArray();
  37.         WriteLine($"Dauer: {sw.Elapsed.TotalMilliseconds}ms");
  38.  
  39.         WriteLine($"Empfangene Daten:\nK: {target}\nL: [ {string.Join(", ", list)} ]\n");
  40.  
  41.         WriteLine("Starte Berechnung...");
  42.         sw.Restart();
  43.         int[] ret = Solve(list, target);
  44.         sw.Stop();
  45.         WriteLine($"Lösung: [ {string.Join(", ", ret)} ]");
  46.         WriteLine($"Dauer: {sw.Elapsed.TotalMilliseconds}ms / {sw.Elapsed.Ticks} Ticks");
  47.  
  48.         string jsonResponse(int[] s) => $"{{\"token\": [{string.Join(", ", s)}]}}";
  49.         string response = Client.PostAsync(SOLUTIONS_URL, jsonResponse(ret), "application/json").Result;
  50.  
  51.         BackgroundColor = response.StartsWith("Success") ? ConsoleColor.DarkGreen : ConsoleColor.DarkRed;
  52.         WriteLine("Ergebnis: " + response);
  53.         BackgroundColor = ConsoleColor.Black;
  54.       } while (ReadKey().Key != ConsoleKey.Escape);
  55.       WriteLine("ENDE");
  56.     }
  57.  
  58.     private static int[] Solve(int[] arr, int target) {
  59.       Dictionary<int, KeyValuePair<int, int>> dict = new Dictionary<int, KeyValuePair<int, int>>(arr.Length * arr.Length);
  60.  
  61.       for (int i = 0; i < arr.Length - 1; i++) {
  62.         for (int j = i; j < arr.Length; j++) {
  63.           int sum = arr[i] + arr[j];
  64.  
  65.           dict[sum] = new KeyValuePair<int, int>(i, j);
  66.  
  67.           if (dict.ContainsKey(target - sum)) {
  68.             KeyValuePair<int, int> p = dict[target - sum];
  69.             return new[] { i, j, p.Key, p.Value };
  70.           }
  71.         }
  72.       }
  73.  
  74.       return new[] { -1, -1, -1, -1 };
  75.     }
  76.   }
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement