157 lines
4.3 KiB
C#
157 lines
4.3 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using static System.Console;
|
|
|
|
namespace _2018_06
|
|
{
|
|
class Program
|
|
{
|
|
static void Main(string[] args)
|
|
{
|
|
string[] resTab = File.ReadAllLines(@"..\..\..\Data\Adventofcode_181206\2018_06_data.txt");
|
|
List<Tuple<string, int, int>> koords = new List<Tuple<string, int, int>>();
|
|
Tuple<string, int, int> tuple = null;
|
|
int antal = 0;
|
|
foreach (string v in resTab)
|
|
{
|
|
string[] nums = v.Split(new char[] { ',' });
|
|
tuple = Tuple.Create(ChNumber(antal), int.Parse(nums[0]), int.Parse(nums[1]));
|
|
koords.Add(tuple);
|
|
antal++;
|
|
}
|
|
|
|
int points = 0;
|
|
int a = 0;
|
|
int b = 0;
|
|
|
|
foreach (Tuple<string, int, int> tp in koords)
|
|
{
|
|
points++;
|
|
WriteLine($"point {points}, id: {tp.Item1}, X: {tp.Item2} Y: {tp.Item3}");
|
|
if (tp.Item2 > a)
|
|
{
|
|
a = tp.Item2;
|
|
}
|
|
|
|
if (tp.Item3 > b)
|
|
{
|
|
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<string, int, int> 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<string> infinites = new SortedSet<string>();
|
|
|
|
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<string, int> areas = new Dictionary<string, int>();
|
|
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 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;
|
|
}
|
|
}
|
|
}
|