You've managed to sneak in to the prototype suit manufacturing lab. The Elves are making decent progress, but are still struggling with the suit's size reduction capabilities.
While the very latest in 1518 alchemical technology might have solved their problem eventually, you can do better. You scan the chemical composition of the suit's material and discover that it is formed by extremely long polymers (one of which is available as your puzzle input).
Read the full puzzle.
using System.Collections.Generic;
using System.Linq;
namespace AdventOfCode.Y2018.Day05;
[ProblemName("Alchemical Reduction")]
class Solution : Solver {
public object PartOne(string input) => React(input);
public object PartTwo(string input) => (from ch in "abcdefghijklmnopqrstuvwxyz" select React(input, ch)).Min();
char ToLower(char ch) => ch <= 'Z' ? (char)(ch - 'A' + 'a') : ch;
int React(string input, char? skip = null) {
var stack = new Stack<char>("⊥");
foreach (var ch in input) {
var top = stack.Peek();
if (ToLower(ch) == skip) {
continue;
} else if (top != ch && ToLower(ch) == ToLower(top)) {
stack.Pop();
} else {
stack.Push(ch);
}
}
return stack.Count() - 1;
}
}
Please ☆ my repo if you like it!