-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathDay19.cs
More file actions
173 lines (149 loc) · 5.02 KB
/
Day19.cs
File metadata and controls
173 lines (149 loc) · 5.02 KB
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 (var lineRange in input.SplitLines())
{
var line = input[lineRange];
if (line.Length == 0)
{
break;
}
var lhsLen = line[1] == ' ' ? 1 : 2;
var lhs = Encoding.ASCII.GetString(line[0..lhsLen]);
var rhs = line[(lhsLen + 4)..];
var replacement = ParseMolecule(elements, replacements, rhs);
if (!elements.TryGetValue(lhs, out var lhsIndex))
{
lhsIndex = elements.Count;
elements.Add(lhs, lhsIndex);
replacements.Add([replacement]);
}
else
{
replacements[lhsIndex].Add(replacement);
}
}
var moleculeSpan = input[(input.LastIndexOf((byte)'\n') + 1)..];
var molecule = ParseMolecule(elements, replacements, moleculeSpan);
var part1 = SolvePart1(replacements, molecule);
var 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>();
var 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 var 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)
{
var total = 0;
for (var i = 0; i < molecule.Count; i++)
{
var 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;
}
var 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 (var rep1 in replacements[cur])
{
if (rep1[0] != cur)
{
total++;
continue;
}
var shouldCount = true;
foreach (var rep2 in replacements[next])
{
if (rep1.Count == rep2.Count && rep2[^1] == next)
{
var overlaps = true;
for (var 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)
{
var yElemIndex = replacements["Y"];
var rnElemIndex = replacements["Rn"];
var arElemIndex = replacements["Ar"];
var yCount = 0;
var rnCount = 0;
var arCount = 0;
foreach (var 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;
}
}