Code for Day05 part 1
This commit is contained in:
@@ -14,5 +14,93 @@ namespace AdventOfCode2018.Tests
|
||||
|
||||
Assert.AreEqual("10", result);
|
||||
}
|
||||
|
||||
#region ReducePolymer
|
||||
|
||||
[TestMethod()]
|
||||
public void ReducePolymer__Remove_cC()
|
||||
{
|
||||
Day05 day05 = new Day05();
|
||||
|
||||
string result = day05.ReducePolymer("dabAcCaCBA");
|
||||
|
||||
Assert.AreEqual("dabAaCBA", result);
|
||||
}
|
||||
|
||||
[TestMethod()]
|
||||
public void ReducePolymer__Remove_cC_AtEnd()
|
||||
{
|
||||
Day05 day05 = new Day05();
|
||||
|
||||
string result = day05.ReducePolymer("dabAcC");
|
||||
|
||||
Assert.AreEqual("dabA", result);
|
||||
}
|
||||
|
||||
[TestMethod()]
|
||||
public void ReducePolymer__Remove_Only_cC()
|
||||
{
|
||||
Day05 day05 = new Day05();
|
||||
|
||||
string result = day05.ReducePolymer("cC");
|
||||
|
||||
Assert.AreEqual("", result);
|
||||
}
|
||||
|
||||
[TestMethod()]
|
||||
public void ReducePolymer__Remove_cC_AtStart()
|
||||
{
|
||||
Day05 day05 = new Day05();
|
||||
|
||||
string result = day05.ReducePolymer("cCAAAA");
|
||||
|
||||
Assert.AreEqual("AAAA", result);
|
||||
}
|
||||
|
||||
[TestMethod()]
|
||||
public void ReducePolymer__Remove_Aa()
|
||||
{
|
||||
Day05 day05 = new Day05();
|
||||
|
||||
string result = day05.ReducePolymer("dabAaCBA");
|
||||
|
||||
Assert.AreEqual("dabCBA", result);
|
||||
}
|
||||
|
||||
[TestMethod()]
|
||||
public void ReducePolymer__Remove_cCc()
|
||||
{
|
||||
Day05 day05 = new Day05();
|
||||
|
||||
string result = day05.ReducePolymer("dabCBAcCcaDA");
|
||||
|
||||
Assert.AreEqual("dabCBAcaDA", result);
|
||||
}
|
||||
|
||||
[TestMethod()]
|
||||
public void ReducePolymer__Irreductible()
|
||||
{
|
||||
Day05 day05 = new Day05();
|
||||
|
||||
string result = day05.ReducePolymer("dabCBAcaDA");
|
||||
|
||||
Assert.AreEqual("dabCBAcaDA", result);
|
||||
}
|
||||
|
||||
#endregion ReducePolymer
|
||||
|
||||
#region FullyReducePolymer
|
||||
|
||||
[TestMethod()]
|
||||
public void FullyReducePolymer__Test()
|
||||
{
|
||||
Day05 day05 = new Day05();
|
||||
|
||||
string result = day05.FullyReducePolymer("dabAcCaCBAcCcaDA");
|
||||
|
||||
Assert.AreEqual("dabCBAcaDA", result);
|
||||
}
|
||||
|
||||
#endregion FullyReducePolymer
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,6 @@
|
||||
namespace AdventOfCode2018
|
||||
using System.Text;
|
||||
|
||||
namespace AdventOfCode2018
|
||||
{
|
||||
/*
|
||||
--- Day 5: Alchemical Reduction ---
|
||||
@@ -31,9 +33,46 @@
|
||||
|
||||
public class Day05
|
||||
{
|
||||
public string ReducePolymer(string polymer)
|
||||
{
|
||||
if (polymer.Length <= 1) { return polymer; }
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
||||
int i;
|
||||
for (i = 1; i < polymer.Length; i++)
|
||||
{
|
||||
char previousCharacter = polymer[i - 1];
|
||||
char character = polymer[i];
|
||||
if (previousCharacter != character && char.ToLower(previousCharacter) == char.ToLower(character))
|
||||
{
|
||||
i++;
|
||||
}
|
||||
else
|
||||
{
|
||||
sb.Append(previousCharacter);
|
||||
}
|
||||
}
|
||||
if (i == polymer.Length) { sb.Append(polymer[i - 1]); }
|
||||
|
||||
return sb.ToString();
|
||||
}
|
||||
|
||||
public string FullyReducePolymer(string input)
|
||||
{
|
||||
string previousPolymer = null;
|
||||
string polymer = input;
|
||||
do
|
||||
{
|
||||
previousPolymer = polymer;
|
||||
polymer = ReducePolymer(polymer);
|
||||
} while (previousPolymer.Length > polymer.Length);
|
||||
return polymer;
|
||||
}
|
||||
|
||||
public string ResolvePart1(string input)
|
||||
{
|
||||
return null;
|
||||
string reducedPolymer = FullyReducePolymer(input);
|
||||
return reducedPolymer.Length.ToString();
|
||||
}
|
||||
|
||||
public string ResolvePart2(string input)
|
||||
|
||||
Reference in New Issue
Block a user