diff --git a/AOC.Test/Test2015.cs b/AOC.Test/Test2015.cs index ceac1cc..1c0b16e 100644 --- a/AOC.Test/Test2015.cs +++ b/AOC.Test/Test2015.cs @@ -24,6 +24,7 @@ public class Test2015 [DataRow(typeof(Day16), "103", "405")] [DataRow(typeof(Day17), "1304", "18")] [DataRow(typeof(Day18), "1061", "1006")] + [DataRow(typeof(Day19), "576", "207")] public void CheckAllDays(Type dayType, string part1, string part2) { Common.CheckDay(dayType, part1, part2); @@ -42,6 +43,7 @@ public class Test2015 [DataRow(typeof(Day10), "237746", "3369156")] [DataRow(typeof(Day13), "330", "286")] [DataRow(typeof(Day15), "62842880", "57600000")] + [DataRow(typeof(Day19), "4", "2")] public void CheckTestInputs(Type dayType, string part1, string part2) { Common.CheckDay(dayType, part1, part2, true); diff --git a/AOC2015/AOC2015.csproj b/AOC2015/AOC2015.csproj index 77ab940..50a1bbe 100644 --- a/AOC2015/AOC2015.csproj +++ b/AOC2015/AOC2015.csproj @@ -27,6 +27,7 @@ + diff --git a/AOC2015/Day19.cs b/AOC2015/Day19.cs index 21a1f1b..ddd2c6b 100644 --- a/AOC2015/Day19.cs +++ b/AOC2015/Day19.cs @@ -1,3 +1,5 @@ +using MoreLinq; + namespace AOC2015; /// @@ -5,11 +7,42 @@ namespace AOC2015; /// public sealed class Day19() : Day(2015, 19, "Medicine for Rudolph") { + private IEnumerable? _rules; + private string? _compound; + public override void ProcessInput() { + var i = Input.Split("").ToList(); + _rules = i[0].Select(r => r.Split(" => ")); + _compound = i[1].Single(); } - public override object Part1() => ""; + private int CountSubstring(string needle) + { + var count = 0; + for (var i = _compound!.IndexOf(needle, StringComparison.InvariantCulture); + i >= 0; + ++count, i = _compound.IndexOf(needle, i + 1, StringComparison.InvariantCulture)) + { + } - public override object Part2() => ""; -} + return count; + } + + public override object Part1() + { + HashSet compounds = new(); + foreach (var rule in _rules!) + foreach (var match in Regex.EnumerateMatches(_compound, rule[0])) + compounds.Add(_compound!.Remove(match.Index, rule[0].Length).Insert(match.Index, rule[1])); + + return compounds.Count; + } + + public override object Part2() => + _compound!.Count(char.IsUpper) + - CountSubstring("Rn") + - CountSubstring("Ar") + - 2 * CountSubstring("Y") + - 1; +} \ No newline at end of file diff --git a/AOC2015/input2015/test19.in b/AOC2015/input2015/test19.in new file mode 100644 index 0000000..af30faa --- /dev/null +++ b/AOC2015/input2015/test19.in @@ -0,0 +1,5 @@ +H => HO +H => OH +O => HH + +HOH