-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day14.cs
192 lines (163 loc) · 5.81 KB
/
Day14.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
namespace AdventOfCode2021;
public static class Day14
{
public static void Part1()
{
string[] inputs = InputHelper.GetInput(14);
string polymerAsString = inputs[0];
Dictionary<ElementPair, long> polymer = new Dictionary<ElementPair, long>();
Dictionary<char, long> elementCount = new Dictionary<char, long>();
for (int i = 0; i < polymerAsString.Length - 1; i++)
{
char first = polymerAsString[i];
char second = polymerAsString[i + 1];
var pair = new ElementPair(first, second);
if (polymer.ContainsKey(pair))
{
polymer[pair]++;
}
else
{
polymer.Add(pair, 1);
}
}
foreach (char element in polymerAsString)
{
if (elementCount.ContainsKey(element))
{
elementCount[element]++;
}
else
{
elementCount.Add(element, 1);
}
}
IReadOnlyDictionary<ElementPair, char> pairInsertionRules
= ParsePairInsertionRules(inputs);
SimulatePairInsertion(polymer, elementCount, pairInsertionRules, 40);
long max = elementCount.Max(keyPair => keyPair.Value);
long min = elementCount.Min(keyPair => keyPair.Value);
Console.WriteLine(max - min);
}
private static void SimulatePairInsertion(Dictionary<ElementPair, long> polymer, Dictionary<char, long> elementCount, IReadOnlyDictionary<ElementPair, char> pairInsertionRules, int steps)
{
while (steps > 0)
{
steps--;
Dictionary<ElementPair, long> pairsToAdd = new Dictionary<ElementPair, long>();
foreach (KeyValuePair<ElementPair, long> pairCount in polymer)
{
ElementPair pair = pairCount.Key;
long count = pairCount.Value;
char insertedCharacter = pairInsertionRules[pair];
if (elementCount.ContainsKey(insertedCharacter))
{
elementCount[insertedCharacter] += count;
}
else
{
elementCount.Add(insertedCharacter, count);
}
ElementPair newPair1 = new ElementPair(pair.First, insertedCharacter);
ElementPair newPair2 = new ElementPair(insertedCharacter, pair.Second);
if (pairsToAdd.ContainsKey(pair))
{
pairsToAdd[pair] -= count;
}
else
{
pairsToAdd.Add(pair, -count);
}
if (pairsToAdd.ContainsKey(newPair1))
{
pairsToAdd[newPair1] += count;
}
else
{
pairsToAdd.Add(newPair1, count);
}
if (pairsToAdd.ContainsKey(newPair2))
{
pairsToAdd[newPair2] += count;
}
else
{
pairsToAdd.Add(newPair2, count);
}
}
foreach (KeyValuePair<ElementPair, long> pairCount in pairsToAdd)
{
if (polymer.ContainsKey(pairCount.Key))
{
polymer[pairCount.Key] += pairCount.Value;
}
else
{
polymer.Add(pairCount.Key, pairCount.Value);
}
}
}
}
private static IReadOnlyDictionary<ElementPair, char> ParsePairInsertionRules(string[] inputs)
{
Dictionary<ElementPair, char> pairInsertionRules = new Dictionary<ElementPair, char>();
for (int i = 2; i < inputs.Length; i++)
{
string pairInsertionAsString = inputs[i];
char first = pairInsertionAsString[0];
char second = pairInsertionAsString[1];
char inserted = pairInsertionAsString[^1];
pairInsertionRules.Add((first, second), inserted);
}
return pairInsertionRules;
}
public static void Part2()
{
string[] inputs = InputHelper.GetInput(14);
string polymerAsString = inputs[0];
Dictionary<ElementPair, long> polymer = new Dictionary<ElementPair, long>();
Dictionary<char, long> elementCount = new Dictionary<char, long>();
for (int i = 0; i < polymerAsString.Length - 1; i++)
{
char first = polymerAsString[i];
char second = polymerAsString[i + 1];
var pair = new ElementPair(first, second);
if (polymer.ContainsKey(pair))
{
polymer[pair]++;
}
else
{
polymer.Add(pair, 1);
}
}
foreach (char element in polymerAsString)
{
if (elementCount.ContainsKey(element))
{
elementCount[element]++;
}
else
{
elementCount.Add(element, 1);
}
}
IReadOnlyDictionary<ElementPair, char> pairInsertionRules
= ParsePairInsertionRules(inputs);
SimulatePairInsertion(polymer, elementCount, pairInsertionRules, 40);
long max = elementCount.Max(keyPair => keyPair.Value);
long min = elementCount.Min(keyPair => keyPair.Value);
Console.WriteLine(max - min);
}
}
internal record struct ElementPair(char First, char Second)
{
public static implicit operator (char first, char second)(ElementPair value)
{
return (value.First, value.Second);
}
public static implicit operator ElementPair((char first, char second) value)
{
return new ElementPair(value.first, value.second);
}
}