Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * ############################################
- * ### The Morpheus Tutorials ###
- * ### Lösung: Coding Challenge 7 ###
- * ### Version: 2018.08.27-0110 ###
- * ############################################
- */
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.Linq;
- using System.Net.Http;
- using System.Text;
- using Newtonsoft.Json;
- using Newtonsoft.Json.Linq;
- using static System.Console;
- namespace Challenge_7 {
- internal class Program {
- private static readonly HttpClient client = new HttpClient();
- private const string CHALLENGE_URL = "https://cc.the-morpheus.de/challenges/7/";
- private const string SOLUTIONS_URL = "https://cc.the-morpheus.de/solutions/7/";
- private static void Messung(int durchläufe, Action action) {
- Stopwatch sw = new Stopwatch();
- Stopwatch gesamt = new Stopwatch();
- List<TimeSpan> zeiten = new List<TimeSpan>();
- {
- gesamt.Start();
- for (int i = 1; i <= durchläufe; i++) {
- Title = $"Durchlauf #{i:0000}";
- sw.Restart();
- action();
- sw.Stop();
- zeiten.Add(sw.Elapsed);
- }
- gesamt.Stop();
- }
- string format(long time) => $"{TimeSpan.FromTicks(time):mm\\:ss\\:ffffff}";
- WriteLine($"Min: {format(zeiten.Min(t => t.Ticks))}");
- WriteLine($"Avg: {format(Convert.ToInt64(zeiten.Average(t => t.Ticks)))}");
- WriteLine($"Max: {format(zeiten.Max(t => t.Ticks))}");
- WriteLine($"Gesamt: {format(gesamt.Elapsed.Ticks)}");
- WriteLine($"Durchläufe: {durchläufe}");
- }
- private static int[] SolveWithDictionary(int[] arr, int target) {
- Dictionary<int, int> dict = new Dictionary<int, int>();
- int possibleProblemTarget = (target % 2 == 0) ? target / 2 : -1;
- for (int i = 0; i < arr.Length; i++) {
- int ai = arr[i];
- int diff = target - ai;
- if (diff < 0) {
- continue;
- }
- if (diff == possibleProblemTarget) {
- return new[] { i, i };
- }
- if (dict.TryGetValue(diff, out int dd)) {
- if (arr[dd] + ai == target) {
- return new int[] { dd, i };
- }
- }
- if (!dict.ContainsKey(ai)) {
- dict.Add(ai, i);
- }
- }
- return new[] { -1, -1 };
- }
- private static void Solve(Func<int[], int, int[]> solver, int runs) {
- string jsonGet = client.GetAsync(CHALLENGE_URL).Result.Content.ReadAsStringAsync().Result;
- dynamic obj = JsonConvert.DeserializeObject(jsonGet);
- int target = obj.k;
- int[] list = ((JArray)obj.list).ToObject<IEnumerable<int>>().ToArray();
- int[] solution = null;
- Messung(runs, () => solution = solver(list, target));
- string jsonResponse(int[] s) => $"{{\"token\": [{string.Join(", ", s)}]}}";
- string response = client.PostAsync(SOLUTIONS_URL,
- new StringContent(jsonResponse(solution), Encoding.UTF8, "application/json")).Result.Content.ReadAsStringAsync().Result;
- if (response != "Success") {
- Clear();
- WriteLine("<- - - E R R O R - - ->");
- WriteLine($"K: {target}");
- WriteLine($"List:\n[{string.Join(", ", list)}]");
- WriteLine($"My Solution: [{string.Join(", ", solution)}]");
- WriteLine($"Status: {response}");
- WriteLine("<- - - E R R O R - - ->");
- }
- }
- private static void Main(string[] args) {
- try {
- do {
- const int RUNS = 20000;
- WriteLine("Dictionary:");
- Solve(SolveWithDictionary, RUNS);
- } while (ReadKey().Key != ConsoleKey.Escape);
- } catch (Exception ex) {
- WriteLine(ex.StackTrace);
- ReadKey();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement