-
Notifications
You must be signed in to change notification settings - Fork 2
/
Form1.cs
284 lines (258 loc) · 12.2 KB
/
Form1.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
282
283
284
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GenericParsing;
namespace Unbalanced
{
public partial class Form1 : Form
{
private static int callsCount;
private static Decimal maxAbsDiff;
private static List<String> maxAbsDiffOps;
public Form1()
{
InitializeComponent();
}
/// <summary>
/// Recursively run through every single way to add/subtract the numbers together, producing a List describing the different
/// possible balances, and the operations conducted to get said balance. Brute-force, with the sole enhancement being that if
/// one of the numbers is Zero, then instead of branching down both the Add and Subtract paths, will go down a single
/// "Plus-Minus" path, bringing this algorithm to 2^(n - [Count of Accounts with Zero balance])
///
/// </summary>
/// <param name="SumsAndOps">List of Tuples, where each Tuple represents a Sum and the Operations conducted to obtain the sum.</param>
/// <param name="AbsSumsAndOps">As for SumsAndOps, but the Tuple has an additional item (item 1) which is the absolute value of the Sum</param>
/// <param name="DecimalsToSum">The numbers we are attempting to balance to Zero.</param>
/// <param name="oppsSoFar">The operations conducted so far.</param>
/// <param name="sumSoFar">The Sum of all operations so far.</param>
/// <param name="count">How many of the numbers we're attempting to balance that we've got through so far.</param>
/// <param name="stop">At what point the recursive method stops (this should be set to the sidze of the DecimalsToSum list).</param>
private static void BuildPathsAndSumsTogether (List<Tuple<Decimal,List<String>>> SumsAndOps, List<Tuple<Decimal,Decimal,List<String>>> AbsSumsAndOps, List<Decimal> DecimalsToSum, List<String> oppsSoFar, Decimal sumSoFar, int count, int stop)
{
callsCount++;
if (count < stop)
{
Decimal amount = DecimalsToSum[count];
if(amount == 0){
oppsSoFar.Add("+-");
sumSoFar += 0;
BuildPathsAndSumsTogether(SumsAndOps, AbsSumsAndOps, DecimalsToSum, new List<String>(oppsSoFar), sumSoFar, count + 1, stop);
}
else {
oppsSoFar.Add("+");
sumSoFar += amount;
BuildPathsAndSumsTogether(SumsAndOps, AbsSumsAndOps, DecimalsToSum, new List<String>(oppsSoFar), sumSoFar, count + 1, stop);
oppsSoFar.RemoveAt(count);
oppsSoFar.Add("-");
sumSoFar -= (amount*2);
BuildPathsAndSumsTogether(SumsAndOps, AbsSumsAndOps, DecimalsToSum, new List<String>(oppsSoFar), sumSoFar, count + 1, stop);
}
}
else
{
SumsAndOps.Add(new Tuple<decimal, List<string>>(sumSoFar,oppsSoFar));
AbsSumsAndOps.Add(new Tuple<decimal, decimal, List<string>>(Math.Abs(sumSoFar), sumSoFar, oppsSoFar));
}
}
/// <summary>
/// Recursively run through every single way to add/subtract the numbers together, producing a List describing the different
/// possible balances, and the operations conducted to get said balance. Brute-force, with the sole enhancement being that if
/// one of the numbers is Zero, then instead of branching down both the Add and Subtract paths, will go down a single
/// "Plus-Minus" path, bringing this algorithm to 2^(n - [Count of Accounts with Zero balance])
///
/// </summary>
/// <param name="SumsAndOps">List of Tuples, where each Tuple represents a Sum and the Operations conducted to obtain the sum.</param>
/// <param name="AbsSumsAndOps">As for SumsAndOps, but the Tuple has an additional item (item 1) which is the absolute value of the Sum</param>
/// <param name="DecimalsToSum">The numbers we are attempting to balance to Zero.</param>
/// <param name="oppsSoFar">The operations conducted so far.</param>
/// <param name="sumSoFar">The Sum of all operations so far.</param>
/// <param name="count">How many of the numbers we're attempting to balance that we've got through so far.</param>
/// <param name="stop">At what point the recursive method stops (this should be set to the sidze of the DecimalsToSum list).</param>
private static void BuildPathsAndSumsTogetherMinimal (List<Decimal> DecimalsToSum, List<String> oppsSoFar, Decimal sumSoFar, int count, int stop)
{
callsCount++;
if (count < stop)
{
Decimal amount = DecimalsToSum[count];
if(amount == 0){
oppsSoFar.Add("+-");
sumSoFar += 0;
BuildPathsAndSumsTogetherMinimal( DecimalsToSum, new List<String>(oppsSoFar), sumSoFar, count + 1, stop);
}
else {
oppsSoFar.Add("+");
sumSoFar += amount;
BuildPathsAndSumsTogetherMinimal( DecimalsToSum, new List<String>(oppsSoFar), sumSoFar, count + 1, stop);
oppsSoFar.RemoveAt(count);
oppsSoFar.Add("-");
sumSoFar -= (amount*2);
BuildPathsAndSumsTogetherMinimal( DecimalsToSum, new List<String>(oppsSoFar), sumSoFar, count + 1, stop);
}
}
else
{
if(maxAbsDiff == -9999) {
maxAbsDiff = Math.Abs(sumSoFar);
maxAbsDiffOps = oppsSoFar;
}
else if(maxAbsDiff > Math.Abs(sumSoFar)) {
maxAbsDiff = Math.Abs(sumSoFar);
maxAbsDiffOps = oppsSoFar;
}
//SumsAndOps.Add(new Tuple<decimal, List<string>>(sumSoFar,oppsSoFar));
}
}
/// <summary>
/// Recursively run through every single way to add/subtract the numbers together, producing a List describing the different
/// possible balances, and the operations conducted to get said balance. Brute-force, with the sole enhancement being that if
/// one of the numbers is Zero, then instead of branching down both the Add and Subtract paths, will go down a single
/// "Plus-Minus" path, bringing this algorithm to 2^(n - [Count of Accounts with Zero balance])
///
/// </summary>
/// <param name="SumsAndOps">List of Tuples, where each Tuple represents a Sum and the Operations conducted to obtain the sum.</param>
/// <param name="AbsSumsAndOps">As for SumsAndOps, but the Tuple has an additional item (item 1) which is the absolute value of the Sum</param>
/// <param name="DecimalsToSum">The numbers we are attempting to balance to Zero.</param>
/// <param name="oppsSoFar">The operations conducted so far.</param>
/// <param name="sumSoFar">The Sum of all operations so far.</param>
/// <param name="count">How many of the numbers we're attempting to balance that we've got through so far.</param>
/// <param name="stop">At what point the recursive method stops (this should be set to the sidze of the DecimalsToSum list).</param>
private static void BuildPathsAndSumsTogetherNoAbs(List<Tuple<Decimal,List<String>>> SumsAndOps, List<Decimal> DecimalsToSum, List<String> oppsSoFar, Decimal sumSoFar, int count, int stop)
{
callsCount++;
if (count < stop)
{
Decimal amount = DecimalsToSum[count];
if(amount == 0){
oppsSoFar.Add("+-");
sumSoFar += 0;
BuildPathsAndSumsTogetherNoAbs(SumsAndOps, DecimalsToSum, new List<String>(oppsSoFar), sumSoFar, count + 1, stop);
}
else {
oppsSoFar.Add("+");
sumSoFar += amount;
BuildPathsAndSumsTogetherNoAbs(SumsAndOps, DecimalsToSum, new List<String>(oppsSoFar), sumSoFar, count + 1, stop);
oppsSoFar.RemoveAt(count);
oppsSoFar.Add("-");
sumSoFar -= (amount*2);
BuildPathsAndSumsTogetherNoAbs(SumsAndOps, DecimalsToSum, new List<String>(oppsSoFar), sumSoFar, count + 1, stop);
}
}
else
{
SumsAndOps.Add(new Tuple<decimal, List<string>>(sumSoFar,oppsSoFar));
}
}
private void btn1_Click(object sender, EventArgs e)
{
DialogResult result = ofd1.ShowDialog();
}
private void btn2_Click(object sender, EventArgs e)
{
//ArgumentParser argParser = new ArgumentParser(args);
String dataSource = ofd1.FileName;//argParser["o"];
if (dataSource != null)
{
try
{
GenericParser parser = new GenericParser();
parser.SetDataSource(dataSource);
parser.FirstRowHasHeader = true;
parser.Read();
int columnCount = parser.ColumnCount;
//Work out the names of the Accounts we need to sum
List<string> ItemNames = new List<string>();
for (int i = 0; i < columnCount; i++)
{
ItemNames.Add (parser.GetColumnName(i));
}
// //Work out the numbers we need to sum
// List<Decimal> DecimalsToSum = new List<decimal>();
// for (int decimalCounter = 0; decimalCounter < columnCount; decimalCounter++)
// {
// DecimalsToSum.Add(Decimal.Parse(parser[decimalCounter]));
// }
//
// List<Tuple<Decimal,List<String>>> SumsAndOps = new List<Tuple<Decimal,List<String>>>();
// List<Tuple<Decimal,Decimal,List<String>>> AbsSumsAndOps = new List<Tuple<Decimal,Decimal,List<String>>>();
// List<String> OpsSoFar = new List<string>();
//
// callsCount = 0;
// BuildPathsAndSumsTogether (SumsAndOps, AbsSumsAndOps, DecimalsToSum, OpsSoFar, 0, 0, columnCount);
// SumsAndOps.Sort( (a,b) => a.Item1.CompareTo(b.Item1) );
// AbsSumsAndOps.Sort( (a,b) => a.Item1.CompareTo(b.Item1) );
//Work out the numbers we need to sum
List<Decimal> DecimalsToSum = new List<decimal>();
for (int decimalCounter = 0; decimalCounter < columnCount; decimalCounter++)
{
DecimalsToSum.Add(Decimal.Parse(parser[decimalCounter]));
}
//List<Tuple<Decimal,List<String>>> SumsAndOps = new List<Tuple<Decimal,List<String>>>();
List<String> OpsSoFar = new List<string>();
callsCount = 0;
maxAbsDiff = -9999;
BuildPathsAndSumsTogetherMinimal(DecimalsToSum, OpsSoFar, 0, 0, columnCount);
String stuff = "yarr";
Decimal outcome = maxAbsDiff;
textBox1.Text += "Balances To" +":"+ maxAbsDiff + Environment.NewLine;
for(int i = 0; i < DecimalsToSum.Count; i++){
textBox1.Text += maxAbsDiffOps[i] + "," + DecimalsToSum[i] + Environment.NewLine;
}
//BuildPathsAndSumsTogetherNoAbs (SumsAndOps, DecimalsToSum, OpsSoFar, 0, 0, columnCount);(SumsAndOps, DecimalsToSum, OpsSoFar, 0, 0, columnCount);
// SumsAndOps.Sort( (a,b) => a.Item1.CompareTo(b.Item1) );
// int countSuccess = 0;
// textBox1.Text = "";
// foreach(Tuple<Decimal,List<string>> sumWithOps in SumsAndOps) {
// if(sumWithOps.Item1 == 0) {
// countSuccess++;
//
// for(int account = 0; account < ItemNames.Count; account++) {
// textBox1.Text += (ItemNames[account].PadLeft(30) + ":"
// + DecimalsToSum[account].ToString().PadLeft(15).PadRight(4)
// + "(" + sumWithOps.Item2[account] + ")"
// + Environment.NewLine);
// Console.WriteLine(ItemNames[account]+":"+DecimalsToSum[account]+":"+sumWithOps.Item2[account]);
// }
// textBox1.Text += ("Balances To".PadLeft(30) + ":"
// + sumWithOps.Item1.ToString().PadLeft(15)
// + Environment.NewLine + Environment.NewLine);
// Console.WriteLine("Balances To" +":"+ sumWithOps.Item1);
// Console.WriteLine();
// }
// }
//
// if(countSuccess == 0) {
// int n = 20;
//
//// textBox1.Text = "This combination of numbers has no possible way to balance. The " + n + " solutions closest to 0 follow." + Environment.NewLine;
////
//// for(int closeResultPos = 0 ; closeResultPos < n || closeResultPos >= AbsSumsAndOps.Count; closeResultPos++) {
//// Tuple<Decimal,Decimal,List<string>> sumWithOps = AbsSumsAndOps[closeResultPos];
//// for(int account = 0; account < ItemNames.Count; account++) {
//// textBox1.Text += (ItemNames[account].PadLeft(30) + ":"
//// + DecimalsToSum[account].ToString().PadLeft(15).PadRight(4)
//// + "(" + sumWithOps.Item3[account] + ")"
//// + Environment.NewLine);
//// Console.WriteLine(ItemNames[account]+":"+DecimalsToSum[account]+":"+sumWithOps.Item3[account]);
//// }
////
//// textBox1.Text += ("Balances To".PadLeft(30) + ":"
//// + sumWithOps.Item2.ToString().PadLeft(15)
//// + Environment.NewLine + Environment.NewLine);
//// Console.WriteLine("Balances To" +":"+ sumWithOps.Item2);
//// Console.WriteLine();
//// }
// }
}
catch (Exception ex)
{
textBox1.Text = ex.Message;
}
}
}
}
}