Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void Main()
- {
- ///TEST PART
- Debug.Assert(GetAnswerA("dabAcCaCBAcCcaDA") == 10);
- Debug.Assert(GetAnswerB("dabAcCaCBAcCcaDA") == 4);
- var aoc = new AdventOfCode(2018, 5);
- aoc.SubmitAnswer(GetAnswerA(aoc.InputLines[0]), Part.A);
- aoc.SubmitAnswer(GetAnswerB(aoc.InputLines[0]), Part.B);
- }
- public int GetAnswerB(string input)
- {
- //keep track of the shortest length found
- int best = int.MaxValue;
- //remove each letter in the alphabet
- for (char i = 'a'; i <= 'z'; i++)
- {
- //remove the upper and lower
- var result = input.Replace(i.ToString(), "");
- result = result.Replace(i.ToString().ToUpper(), "");
- //get the resulting length
- best = Math.Min(GetAnswerA(result), best);
- }
- return best;
- }
- public int GetAnswerA(string result)
- {
- //to array, so we can change values
- var linked = result.ToArray();
- //keep track if the lenght changed
- int len = 0;
- //keep repeating while the length changes
- while (len != linked.Length)
- {
- //update length
- len = linked.Length;
- //move over the array
- for(int i = 0; i < linked.Length - 1; i++)
- {
- int j = i + 1;
- //check inside the array?, are the upper and lower diff is 32 in dec)
- while(i >= 0 && j < linked.Length && Math.Abs(linked[i] - linked[j]) == 32)
- {
- //replate by null
- linked[i--] = '\0'; //move i back
- linked[j++] = '\0'; //move j forward (not on nulls anymore)
- }
- i = j -1; //continue where we were
- }
- //new array
- linked = linked.Where(x => x != '\0').ToArray();
- }
- return linked.Length;
- }
Advertisement
Add Comment
Please, Sign In to add comment