-
Notifications
You must be signed in to change notification settings - Fork 10
/
Day19.cs
173 lines (149 loc) · 5.09 KB
/
Day19.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
using AdventOfCode.CSharp.Common;
using System;
using System.Collections.Generic;
using System.Text;
namespace AdventOfCode.CSharp.Y2015.Solvers;
public class Day19 : ISolver
{
public static void Solve(ReadOnlySpan<byte> input, Solution solution)
{
input = input.TrimEnd((byte)'\n');
var elements = new Dictionary<string, int>();
var replacements = new List<List<List<int>>>(); // replacements[element][replacement][replacementElement]
foreach (Range lineRange in input.SplitLines())
{
ReadOnlySpan<byte> line = input[lineRange];
if (line.Length == 0)
{
break;
}
int lhsLen = line[1] == ' ' ? 1 : 2;
string lhs = Encoding.ASCII.GetString(line[0..lhsLen]);
ReadOnlySpan<byte> rhs = line[(lhsLen + 4)..];
List<int> replacement = ParseMolecule(elements, replacements, rhs);
if (!elements.TryGetValue(lhs, out int lhsIndex))
{
lhsIndex = elements.Count;
elements.Add(lhs, lhsIndex);
replacements.Add([replacement]);
}
else
{
replacements[lhsIndex].Add(replacement);
}
}
ReadOnlySpan<byte> moleculeSpan = input[(input.LastIndexOf((byte)'\n') + 1)..];
List<int>? molecule = ParseMolecule(elements, replacements, moleculeSpan);
int part1 = SolvePart1(replacements, molecule);
int part2 = SolvePart2(elements, molecule);
solution.SubmitPart1(part1);
solution.SubmitPart2(part2);
}
private static List<int> ParseMolecule(
Dictionary<string, int> elements,
List<List<List<int>>> replacements,
ReadOnlySpan<byte> moleculeSpan)
{
var replacement = new List<int>();
int i = 0;
while (i < moleculeSpan.Length)
{
string element;
if (i == moleculeSpan.Length - 1 || moleculeSpan[i + 1] is >= (byte)'A' and <= (byte)'Z')
{
element = char.ToString((char)moleculeSpan[i]);
i++;
}
else
{
element = Encoding.ASCII.GetString(moleculeSpan.Slice(i, 2));
i += 2;
}
if (!elements.TryGetValue(element, out int elementIndex))
{
elementIndex = elements.Count;
elements.Add(element, elementIndex);
replacements.Add([]);
}
replacement.Add(elementIndex);
}
return replacement;
}
private static int SolvePart1(List<List<List<int>>> replacements, List<int> molecule)
{
int total = 0;
for (int i = 0; i < molecule.Count; i++)
{
int cur = molecule[i];
// the last molecule can't overlap with any next molecules
// so we just assume all replacements are valid
if (i == molecule.Count - 1)
{
total += replacements[cur].Count;
break;
}
int next = molecule[i + 1];
// for each replacement for the current molecule, see if rep1 + next == cur + rep2
// if it is possible, don't count it.
foreach (List<int>? rep1 in replacements[cur])
{
if (rep1[0] != cur)
{
total++;
continue;
}
bool shouldCount = true;
foreach (List<int> rep2 in replacements[next])
{
if (rep1.Count == rep2.Count && rep2[^1] == next)
{
bool overlaps = true;
for (int j = 1; j < rep1.Count; j++)
{
if (rep1[j] != rep2[j - 1])
{
overlaps = false;
break;
}
}
if (overlaps)
{
shouldCount = false;
break;
}
}
}
if (shouldCount)
{
total++;
}
}
}
return total;
}
private static int SolvePart2(Dictionary<string, int> replacements, List<int> molecule)
{
int yElemIndex = replacements["Y"];
int rnElemIndex = replacements["Rn"];
int arElemIndex = replacements["Ar"];
int yCount = 0;
int rnCount = 0;
int arCount = 0;
foreach (int element in molecule)
{
if (element == yElemIndex)
{
yCount++;
}
else if (element == rnElemIndex)
{
rnCount++;
}
else if (element == arElemIndex)
{
arCount++;
}
}
return molecule.Count - rnCount - arCount - 2 * yCount - 1;
}
}