/
Program.cs
364 lines (331 loc) · 15.3 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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace aocd4
{
internal class Program
{
static class AocD4
{
/* ------------------------------------------------------------- */
//Get matrix from text file and convert it in 2D Array
public static int[,] AcquireMatrix(string s, int lenght)
{
string[] arrayStrings = s.Split('\n');
for (int a = 0; a < arrayStrings.Length; a++)
//riduce spazi duplici in singoli spazi
{
arrayStrings[a] = arrayStrings[a].Replace(" ", " ");
}
int[,] result = new int[arrayStrings.Length, lenght];
int i = 0, j;
foreach (var row in arrayStrings)
{
j = 0;
foreach (var col in row.Trim().Split(' '))
{
if (col == "") break;
result[i, j] = int.Parse(col.Trim('\r'));
j++;
}
i++;
}
return result;
}
/* ------------------------------------------------------------- */
//Print a matrix
public static void PrintMatrix(int[,] mat)
{
Console.WriteLine("\n");
for (int i = 0; i < mat.GetLength(0); i++)
{
for (int j = 0; j < mat.GetLength(1); j++)
{
Console.Write(" " + mat[i, j]);
}
Console.WriteLine("");
}
}
/* ------------------------------------------------------------- */
//Sum digits contained in a matrix
public static int SumMatrixDigits(int[,] mat, int row, int col)
{
int sum = 0;
for (int i = 0; i < row; i++)
for (int j = 0; j < col; j++)
sum += mat[i, j];
return sum;
}
/* ------------------------------------------------------------- */
//Split single matrix to square matrix of row/col
public static List<int[,]> SplitSquareMatrix(int[,] mat, int row)
{
List<int[,]> result = new List<int[,]>();
int tempcont = 5;
int a = 0;
int[,] tempmatrix = new int[row, row];
for (int i = 0; i < mat.GetLength(0); i++)
{
if (i == tempcont)
{
result.Add((int[,])tempmatrix.Clone()); //per evitare il reference problem, è necessario copiare la matrice
a = 0;
continue;
}
for (int j = 0; j < mat.GetLength(1); j++)
{
tempmatrix[a, j] = mat[i, j];
}
a++;
if (i > tempcont) tempcont += 6;
}
return result;
}
/* ------------------------------------------------------------- */
//add number of row to matrix (last row) with value to fill
public static int[,] AddRowToMatrix(int[,] matrix, int rowToAdd, int valueToFill)
{
int row = matrix.GetLength(0) + rowToAdd;
int[,] result = new int[row, matrix.GetLength(1)];
/* First copy the original matrix to preserve it */
for (int r = 0; r < matrix.GetLength(0); r++)
{
for (int c = 0; c < matrix.GetLength(1); c++)
{
result[r, c] = matrix[r, c];
}
}
/*Insert Values into Main Matrix
--------------------------------------------------------------------------------*/
for (int c = 0; c < matrix.GetLength(1); c++)
{
result[row - 1, c] = valueToFill;
}
return result;
}
/* ------------------------------------------------------------- */
//add number of col to matrix (last col) with value to fill
public static int[,] AddColToMatrix(int[,] matrix, int colToAdd, int valueToFill)
{
int col = matrix.GetLength(1) + colToAdd;
int[,] result = new int[matrix.GetLength(0), col];
/* First copy the original matrix to preserve it*/
for (int r = 0; r < matrix.GetLength(0); r++)
{
for (int c = 0; c < matrix.GetLength(1); c++)
{
result[r, c] = matrix[r, c];
}
}
/* Then Insert Values into Main Matrix
--------------------------------------------------------------------------------*/
for (int r = 0; r < matrix.GetLength(0); r++)
{
result[r, col - 1] = valueToFill;
}
return result;
}
/* ------------------------------------------------------------- */
//convert string of numbers with comma -> in list
public static List<int> GetNumbersInList(string s)
{
List<int> list = new List<int>();
var a = s.Split(',').ToList();
for (int i = 0; i < a.Count; i++)
{
list.Add(int.Parse(a[i].ToString()));
}
return list;
}
/* ------------------------------------------------------------- */
//print a list
public static void PrintList(List<int> list)
{
foreach (var item in list)
{
Console.Write(" " + item);
}
}
/* ------------------------------------------------------------- */
// search matrix for containing number
public static bool MatrixContainsNumber(int[,] matrix, int number)
{
bool res = false;
for (int r = 0; r < matrix.GetLength(0); r++)
{
for (int c = 0; c < matrix.GetLength(1); c++)
{
if (matrix[r, c] == number)
{
res = true;
break;
}
}
if (res == true) break;
}
return res;
}
/* ------------------------------------------------------------- */
// get row of an item found
public static int GetRowOfItem(int[,] matrix, int item)
{
int row = 0;
for (int r = 0; r < matrix.GetLength(0); r++)
{
for (int c = 0; c < matrix.GetLength(1); c++)
{
if (matrix[r, c] == item)
{
row = r; break;
}
}
}
return row;
}
/* ------------------------------------------------------------- */
// get col of an item found
public static int GetColOfItem(int[,] matrix, int item)
{
int col = 0;
for (int r = 0; r < matrix.GetLength(0); r++)
{
for (int c = 0; c < matrix.GetLength(1); c++)
{
if (matrix[r, c] == item)
{
col = c; break;
}
}
}
return col;
}
/* ------------------------------------------------------------- */
//for every item found, decrease the last row/col
public static void CountItemFoundInMatrix(int[,] matrix, int row, int col)
{
matrix[row, 5]--;
matrix[5, col]--;
}
/* ------------------------------------------------------------- */
//Verify if last row or col is -6 for declaring winner matrix
public static bool MatrixIsWinner(int[,] matrix)
{
bool winner = false;
for (int r = 0; r < matrix.GetLength(0); r++)
{
if (matrix[r, 5] == -6)
{
winner = true; break;
}
for (int c = 0; c < matrix.GetLength(1); c++)
{
if (matrix[5, c] == -6)
{
winner = true; break;
}
}
}
return winner;
}
/* ------------------------------------------------------------- */
// ---- check last row / col lass than --> costruire funzione apposita
public static void ResetMatrixLastRowCol (int[,] matrix)
{
for (int r = 0; r < matrix.GetLength(0); r++)
{
matrix[r, 5] = -1;
for (int c = 0; c < matrix.GetLength(1); c++)
matrix[5, c] = -1;
}
}
/* ------------------------------------------------------------- */
}
static void Main(string[] args)
{
/* -------------------SETUP -------------------------- */
string matrixstring = File.ReadAllText(@"c:\input2.txt");
//read all the file and convert in string
string numbersextracted = File.ReadAllText(@"c:\input-numbers2.txt");
//read all the numbers extracted
int[,] res = AocD4.AcquireMatrix(matrixstring, 5);
//convert string in one maxi matrix
List<int[,]> listOfMatrix = AocD4.SplitSquareMatrix(res, 5);
//split maxi matrix in list of matrices by 5 row and col
List<int> listOfNumbers = AocD4.GetNumbersInList(numbersextracted);
//convert string of extracted numbers in list
/* ----------------------------------------------------- */
//adding -1 to all matrix splitted to track extracted num on row and col
for (int i = 0; i < listOfMatrix.Count; i++)
{
listOfMatrix[i] = AocD4.AddRowToMatrix(listOfMatrix[i], 1, -1);
listOfMatrix[i] = AocD4.AddColToMatrix(listOfMatrix[i], 1, -1);
}
/* -------------------END SETUP ------------------------ */
List<int> extractedNums = new List<int>();
//containing all the extracted numbers
List<int> extractedInWinnerMatrix = new List<int>();
List<int[,]> winnerMatrix = new List<int[,]>();
//containing extracted numbers in the winner matrix
Console.WriteLine("---------- ADVENT OF CODE ------------ ");
Console.WriteLine("---------- DAY 4 // PART 2 ------------ ");
Console.WriteLine("Press a key when ready to process input data");
Console.ReadKey();
int j;
var lastWinnerMatrix = new int[6, 6];
foreach (var item in listOfNumbers) //loop over all numbers extracted
{
for (j = 0; j < listOfMatrix.Count; j++) //loop over all matrix
{
Console.WriteLine($"\nChecking number... {item} in matrix nr. {j} \n");
if (AocD4.MatrixContainsNumber(listOfMatrix[j], item)) //check matrix for item
{
Console.WriteLine($"\n------------------------------------------\n" +
$"Found number {item} in matrix number {j} !" +
$"\n------------------------------------------\n");
AocD4.CountItemFoundInMatrix(listOfMatrix[j],
AocD4.GetRowOfItem(listOfMatrix[j], item),
AocD4.GetColOfItem(listOfMatrix[j], item)); var t = listOfMatrix[j];
if ( (AocD4.MatrixIsWinner(listOfMatrix[j])) && (!winnerMatrix.Contains(listOfMatrix[j])) )
//check if matrix is winner and itsn't duplicated
{
Console.WriteLine($"Adding winner matrix {j} to list of winner matrix");
winnerMatrix.Add(listOfMatrix[j]); //list of winner matrix
}
}
}
}
Console.WriteLine($"\n---------------------------\nWe have last winner matrix\n---------------------------\n ");
lastWinnerMatrix = winnerMatrix.Last();
AocD4.PrintMatrix(lastWinnerMatrix);
AocD4.ResetMatrixLastRowCol(lastWinnerMatrix); //reset last row/col for calculate final result
AocD4.PrintMatrix(lastWinnerMatrix); //matrix after reset of last row/col
var sumExtracted = 0; //value of all the numbers extracted
foreach (var item in listOfNumbers) //loop for check all the extracted numbers
{
if (AocD4.MatrixContainsNumber(lastWinnerMatrix, item)) //check matrix for item
{
//Console.WriteLine($"\nNumber {item} is contained in winner matrix");
sumExtracted += item;
AocD4.CountItemFoundInMatrix(lastWinnerMatrix,
AocD4.GetRowOfItem(lastWinnerMatrix, item),
AocD4.GetColOfItem(lastWinnerMatrix, item));
//this is for calculate how many numbers are extracted in a row/col
if (AocD4.MatrixIsWinner(lastWinnerMatrix))
{
Console.WriteLine($"\n---------------------------\nW) Winner number: {item}");
Console.WriteLine($"A) Sum of all numbers extracted in winner matrix: {sumExtracted}");
var unmarked = AocD4.SumMatrixDigits(lastWinnerMatrix, 5, 5) - sumExtracted;
Console.WriteLine($"B) Sum of all digits in winner matrix: {AocD4.SumMatrixDigits(lastWinnerMatrix, 5, 5)}");
Console.WriteLine($"C) Unmarked numbers: B) - A) = {unmarked}");
Console.WriteLine($"D) Final result: C) * W) = {unmarked*item}");
break;
}
}
}
Console.ReadKey();
}
}
}