-
Notifications
You must be signed in to change notification settings - Fork 0
/
Utils.cs
117 lines (98 loc) · 3.55 KB
/
Utils.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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
namespace AdventOfCode2018
{
static class Utils
{
public static IEnumerable<T> flatten<T>(this T[,] array)
{
for (int i = 0; i < array.Length; i++)
{
yield return array[i % array.GetLength(0), i / array.GetLength(0)];
}
}
public static IEnumerable<string> splitLines(string input)
{
return input.Replace("\r", "").Split('\n').Select(l => l.Trim());
}
public static IEnumerable<string> splitLinesWithoutTrim(string input)
{
return input.Replace("\r", "").Split('\n');
}
public static bool Test(Func<string, dynamic, string> method, string input, string output, dynamic options = null)
{
return Test(method, new string[] { input }, new string[] { output }, options);
}
public static bool Test(Func<string, dynamic, string> method, dynamic[] inputs, string[] outputs, dynamic options = null)
{
var success = true;
for (int i = 0; i < inputs.Length; i++)
{
var actual = method(inputs[i], options);
if (actual == outputs[i])
{
Write("OK: ", ConsoleColor.Green);
}
else
{
Write("WRONG: ", ConsoleColor.Red);
success = false;
}
var input = inputs[i].ToString().Replace("\n", " ").Replace("\r", "");
if (input.Length > 40)
{
input = input.Substring(0, 40) + "...";
}
Write(input + " -> ", ConsoleColor.White);
if (actual != outputs[i])
{
WriteLine(actual, ConsoleColor.Red);
WriteLine(" Should be " + outputs[i], ConsoleColor.Red);
}
else
{
WriteLine(actual, ConsoleColor.Green);
}
}
return success;
}
static MD5 md5 = System.Security.Cryptography.MD5.Create();
public static string MD5(string input)
{
return BitConverter.ToString(md5.ComputeHash(Encoding.ASCII.GetBytes(input))).Replace("-", "").ToLower();
}
public static void WriteLine(string msg, ConsoleColor color)
{
Write(msg, color);
Console.WriteLine();
}
public static void Write(string msg, ConsoleColor color)
{
var old = Console.ForegroundColor;
Console.ForegroundColor = color;
Console.Write(msg);
Console.ForegroundColor = old;
}
public static void ClearLine()
{
Console.Write(new string(' ', Console.WindowWidth));
Console.CursorLeft = 0;
}
public static void WriteTransient(string msg)
{
Console.Write(msg);
Console.CursorLeft = 0;
}
public static void DumpToFile(StringBuilder sb)
{
string temp = System.IO.Path.GetTempFileName().Replace(".tmp", ".txt");
System.IO.File.WriteAllText(temp, sb.ToString());
Process.Start(temp);
}
}
}