137 lines
3.7 KiB
C#
137 lines
3.7 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
|
|
namespace _2018_02
|
|
{
|
|
class Program
|
|
{
|
|
static void Main(string[] args)
|
|
{
|
|
int twos = 0, threes = 0;
|
|
|
|
Dictionary<string, int> resCheck = new Dictionary<string, int>();
|
|
string indata = File.ReadAllText(@"I:\Adventofcode_181201\data181202.txt");
|
|
//string indata = "abcdef\r\nbababc\r\nabbcde\r\nabcccd\r\naabcdd\r\nabcdee\r\nababab\r\n";
|
|
|
|
string[] resTab = indata.Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
|
|
|
|
foreach (var boxId in resTab)
|
|
{
|
|
char[] characters = boxId.ToArray();
|
|
Array.Sort(characters);
|
|
analyseString(characters, ref twos, ref threes);
|
|
|
|
|
|
}
|
|
|
|
System.Console.WriteLine($"twos: {twos} threes: {threes} checksum: {twos * threes} ");
|
|
|
|
SecondAnalyse(resTab);
|
|
|
|
|
|
}
|
|
|
|
|
|
private static void analyseString(char[] characters, ref int twos, ref int threes)
|
|
{
|
|
int locTwos = 0, locthrees = 0;
|
|
char lastChar = ' ';
|
|
byte cnt = 0;
|
|
foreach (var tkn in characters)
|
|
{
|
|
if (tkn == lastChar)
|
|
{
|
|
cnt++;
|
|
}
|
|
else
|
|
{
|
|
if (cnt == 2)
|
|
{
|
|
if (locTwos == 0)
|
|
locTwos++;
|
|
}
|
|
if (cnt == 3)
|
|
{
|
|
if (locthrees == 0)
|
|
{
|
|
locthrees++;
|
|
}
|
|
}
|
|
lastChar = tkn;
|
|
cnt = 1;
|
|
}
|
|
}
|
|
if (cnt == 2)
|
|
{
|
|
if (locTwos == 0)
|
|
locTwos++;
|
|
}
|
|
if (cnt == 3)
|
|
{
|
|
if (locthrees == 0)
|
|
{
|
|
locthrees++;
|
|
}
|
|
}
|
|
|
|
twos += locTwos;
|
|
threes += locthrees;
|
|
}
|
|
|
|
private static void SecondAnalyse(string[] restab)
|
|
{
|
|
bool ready = false;
|
|
var xlen = restab[0].Length;
|
|
|
|
foreach (var str in restab)
|
|
{
|
|
for (int i = 0; i < xlen; i++)
|
|
{
|
|
foreach (var str2 in restab)
|
|
{
|
|
if (str2 == str) { }
|
|
else
|
|
{
|
|
string stx = "", sty = "";
|
|
var st0 = stringUtanPos(str, i, out stx);
|
|
var st1 = stringUtanPos(str2, i, out sty);
|
|
if (st0 == st1)
|
|
{
|
|
System.Console.WriteLine($"box 1: {str},\r\nbox 2: {str2} diff chars {stx} , {sty}\r\n{st0}\r\n{st1}");
|
|
ready = true;
|
|
break;
|
|
}
|
|
|
|
|
|
}
|
|
if (ready) break;
|
|
}
|
|
if (ready) break;
|
|
}
|
|
if (ready) break;
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
private static string stringUtanPos(string str, int i, out string stx)
|
|
{
|
|
string tmp = str;
|
|
stx = str.Substring(i, 1);
|
|
if (i > 0)
|
|
{
|
|
tmp = str.Substring(0, i) + str.Substring(i + 1);
|
|
}
|
|
else
|
|
{
|
|
tmp = str.Substring(i+1);
|
|
}
|
|
|
|
return tmp;
|
|
}
|
|
}
|
|
}
|