Day02 part 2
This commit is contained in:
@@ -22,5 +22,23 @@ namespace AdventOfCode2018.Tests
|
|||||||
|
|
||||||
Assert.AreEqual("12", result);
|
Assert.AreEqual("12", result);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[TestMethod()]
|
||||||
|
public void ResolveDay02_Part2__Test()
|
||||||
|
{
|
||||||
|
Day02 day02 = new Day02();
|
||||||
|
|
||||||
|
string result = day02.ResolveDay02_Part2(new string[] {
|
||||||
|
"abcde",
|
||||||
|
"fghij",
|
||||||
|
"klmno",
|
||||||
|
"pqrst",
|
||||||
|
"fguij",
|
||||||
|
"axcye",
|
||||||
|
"wvxyz",
|
||||||
|
});
|
||||||
|
|
||||||
|
Assert.AreEqual("fgij", result);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,5 +1,6 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
|
||||||
namespace AdventOfCode2018
|
namespace AdventOfCode2018
|
||||||
{
|
{
|
||||||
@@ -30,6 +31,24 @@ namespace AdventOfCode2018
|
|||||||
|
|
||||||
What is the checksum for your list of box IDs?
|
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
|
public class Day02
|
||||||
@@ -68,5 +87,32 @@ namespace AdventOfCode2018
|
|||||||
|
|
||||||
return result.ToString();
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -27,6 +27,8 @@ namespace AdventOfCode2018
|
|||||||
string[] linesDay02 = File.ReadAllLines("inputs/Day02.txt");
|
string[] linesDay02 = File.ReadAllLines("inputs/Day02.txt");
|
||||||
string resultDay02 = day02.ResolveDay02(linesDay02);
|
string resultDay02 = day02.ResolveDay02(linesDay02);
|
||||||
Console.WriteLine("Day02 Result: {0}", resultDay02);
|
Console.WriteLine("Day02 Result: {0}", resultDay02);
|
||||||
|
string resultDay02Part2 = day02.ResolveDay02_Part2(linesDay02);
|
||||||
|
Console.WriteLine("Day02_Part2 Result: {0}", resultDay02Part2);
|
||||||
}
|
}
|
||||||
|
|
||||||
Console.Read();
|
Console.Read();
|
||||||
|
|||||||
Reference in New Issue
Block a user