-
Notifications
You must be signed in to change notification settings - Fork 0
/
ExcerciseBase.cs
127 lines (96 loc) · 3.27 KB
/
ExcerciseBase.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
using System;
using System.IO;
using MathNet.Numerics.LinearAlgebra;
using MathNet.Numerics.LinearAlgebra.Complex;
namespace AdventOfCode2021
{
public class ExcerciseBase
{
public int Day { get; set; }
public int Part { get; set; }
protected void ReadFileLineByLine(string file, Action<string> action)
{
if (!File.Exists(file))
{
throw new FileNotFoundException("File not found");
}
StreamReader rdr = new StreamReader(file);
string line;
while ((line = rdr.ReadLine()) != null)
{
action.Invoke(line);
}
rdr.Close();
}
protected string ReadFullFile(string file)
{
if (!File.Exists(file))
{
throw new FileNotFoundException("File not found");
}
StreamReader rdr = new StreamReader(file);
string content = rdr.ReadToEnd().Trim();
rdr.Close();
return content;
}
protected Matrix<double> ReadMatrix(string file)
{
string input = ReadFullFile(file);
string[] lines = input.Split("\r\n");
int rows = lines.Length;
int cols = lines[0].Trim().Length;
Matrix<double> m = Matrix<double>.Build.Dense(rows, cols, -1);
for (int j = 0; j < rows; j++)
{
for (int k = 0; k < cols; k++)
{
m[j, k] = Convert.ToInt32(lines[j][k].ToString());
}
}
return m;
}
protected void PrintMatrix<T>(Matrix<T> matrix)
where T: struct, IFormattable, IEquatable<T>
{
//Console.WriteLine(matrix);
for (int r = 0; r < matrix.RowCount; r++)
{
for (int c = 0; c < matrix.ColumnCount; c++)
{
Console.Write(matrix[r, c].ToString("G0", System.Globalization.CultureInfo.InvariantCulture) + " ");
}
Console.WriteLine("");
}
//for (int x = 0; x < matrix.ColumnCount; x++)
//{
//}
}
protected void PrintMatrix<T>(Matrix<T> matrix, Dictionary<T, string> mapping)
where T: struct, IFormattable, IEquatable<T>
{
//Console.WriteLine(matrix);
for (int r = 0; r < matrix.RowCount; r++)
{
for (int c = 0; c < matrix.ColumnCount; c++)
{
var value = matrix[r, c];
if (mapping.ContainsKey(value))
{
Console.Write(mapping[value]);
}
else
{
Console.Write(value.ToString("G0", System.Globalization.CultureInfo.InvariantCulture));
}
}
Console.WriteLine("");
}
//for (int x = 0; x < matrix.ColumnCount; x++)
//{
//}
}
public virtual void Run()
{
}
}
}