-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
281 lines (260 loc) · 9.5 KB
/
Program.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace MatchSolver
{
enum DigitType
{
Number, Symbol
}
public static class MatchDigitsStore
{
public static List<List<bool>> store = new List<List<bool>>
{
new List<bool> { true, true, true, false, true, true, true },
new List<bool> { false, false, true, false, false, true, false },
new List<bool> { true, false, true, true, true, false, true },
new List<bool> { true, false, true, true, false, true, true },
new List<bool> { false, true, true, true, false, true, false },
new List<bool> { true, true, false, true, false, true, true },
new List<bool> { true, true, false, true, true, true, true },
new List<bool> { true, false, true, false, false, true, false },
new List<bool> { true, true, true, true, true, true, true },
new List<bool> { true, true, true, true, false, true, true }
};
};
class MatchDigit
{
public MatchDigit(int digit)
{
MatchPositions = new List<bool>(MatchDigitsStore.store[digit].ToArray());
DigitType = DigitType.Number;
}
public MatchDigit(char symbol)
{
try
{
MatchPositions = symbol switch
{
'-' => new List<bool> { true, false, false },
'=' => new List<bool> { true, true, false },
'+' => new List<bool> { true, false, true },
_ => throw new InvalidOperationException("Wrong symbol found while processing equation: " + symbol),
};
}
catch (InvalidOperationException ex)
{
Console.WriteLine(ex.Message);
Environment.Exit(-1);
}
DigitType = DigitType.Symbol;
}
public DigitType DigitType { get; set; }
public List<bool> MatchPositions { get; set; }
public int ToInteger()
{
if (DigitType == DigitType.Symbol)
{
throw new InvalidOperationException("Cannot get integer value for a symbol.");
}
foreach (List<bool> digitPositions in MatchDigitsStore.store)
{
if (MatchPositions.SequenceEqual(digitPositions))
{
return MatchDigitsStore.store.IndexOf(digitPositions);
}
}
return -1;
}
public char ToSymbol()
{
if (DigitType == DigitType.Number)
{
throw new InvalidOperationException("Cannot get char value for a number.");
}
if (MatchPositions.SequenceEqual(new List<bool> { true, false, false }))
{
return '-';
}
else if (MatchPositions.SequenceEqual(new List<bool> { true, true, false }))
{
return '=';
}
else if (MatchPositions.SequenceEqual(new List<bool> { true, false, true }))
{
return '+';
}
else
{
return 'i';
}
}
public override string ToString()
{
if (DigitType == DigitType.Number)
{
return ToInteger().ToString();
}
else
{
if (ToSymbol() == 'i')
{
return "-1";
}
else
{
return ToSymbol().ToString();
}
}
}
internal bool IsValid()
{
if (DigitType == DigitType.Number)
{
return ToInteger() != -1;
}
else
{
return ToSymbol() != 'i';
}
}
}
class Program
{
static string inputEquation = "";
static bool verbose = false;
static void Main(string[] args)
{
if (args.Length == 0)
{
Console.WriteLine("Usage: MathSolver <equation> [--verbose]");
Environment.Exit(-1);
}
else
{
var argsList = args.ToList();
if (argsList.Contains("--verbose"))
{
verbose = true;
argsList.Remove("--verbose");
}
inputEquation = argsList.FirstOrDefault();
}
if (String.IsNullOrWhiteSpace(inputEquation))
{
Console.WriteLine("An equation is required.");
Environment.Exit(-1);
}
List<MatchDigit> origEquation = ConstructEquation(inputEquation);
for (int firstDigitIndex = 0; firstDigitIndex < origEquation.Count; firstDigitIndex++)
{
MatchDigit matchDigitToRemoveFrom = origEquation[firstDigitIndex];
for (int i = 0; i < matchDigitToRemoveFrom.MatchPositions.Count; i++)
{
if (matchDigitToRemoveFrom.MatchPositions[i] == true)
{
matchDigitToRemoveFrom.MatchPositions[i] = false;
for (int secondDigitIndex = 0; secondDigitIndex < origEquation.Count; secondDigitIndex++)
{
MatchDigit matchDigitToAddTo = origEquation[secondDigitIndex];
for (int j = 0; j < matchDigitToAddTo.MatchPositions.Count; j++)
{
if (i==j)
{
continue;
}
if (matchDigitToAddTo.MatchPositions[j] == false)
{
matchDigitToAddTo.MatchPositions[j] = true;
if (matchDigitToAddTo.IsValid() && matchDigitToRemoveFrom.IsValid())
{
if (verbose)
{
Console.WriteLine(PrintEquation(origEquation, firstDigitIndex, i, secondDigitIndex, j));
}
if (EvaluateEquation(origEquation))
{
Console.WriteLine("SOLVED: " + PrintEquation(origEquation, firstDigitIndex, i, secondDigitIndex, j));
}
}
matchDigitToAddTo.MatchPositions[j] = false;
}
}
}
matchDigitToRemoveFrom.MatchPositions[i] = true;
}
}
}
Console.WriteLine("Done.");
}
private static List<MatchDigit> ConstructEquation(string inputEquation)
{
List<MatchDigit> equation = new List<MatchDigit>(inputEquation.Length);
foreach (char character in inputEquation)
{
if (Char.IsDigit(character))
{
equation.Add(new MatchDigit(Convert.ToInt32(character.ToString())));
}
else
{
equation.Add(new MatchDigit(character));
}
}
return equation;
}
static bool EvaluateEquation(List<MatchDigit> origEquation)
{
List<int> equationSubs = new List<int>();
int currentValue = 0;
char currentSymbol = '+';
for (int i = 0; i < origEquation.Count; i++)
{
MatchDigit currentDigit = origEquation[i];
if (currentDigit.DigitType == DigitType.Number)
{
if (currentSymbol == '+')
{
currentValue += currentDigit.ToInteger();
}
else if (currentSymbol == '-')
{
currentValue -= currentDigit.ToInteger();
}
else
{
equationSubs.Add(currentValue);
currentValue = currentDigit.ToInteger();
}
}
else
{
currentSymbol = currentDigit.ToSymbol();
}
}
equationSubs.Add(currentValue);
if (equationSubs.Count < 2)
{
return false;
}
foreach (int subExpValue in equationSubs)
{
if (subExpValue != equationSubs.First())
{
return false;
}
}
return true;
}
static string PrintEquation(List<MatchDigit> equation, int fromDigit, int fromPos, int toDigit, int toPos)
{
StringBuilder sb = new StringBuilder($"Moved from {fromDigit} position {fromPos} to {toDigit} position {toPos} : ");
foreach (MatchDigit digit in equation)
{
sb.Append(digit);
}
return sb.ToString();
}
}
}