From a35f71f02e8653d9738c698cd1f5837bb82327fc Mon Sep 17 00:00:00 2001 From: Josh Creek Date: Tue, 14 Dec 2021 13:58:13 +0000 Subject: [PATCH] feat(*): Add 2021-14 part 2 initial not efficient enough attempt files --- 2021/14/Program.cs | 96 +++++++++++++++++++++++++++++++++++++++++-- 2021/14/TaskPart2.txt | 6 +++ 2 files changed, 99 insertions(+), 3 deletions(-) create mode 100644 2021/14/TaskPart2.txt diff --git a/2021/14/Program.cs b/2021/14/Program.cs index 420d788..2da7c65 100644 --- a/2021/14/Program.cs +++ b/2021/14/Program.cs @@ -1,4 +1,6 @@ -namespace Day14 +using System.Diagnostics; + +namespace Day14 { class Program { @@ -7,8 +9,8 @@ string[] lines = File.ReadAllLines("input.txt"); - Part1(lines); - //Part2(lines); + //Part1(lines); + Part2(lines); } static void Part1(string[] lines) @@ -62,6 +64,93 @@ Console.WriteLine(mostCommon.count - leastCommon.count); } + static void Part2(string[] lines) + { + string polymerTemplate = lines[0]; + + Console.WriteLine($"Template: {polymerTemplate}"); + + // Pair insertion rules are stored from index 2 onwards + // A rule like AB -> C means that when elements A and B are immediately adjacent, element C + // should be inserted between them. + // Also, because all pairs are considered simultaneously, inserted elements are not considered + // to be part of a pair until the next step. + + Dictionary<(char, char), char> rules = new Dictionary<(char, char), char>(); + // Consider each rule + for (int i = 2; i < lines.Length; i++) + { + // e.g. AB -> C + string pairToMatch = lines[i].Substring(0, 2); + string characterToInsert = lines[i].Substring(6, 1); + + rules.Add((pairToMatch[0], pairToMatch[1]), characterToInsert[0]); + } + + // Set the number of steps to run + for (int i = 0; i < 40; i++) + { + Stopwatch stopWatch = new Stopwatch(); + stopWatch.Start(); + + CompleteStepNew(ref polymerTemplate, rules); + + stopWatch.Stop(); + TimeSpan ts = stopWatch.Elapsed; + string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}", + ts.Hours, ts.Minutes, ts.Seconds, + ts.Milliseconds / 10); + Console.WriteLine($"Step {i + 1} took {elapsedTime}"); + } + + // Count occurrences of each character + char[] chars = polymerTemplate.ToCharArray(); + List uniqueCharacters = chars.Select(x => x).Distinct().ToList(); + List<(char character, int count)> countList = new List<(char character, int count)>(); + + foreach (char character in uniqueCharacters) + { + countList.Add((character, polymerTemplate.Count(c => c == character))); + } + + (char character, int count) mostCommon = countList.MaxBy(x => x.count); + (char character, int count) leastCommon = countList.MinBy(x => x.count); + + Console.WriteLine(mostCommon.count - leastCommon.count); + } + + static void CompleteStepNew(ref string polymerTemplate, Dictionary<(char, char), char> rules) + { + LinkedList temporaryList = new LinkedList(); + char[] temporaryTemplate = polymerTemplate.ToCharArray(); + + for (int i = 0; i < temporaryTemplate.Length; i++) + { + temporaryList.AddLast(temporaryTemplate[i]); + } + + LinkedListNode pointer = temporaryList.Find(temporaryList.First()); + + // Insert the new character into the linked list but do not change the temporary template we're checking against + for (int i = 0; i < temporaryTemplate.Count() - 1; i++) + { + char insertValue; + rules.TryGetValue((temporaryTemplate[i], temporaryTemplate[i + 1]), out insertValue); + + if (char.IsLetter(insertValue)) + { + temporaryList.AddAfter(pointer, insertValue); + + // Move on an extra position as we have added a node + pointer = pointer.Next; + } + + pointer = pointer.Next; + } + + polymerTemplate = String.Join("", temporaryList); + } + static void CompleteStep(ref string polymerTemplate, ref string[] lines, List rules) { LinkedList temporaryList = new LinkedList(); @@ -94,6 +183,7 @@ polymerTemplate = String.Join("", temporaryList); } + class Rule { public char First { get; set; } diff --git a/2021/14/TaskPart2.txt b/2021/14/TaskPart2.txt new file mode 100644 index 0000000..e914d44 --- /dev/null +++ b/2021/14/TaskPart2.txt @@ -0,0 +1,6 @@ +--- Part Two --- +The resulting polymer isn't nearly strong enough to reinforce the submarine. You'll need to run more steps of the pair insertion process; a total of 40 steps should do it. + +In the above example, the most common element is B (occurring 2192039569602 times) and the least common element is H (occurring 3849876073 times); subtracting these produces 2188189693529. + +Apply 40 steps of pair insertion to the polymer template and find the most and least common elements in the result. What do you get if you take the quantity of the most common element and subtract the quantity of the least common element? \ No newline at end of file