feat(*): Add 2021-14 files (part 1 not working, part 2 not attempted)

This commit is contained in:
Josh Creek
2021-12-14 10:07:56 +00:00
parent eb2ef8717f
commit 58cd160e82
5 changed files with 200 additions and 1 deletions
+17
View File
@@ -0,0 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<RootNamespace>_14</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<None Update="input.txt">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>
+104
View File
@@ -0,0 +1,104 @@
namespace Day14
{
class Program
{
static void Main(string[] args)
{
string[] lines = File.ReadAllLines("input.txt");
Part1(lines);
//Part2(lines);
}
static void Part1(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.
List<Rule> rules = new List<Rule>();
// 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(new Rule()
{
First = pairToMatch[0],
Second = pairToMatch[1],
Insert = characterToInsert[0],
});
}
// Set the number of steps to run (1 lower than expected due to 0 indexing)
for (int i = 0; i < 9; i++)
{
CompleteStep(ref polymerTemplate, ref lines, rules);
//Console.WriteLine($"After step {i+1}: {polymerTemplate}");
}
// Count occurrences of each character
char[] chars = polymerTemplate.ToCharArray();
List<char> 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 CompleteStep(ref string polymerTemplate, ref string[] lines, List<Rule> rules)
{
LinkedList<char> temporaryList = new LinkedList<char>();
List<char> temporaryTemplate = new List<char>();
foreach (char character in polymerTemplate)
{
temporaryList.AddLast(character);
temporaryTemplate.Add(character);
}
LinkedListNode<char> 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++)
{
Rule matchingRule = rules.FirstOrDefault(r => r.First == temporaryTemplate.ElementAt(i) && r.Second == temporaryTemplate.ElementAt(i + 1));
if (matchingRule is not null)
{
temporaryList.AddAfter(pointer, matchingRule.Insert);
// Move on an extra position as we have added a node
pointer = pointer.Next;
}
pointer = pointer.Next;
}
polymerTemplate = String.Join("", temporaryList);
}
class Rule
{
public char First { get; set; }
public char Second { get; set; }
public char Insert { get; set; }
}
}
}
+48
View File
@@ -0,0 +1,48 @@
--- Day 14: Extended Polymerization ---
The incredible pressures at this depth are starting to put a strain on your submarine. The submarine has polymerization equipment that would produce suitable materials to reinforce the submarine, and the nearby volcanically-active caves should even have the necessary input elements in sufficient quantities.
The submarine manual contains instructions for finding the optimal polymer formula; specifically, it offers a polymer template and a list of pair insertion rules (your puzzle input). You just need to work out what polymer would result after repeating the pair insertion process a few times.
For example:
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
The first line is the polymer template - this is the starting point of the process.
The following section defines the pair insertion rules. A rule like AB -> C means that when elements A and B are immediately adjacent, element C should be inserted between them. These insertions all happen simultaneously.
So, starting with the polymer template NNCB, the first step simultaneously considers all three pairs:
The first pair (NN) matches the rule NN -> C, so element C is inserted between the first N and the second N.
The second pair (NC) matches the rule NC -> B, so element B is inserted between the N and the C.
The third pair (CB) matches the rule CB -> H, so element H is inserted between the C and the B.
Note that these pairs overlap: the second element of one pair is the first element of the next pair. Also, because all pairs are considered simultaneously, inserted elements are not considered to be part of a pair until the next step.
After the first step of this process, the polymer becomes NCNBCHB.
Here are the results of a few steps using the above rules:
Template: NNCB
After step 1: NCNBCHB
After step 2: NBCCNBBBCBHCB
After step 3: NBBBCNCCNBBNBNBBCHBHHBCHB
After step 4: NBBNBNBBCCNBCNCCNBBNBBNBBBNBBNBBCBHCBHHNHCBBCBHCB
This polymer grows quickly. After step 5, it has length 97; After step 10, it has length 3073. After step 10, B occurs 1749 times, C occurs 298 times, H occurs 161 times, and N occurs 865 times; taking the quantity of the most common element (B, 1749) and subtracting the quantity of the least common element (H, 161) produces 1749 - 161 = 1588.
Apply 10 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?
+18
View File
@@ -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
+13 -1
View File
@@ -21,7 +21,11 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "02", "02\02.csproj", "{D38B
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "01", "01\01.csproj", "{58C837E5-8A6F-4794-80DC-D056B8665586}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "10", "10\10.csproj", "{C9300285-27F4-4BC9-98D3-158BD66E7859}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "10", "10\10.csproj", "{C9300285-27F4-4BC9-98D3-158BD66E7859}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "11", "11\11.csproj", "{6F13298E-B8CA-4A67-B1E7-320B5637CE6A}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "14", "14\14.csproj", "{9F9653A8-5A92-40C3-A855-7D0F0EAEC422}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -69,6 +73,14 @@ Global
{C9300285-27F4-4BC9-98D3-158BD66E7859}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C9300285-27F4-4BC9-98D3-158BD66E7859}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C9300285-27F4-4BC9-98D3-158BD66E7859}.Release|Any CPU.Build.0 = Release|Any CPU
{6F13298E-B8CA-4A67-B1E7-320B5637CE6A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{6F13298E-B8CA-4A67-B1E7-320B5637CE6A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6F13298E-B8CA-4A67-B1E7-320B5637CE6A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6F13298E-B8CA-4A67-B1E7-320B5637CE6A}.Release|Any CPU.Build.0 = Release|Any CPU
{9F9653A8-5A92-40C3-A855-7D0F0EAEC422}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{9F9653A8-5A92-40C3-A855-7D0F0EAEC422}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9F9653A8-5A92-40C3-A855-7D0F0EAEC422}.Release|Any CPU.ActiveCfg = Release|Any CPU
{9F9653A8-5A92-40C3-A855-7D0F0EAEC422}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE