Files
AdventOfCode/2018_05/Program.cs

121 lines
3.6 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using static System.Console;
namespace _2018_05
{
class Program
{
static void Main(string[] args)
{
int removes = 0;
string resTab = File.ReadAllText(@"..\..\..\Data\Adventofcode_181205\2018_05_data.txt");
//string resTab = "dabAcCaCBAcCcaDA";
SortedSet<Char> charset = new SortedSet<char>();
foreach (char x in resTab.ToCharArray())
{
try
{
charset.Add(x);
}
catch (Exception)
{ }
}
foreach (char y in charset)
{
Write($"{y},");
}
ReadKey();
string nextStr = StringReduce(ref removes, resTab);
int winner = nextStr.Length;
string wrkWin = "";
foreach (char y in charset)
{
string wrk = "";
wrk += y;
if (wrk != wrk.ToLower())
{
string tmpResTab = "";
string tmpResStr = "";
int tmpRemoves = 0;
foreach (char z in resTab.ToCharArray())
{
if (z.ToString() != wrk && z.ToString() != wrk.ToLower())
{
tmpResTab += z;
}
}
tmpResStr = StringReduce(ref tmpRemoves, tmpResTab);
if (tmpResStr.Length < winner)
{
nextStr = tmpResStr;
winner = tmpResStr.Length;
wrkWin = wrk + wrk.ToLower();
}
}
}
WriteLine($"Resulterande sträng : {nextStr} , {nextStr.Length} tkn reducerad med {wrkWin}");
ReadKey();
nextStr = StringReduce(ref removes, resTab);
WriteLine($"Resulterande sträng : {nextStr} , {nextStr.Length} tkn");
ReadKey();
}
private static string StringReduce(ref int removes, string resTab)
{
int varv = 0;
string nextStr = nextLevelStr(resTab, ref removes, ref varv);
while (removes > 0)
{
nextStr = nextLevelStr(nextStr, ref removes, ref varv);
}
return nextStr;
}
private static string nextLevelStr(string resTab,ref int removes, ref int varv)
{
string tmp = "";
removes = 0;
//WriteLine($"inLängd: {resTab.Length}");
foreach(char tkn in resTab.ToCharArray())
{
tmp += tkn;
if (tmp.Length > 1)
{
string v = tmp.Substring(tmp.Length - 2, 1);
string z = tmp.Substring(tmp.Length - 1, 1);
if ((v.ToUpper()== v && z.ToLower() == z || (z.ToUpper() == z && v.ToLower() == v))
&& (v.ToUpper() == z.ToUpper()))
{
if (tmp.Length > 2)
{
tmp = tmp.Substring(0, tmp.Length - 2);
removes++;
}
else tmp = "";
}
}
}
WriteLine($"varv {++varv} ; aktuell längd: {tmp.Length} removes = {removes}");
return tmp;
}
}
}