Code for Day05 part 2
This commit is contained in:
@@ -15,6 +15,16 @@ namespace AdventOfCode2018.Tests
|
|||||||
Assert.AreEqual("10", result);
|
Assert.AreEqual("10", result);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[TestMethod()]
|
||||||
|
public void ResolvePart2__Test()
|
||||||
|
{
|
||||||
|
Day05 day05 = new Day05();
|
||||||
|
|
||||||
|
string result = day05.ResolvePart2("dabAcCaCBAcCcaDA");
|
||||||
|
|
||||||
|
Assert.AreEqual("4", result);
|
||||||
|
}
|
||||||
|
|
||||||
#region ReducePolymer
|
#region ReducePolymer
|
||||||
|
|
||||||
[TestMethod()]
|
[TestMethod()]
|
||||||
@@ -102,5 +112,89 @@ namespace AdventOfCode2018.Tests
|
|||||||
}
|
}
|
||||||
|
|
||||||
#endregion FullyReducePolymer
|
#endregion FullyReducePolymer
|
||||||
|
|
||||||
|
#region RemoveUnitTypeFromPolymer
|
||||||
|
|
||||||
|
[TestMethod()]
|
||||||
|
public void RemoveUnitTypeFromPolymer__Remove_a()
|
||||||
|
{
|
||||||
|
Day05 day05 = new Day05();
|
||||||
|
|
||||||
|
string result = day05.RemoveUnitTypeFromPolymer("dabAcCaCBAcCcaDA", 'a');
|
||||||
|
|
||||||
|
Assert.AreEqual("dbcCCBcCcD", result);
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod()]
|
||||||
|
public void RemoveUnitTypeFromPolymer__Remove_b()
|
||||||
|
{
|
||||||
|
Day05 day05 = new Day05();
|
||||||
|
|
||||||
|
string result = day05.RemoveUnitTypeFromPolymer("dabAcCaCBAcCcaDA", 'b');
|
||||||
|
|
||||||
|
Assert.AreEqual("daAcCaCAcCcaDA", result);
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod()]
|
||||||
|
public void RemoveUnitTypeFromPolymer__Remove_c()
|
||||||
|
{
|
||||||
|
Day05 day05 = new Day05();
|
||||||
|
|
||||||
|
string result = day05.RemoveUnitTypeFromPolymer("dabAcCaCBAcCcaDA", 'c');
|
||||||
|
|
||||||
|
Assert.AreEqual("dabAaBAaDA", result);
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod()]
|
||||||
|
public void RemoveUnitTypeFromPolymer__Remove_d()
|
||||||
|
{
|
||||||
|
Day05 day05 = new Day05();
|
||||||
|
|
||||||
|
string result = day05.RemoveUnitTypeFromPolymer("dabAcCaCBAcCcaDA", 'd');
|
||||||
|
|
||||||
|
Assert.AreEqual("abAcCaCBAcCcaA", result);
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod()]
|
||||||
|
public void RemoveUnitTypeFromPolymer__Remove_A()
|
||||||
|
{
|
||||||
|
Day05 day05 = new Day05();
|
||||||
|
|
||||||
|
string result = day05.RemoveUnitTypeFromPolymer("dabAcCaCBAcCcaDA", 'A');
|
||||||
|
|
||||||
|
Assert.AreEqual("dbcCCBcCcD", result);
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod()]
|
||||||
|
public void RemoveUnitTypeFromPolymer__Remove_B()
|
||||||
|
{
|
||||||
|
Day05 day05 = new Day05();
|
||||||
|
|
||||||
|
string result = day05.RemoveUnitTypeFromPolymer("dabAcCaCBAcCcaDA", 'B');
|
||||||
|
|
||||||
|
Assert.AreEqual("daAcCaCAcCcaDA", result);
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod()]
|
||||||
|
public void RemoveUnitTypeFromPolymer__Remove_C()
|
||||||
|
{
|
||||||
|
Day05 day05 = new Day05();
|
||||||
|
|
||||||
|
string result = day05.RemoveUnitTypeFromPolymer("dabAcCaCBAcCcaDA", 'C');
|
||||||
|
|
||||||
|
Assert.AreEqual("dabAaBAaDA", result);
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod()]
|
||||||
|
public void RemoveUnitTypeFromPolymer__Remove_D()
|
||||||
|
{
|
||||||
|
Day05 day05 = new Day05();
|
||||||
|
|
||||||
|
string result = day05.RemoveUnitTypeFromPolymer("dabAcCaCBAcCcaDA", 'D');
|
||||||
|
|
||||||
|
Assert.AreEqual("abAcCaCBAcCcaA", result);
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion RemoveUnitTypeFromPolymer
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,4 +1,6 @@
|
|||||||
using System.Text;
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
|
||||||
namespace AdventOfCode2018
|
namespace AdventOfCode2018
|
||||||
{
|
{
|
||||||
@@ -29,6 +31,23 @@ namespace AdventOfCode2018
|
|||||||
|
|
||||||
How many units remain after fully reacting the polymer you scanned? (Note: in this puzzle and others, the input is large; if you copy/paste your input, make sure you get the whole thing.)
|
How many units remain after fully reacting the polymer you scanned? (Note: in this puzzle and others, the input is large; if you copy/paste your input, make sure you get the whole thing.)
|
||||||
|
|
||||||
|
--- Part Two ---
|
||||||
|
|
||||||
|
Time to improve the polymer.
|
||||||
|
|
||||||
|
One of the unit types is causing problems; it's preventing the polymer from collapsing as much as it should. Your goal is to figure out which unit type is causing the most problems, remove all instances of it (regardless of polarity), fully react the remaining polymer, and measure its length.
|
||||||
|
|
||||||
|
For example, again using the polymer dabAcCaCBAcCcaDA from above:
|
||||||
|
|
||||||
|
Removing all A/a units produces dbcCCBcCcD. Fully reacting this polymer produces dbCBcD, which has length 6.
|
||||||
|
Removing all B/b units produces daAcCaCAcCcaDA. Fully reacting this polymer produces daCAcaDA, which has length 8.
|
||||||
|
Removing all C/c units produces dabAaBAaDA. Fully reacting this polymer produces daDA, which has length 4.
|
||||||
|
Removing all D/d units produces abAcCaCBAcCcaA. Fully reacting this polymer produces abCBAc, which has length 6.
|
||||||
|
|
||||||
|
In this example, removing all C/c units was best, producing the answer 4.
|
||||||
|
|
||||||
|
What is the length of the shortest polymer you can produce by removing all units of exactly one type and fully reacting the result?
|
||||||
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
public class Day05
|
public class Day05
|
||||||
@@ -75,9 +94,37 @@ namespace AdventOfCode2018
|
|||||||
return reducedPolymer.Length.ToString();
|
return reducedPolymer.Length.ToString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public string RemoveUnitTypeFromPolymer(string polymer, char unitType)
|
||||||
|
{
|
||||||
|
StringBuilder sb = new StringBuilder();
|
||||||
|
unitType = char.ToLower(unitType);
|
||||||
|
foreach (char c in polymer)
|
||||||
|
{
|
||||||
|
if (char.ToLower(c) != unitType)
|
||||||
|
{
|
||||||
|
sb.Append(c);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return sb.ToString();
|
||||||
|
}
|
||||||
|
|
||||||
public string ResolvePart2(string input)
|
public string ResolvePart2(string input)
|
||||||
{
|
{
|
||||||
return null;
|
List<char> allUnitTypes = input.Select(c => char.ToLower(c)).Distinct().ToList();
|
||||||
|
|
||||||
|
int minPolymerLenght = int.MaxValue;
|
||||||
|
foreach (char unitType in allUnitTypes)
|
||||||
|
{
|
||||||
|
string fixedPolymer = RemoveUnitTypeFromPolymer(input, unitType);
|
||||||
|
string fixedReducedPolymer = FullyReducePolymer(fixedPolymer);
|
||||||
|
int fixedReducedPolymerLenght = fixedReducedPolymer.Length;
|
||||||
|
if (minPolymerLenght > fixedReducedPolymerLenght)
|
||||||
|
{
|
||||||
|
minPolymerLenght = fixedReducedPolymerLenght;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return minPolymerLenght.ToString();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user