Day02 part 2
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace AdventOfCode2018
|
||||
{
|
||||
@@ -30,6 +31,24 @@ namespace AdventOfCode2018
|
||||
|
||||
What is the checksum for your list of box IDs?
|
||||
|
||||
--- Part Two ---
|
||||
|
||||
Confident that your list of box IDs is complete, you're ready to find the boxes full of prototype fabric.
|
||||
|
||||
The boxes will have IDs which differ by exactly one character at the same position in both strings. For example, given the following box IDs:
|
||||
|
||||
abcde
|
||||
fghij
|
||||
klmno
|
||||
pqrst
|
||||
fguij
|
||||
axcye
|
||||
wvxyz
|
||||
|
||||
The IDs abcde and axcye are close, but they differ by two characters (the second and fourth). However, the IDs fghij and fguij differ by exactly one character, the third (h and u). Those must be the correct boxes.
|
||||
|
||||
What letters are common between the two correct box IDs? (In the example above, this is found by removing the differing character from either ID, producing fgij.)
|
||||
|
||||
*/
|
||||
|
||||
public class Day02
|
||||
@@ -68,5 +87,32 @@ namespace AdventOfCode2018
|
||||
|
||||
return result.ToString();
|
||||
}
|
||||
|
||||
private Tuple<int, string> CompareIDPair(string id1, string id2)
|
||||
{
|
||||
if (id1.Length != id2.Length) { throw new ArgumentException("id1 and id2 parameters must be of same length"); }
|
||||
int diffCount = 0;
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (int i = 0; i < id1.Length; i++)
|
||||
{
|
||||
if (id1[i] != id2[i]) { diffCount++; }
|
||||
else { sb.Append(id1[i]); }
|
||||
}
|
||||
return new Tuple<int, string>(diffCount, sb.ToString());
|
||||
}
|
||||
|
||||
public string ResolveDay02_Part2(string[] inputs)
|
||||
{
|
||||
for (int i = 0; i < (inputs.Length - 1); i++)
|
||||
{
|
||||
for (int j = (i + 1); j < inputs.Length; j++)
|
||||
{
|
||||
var result = CompareIDPair(inputs[i], inputs[j]);
|
||||
if (result.Item1 == 1) { return result.Item2; }
|
||||
}
|
||||
}
|
||||
return string.Empty;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user