-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day9.cs
190 lines (158 loc) · 5.85 KB
/
Day9.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
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using AoC.Infrastructure;
using NUnit.Framework;
namespace AoC
{
[TestFixture]
public class Day9 : Challenge
{
private Compressor _compressor;
[SetUp]
public void Setup()
{
_compressor = new Compressor();
}
[TestCase("ADVENT", "ADVENT")]
[TestCase("A(1x5)BC", "ABBBBBC")]
[TestCase("(3x3)XYZ", "XYZXYZXYZ")]
[TestCase("A(2x2)BCD(2x2)EFG", "ABCBCDEFEFG")]
[TestCase("X(8x2)(3x3)ABCY", "X(3x3)ABC(3x3)ABCY")]
public void Sample(string compressed, string decompressed)
{
var result = _compressor.Decompress(compressed);
Assert.That(result.Decompressed, Is.EqualTo(decompressed));
}
[Test]
public void Test()
{
var result = _compressor.Decompress(string.Join("", Input));
Assert.That(result.Decompressed.Length, Is.EqualTo(74532));
}
[Test]
public void Test2()
{
var result = _compressor.PredictLength2(string.Join("", Input));
Assert.That(result, Is.EqualTo(11558231665));
}
[TestCase("XYZ", 3)]
[TestCase("(3x3)XYZ", 9)]
[TestCase("X(8x2)(3x3)ABCY", 20)]
[TestCase("(27x12)(20x12)(13x14)(7x10)(1x12)A", 241920)]
[TestCase("(25x3)(3x3)ABC(2x3)XY(5x2)PQRSTX(18x9)(3x2)TWO(5x7)SEVEN", 445)]
public void Test2(string compressed, int length)
{
var lengthPrediction = _compressor.PredictLength2(compressed);
Assert.That(lengthPrediction, Is.EqualTo(length));
}
}
public class Compressor
{
public Body Decompress(string compressed)
{
var buffer = new StringBuilder();
for (var offset = 0; offset < compressed.Length; offset++)
{
var printDuration = 1;
var sequenceBuffer = new StringBuilder();
sequenceBuffer.Append(compressed[offset]);
if (sequenceBuffer.ToString() == "(")
{
var next = "";
while (next != ")")
{
offset++;
next = compressed[offset].ToString();
sequenceBuffer.Append(next);
}
}
var expanderRegex = new Regex(@"\(([0-9]+)x([0-9]+)\)");
if (expanderRegex.IsMatch(sequenceBuffer.ToString()))
{
var matches = expanderRegex.Matches(sequenceBuffer.ToString())[0];
var sequenceLength = int.Parse(matches.Groups[1].Value);
printDuration = int.Parse(matches.Groups[2].Value);
sequenceBuffer.Clear();
for (var i = 0; i < sequenceLength; i++)
{
offset++;
sequenceBuffer.Append(compressed[offset]);
}
}
for (var i = 0; i < printDuration; i++)
{
buffer.Append(sequenceBuffer);
}
}
return new Body(compressed, buffer.ToString());
}
public long PredictLength2(string compressed)
{
var stackOfCharacters = new Stack<char>(compressed.Reverse());
return CountSequence(stackOfCharacters);
}
private static long CountSequence(Stack<char> stackOfCharacters, int? maxCapture = null)
{
long count = 0;
var iterations = 0;
var maxCharsToCount = maxCapture.GetValueOrDefault(int.MaxValue);
while (stackOfCharacters.Any() && maxCharsToCount > iterations++)
{
var ch = stackOfCharacters.Pop();
if (ch != '(')
{
count++;
continue;
}
var capture = new ExpressionToken(CaptureExpression(stackOfCharacters));
iterations += capture.ValueLength - 1;
var multiplicationFactor = capture.MultiplyBy;
var captureCharacters = capture.SequenceLength;
var countSequence = CountSequence(stackOfCharacters, captureCharacters);
iterations += captureCharacters;
var expandedCount = countSequence*multiplicationFactor;
count += expandedCount;
}
return count;
}
private static string CaptureExpression(Stack<char> stackOfCharacters)
{
var op = new StringBuilder();
while (!op.ToString().EndsWith(")") && stackOfCharacters.Any())
{
op.Append(stackOfCharacters.Pop());
}
return op.ToString().Remove(op.ToString().Length - 1);
}
}
public class ExpressionToken
{
public string Value { get; set; }
public int SequenceLength { get; }
public int MultiplyBy { get; }
public int ValueLength => Value.Length + 2;
public ExpressionToken(string value)
{
Value = value;
var expanderRegex = new Regex(@"([0-9]+)x([0-9]+)");
if (expanderRegex.IsMatch(value))
{
var matches = expanderRegex.Matches(value)[0];
SequenceLength = int.Parse(matches.Groups[1].Value);
MultiplyBy = int.Parse(matches.Groups[2].Value);
}
}
}
public class Body
{
public string Compressed { get; set; }
public string Decompressed { get; set; }
public Body(string compressed, string decompressed)
{
Compressed = compressed;
Decompressed = decompressed;
}
}
}