From 31bfc56ca9a8935d56147fae40afc1239a83fceb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tommy=20=C3=96man?= Date: Sun, 16 Dec 2018 23:15:40 +0100 Subject: [PATCH] Part 1 181206 working --- 2018_06/Program.cs | 118 ++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 106 insertions(+), 12 deletions(-) diff --git a/2018_06/Program.cs b/2018_06/Program.cs index 4fb4cf6..d4888e6 100644 --- a/2018_06/Program.cs +++ b/2018_06/Program.cs @@ -14,47 +14,141 @@ namespace _2018_06 { string[] resTab = File.ReadAllLines(@"..\..\..\Data\Adventofcode_181206\2018_06_data.txt"); List> koords = new List>(); - Tuple tuple = null; + Tuple tuple = null; + int antal = 0; foreach (string v in resTab) { string[] nums = v.Split(new char[] { ',' }); - tuple = Tuple.Create(int.Parse(nums[0]), int.Parse(nums[1])); + tuple = Tuple.Create(ChNumber(antal), int.Parse(nums[0]), int.Parse(nums[1])); koords.Add(tuple); + antal++; } int points = 0; - int a=0; + int a = 0; int b = 0; - foreach (Tuple tp in koords) + foreach (Tuple tp in koords) { points++; - WriteLine($"point {points}, X: {tp.Item1} Y: {tp.Item2}"); - if (tp.Item1 > a) + WriteLine($"point {points}, id: {tp.Item1}, X: {tp.Item2} Y: {tp.Item3}"); + if (tp.Item2 > a) { - a = tp.Item1; + a = tp.Item2; } - if (tp.Item2 > b) + if (tp.Item3 > b) { - b = tp.Item2; + b = tp.Item3; } } WriteLine($"max value: {a},{b}"); ReadKey(); + string[,] area = new string[360, 360]; - + for (int i = 0; i < 360; i++) + { + for (int j = 0; j < 360; j++) + { + int dist = 10000; + foreach (Tuple tp1 in koords) + { + int part1 = tp1.Item2 - i; + int part2 = tp1.Item3 - j; + int ldist = Math.Abs(part1) + Math.Abs(part2); + if (ldist < dist) + { + dist = ldist; + area[i, j] = tp1.Item1; + } + else + { + if (ldist == dist) + { + area[i, j] = "."; + } + } + } + //Write($" {area[i, j]}"); + } + //WriteLine(); + } + + WriteLine("Skapat avståndsmap"); + + ReadKey(); + + SortedSet infinites = new SortedSet(); + + for (int i = 0; i < 360; i++) + { + try + { + infinites.Add(area[0, i]); + infinites.Add(area[359, i]); + infinites.Add(area[i, 0]); + infinites.Add(area[i, 359]); + } + catch (Exception) { } + } + + + foreach (var item in infinites) + { + Write($" {item}"); + + } + + WriteLine(); + WriteLine("Kontrollerat ytterkanter"); + + ReadKey(); + + Dictionary areas = new Dictionary(); + foreach (var item in area) + { + if (infinites.Contains(item)) + { } + else + { + if (areas.Keys.Contains(item)) + { + areas[item]++; + } + else areas.Add(item, 1); + } + } + + int maxUnits = 0; + string maxArea = ""; + + foreach(var item in areas.Keys) + { + WriteLine($"{item}= {areas[item]}"); + if (areas[item] > maxUnits) + { + maxUnits = areas[item]; + maxArea = item; + } + } + + WriteLine($"Vinnare {maxArea}= {areas[maxArea]}"); + + ReadKey(); } - public string ChNumber(int i) + public static string ChNumber(int i) { const string nbr = "ABCDEFGHIJKLMNOPQRSTUVXYZ"; var cnbr = nbr.ToCharArray(); string tmpSvar = ""; - + int a = i / 24; + int b = i - a * 24; + + tmpSvar = cnbr[a].ToString() + cnbr[b].ToString(); return tmpSvar; }