about summary refs log tree commit diff
diff options
context:
space:
mode:
authorBen Harris <ben@tilde.team>2021-12-14 00:51:30 -0500
committerBen Harris <ben@tilde.team>2021-12-14 00:51:30 -0500
commitf77d93de7e137764b8336a238546c2256c7f9a9b (patch)
tree19b8136491df09642eb62a35c33116bdce6592da
parent7273a296beecbdce7b25fbc5bdf6fb793eba148a (diff)
d14p1
-rw-r--r--aoc2021.test/DayTests.cs2
-rw-r--r--aoc2021/Day14.cs57
-rw-r--r--aoc2021/input/day14.in102
-rw-r--r--aoc2021/input/test14.in18
4 files changed, 179 insertions, 0 deletions
diff --git a/aoc2021.test/DayTests.cs b/aoc2021.test/DayTests.cs
index 4b27888..177f7d7 100644
--- a/aoc2021.test/DayTests.cs
+++ b/aoc2021.test/DayTests.cs
@@ -34,6 +34,7 @@ public class DayTests
     [DataRow(typeof(Day11), "1613", "510")]
     [DataRow(typeof(Day12), "4549", "120535")]
     [DataRow(typeof(Day13), "837", Day13Actual)]
+    [DataRow(typeof(Day14), "5656", "")]
     public void CheckAllDays(Type dayType, string part1, string part2)
     {
         var s = Stopwatch.StartNew();
@@ -77,6 +78,7 @@ public class DayTests
     [DataRow(typeof(Day11), "1656", "195")]
     [DataRow(typeof(Day12), "226", "3509")]
     [DataRow(typeof(Day13), "17", Day13Test)]
+    [DataRow(typeof(Day14), "1588", "2188189693529")]
     public void CheckTestInputs(Type dayType, string part1, string part2)
     {
         Day.UseTestInput = true;
diff --git a/aoc2021/Day14.cs b/aoc2021/Day14.cs
new file mode 100644
index 0000000..e4facd7
--- /dev/null
+++ b/aoc2021/Day14.cs
@@ -0,0 +1,57 @@
+namespace aoc2021;
+
+/// <summary>
+/// Day 14: <see href="https://adventofcode.com/2021/day/14"/>
+/// </summary>
+public sealed class Day14 : Day
+{
+    private readonly string _template;
+    private readonly Dictionary<string, string> _substitutionPairs;
+    
+    public Day14() : base(14, "Extended Polymerization")
+    {
+        _template = Input.First();
+        _substitutionPairs = Input.Skip(2).Select(l => l.Split(" -> ")).ToDictionary(k => k[0], v => v[1]);
+    }
+
+    private string DoStep(string input)
+    {
+        var result = new StringBuilder();
+        
+        for (var i = 0; i < input.Length - 1; i++)
+        {
+            var k = input.Substring(i, 2);
+            if (_substitutionPairs.ContainsKey(k))
+            {
+                result.Append(k[0]);
+                result.Append(_substitutionPairs[k]);
+            }
+            else
+            {
+                result.Append(k);
+            }
+        }
+
+        result.Append(input[^1]);
+
+        return result.ToString();
+    }
+
+    public override object Part1()
+    {
+        var s = Enumerable.Range(0, 10).Aggregate(_template, (current, _) => DoStep(current));
+
+        var most = s.ToCharArray()
+            .GroupBy(c => c)
+            .OrderByDescending(g => g.Count())
+            .Select(g => g.Count())
+            .ToList();
+        
+        return most.First() - most.Last();
+    }
+
+    public override object Part2()
+    {
+        return "";
+    }
+}
diff --git a/aoc2021/input/day14.in b/aoc2021/input/day14.in
new file mode 100644
index 0000000..6808cb7
--- /dev/null
+++ b/aoc2021/input/day14.in
@@ -0,0 +1,102 @@
+KHSSCSKKCPFKPPBBOKVF
+
+OS -> N
+KO -> O
+SK -> B
+NV -> N
+SH -> V
+OB -> V
+HH -> F
+HP -> H
+BP -> O
+HS -> K
+SN -> B
+PS -> C
+BS -> K
+CF -> H
+SO -> C
+NO -> H
+PP -> H
+SS -> P
+KV -> B
+KN -> V
+CC -> S
+HK -> H
+FN -> C
+OO -> K
+CH -> H
+CP -> V
+HB -> N
+VC -> S
+SP -> F
+BO -> F
+SF -> H
+VO -> B
+FF -> P
+CN -> O
+NP -> H
+KK -> N
+OP -> S
+BH -> F
+CB -> V
+HC -> P
+KH -> V
+OV -> V
+NK -> S
+PN -> F
+VV -> N
+HO -> S
+KS -> C
+FP -> F
+FH -> F
+BB -> C
+FB -> V
+SB -> K
+KP -> B
+FS -> C
+KC -> P
+SC -> C
+VF -> F
+VN -> B
+CK -> C
+KF -> H
+NS -> C
+FV -> K
+HV -> B
+HF -> K
+ON -> S
+CV -> N
+BV -> F
+NB -> N
+NN -> F
+BF -> N
+VB -> V
+VS -> K
+BK -> V
+VP -> P
+PB -> F
+KB -> C
+VK -> O
+NF -> F
+FO -> F
+PH -> N
+VH -> B
+HN -> B
+FK -> K
+PO -> H
+CO -> B
+FC -> V
+OK -> F
+OF -> V
+PF -> F
+BC -> B
+BN -> O
+NC -> K
+SV -> H
+OH -> B
+PC -> O
+OC -> C
+CS -> P
+PV -> V
+NH -> C
+PK -> H
diff --git a/aoc2021/input/test14.in b/aoc2021/input/test14.in
new file mode 100644
index 0000000..6c1c3a1
--- /dev/null
+++ b/aoc2021/input/test14.in
@@ -0,0 +1,18 @@
+NNCB
+
+CH -> B
+HH -> N
+CB -> H
+NH -> C
+HB -> C
+HC -> B
+HN -> C
+NN -> C
+BH -> H
+NC -> B
+NB -> B
+BN -> B
+BB -> N
+BC -> B
+CC -> N
+CN -> C
\ No newline at end of file