-
Notifications
You must be signed in to change notification settings - Fork 0
/
3kyu_Help_the_general_decode_secret_enemy_messages.cs
100 lines (81 loc) · 3.87 KB
/
3kyu_Help_the_general_decode_secret_enemy_messages.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
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CodingChallenges;
[TestClass]
public class _3kyu_Help_the_general_decode_secret_enemy_messages {
[TestMethod]
public void Test() {
/*
Help the general decode secret enemy messages
General Patron is faced with a problem , his intelligence has intercepted some secret messages from the enemy but they are all encrypted. Those messages are crucial to getting the jump on the enemy and winning the war. Luckily intelligence also captured an encoding device as well. However even the smartest programmers weren't able to crack it though. So the general is asking you , his most odd but brilliant programmer.
You can call the encoder like this.
Console.WriteLine(Encoder.Encode("Hello World!"));
Our cryptoanalysts kept poking at it and found some interesting patterns.
Console.WriteLine(Encoder.Encode ("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"));
Console.WriteLine(Encoder.Encode ("bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"));
Console.WriteLine(Encoder.Encode ("!@#$%^&*()_+-"));
StringBuilder a = new StringBuilder();
StringBuilder b = new StringBuilder();
StringBuilder c = new StringBuilder();
foreach (char w in "abcdefghijklmnopqrstuvwxyz") {
a.Append(Encoder.Encode ( "" + w)[0]);
b.Append(Encoder.Encode ( "_" + w)[1]);
c.Append(Encoder.Encode ("__" + w)[2]);
}
Console.WriteLine(a);
Console.WriteLine(b);
Console.WriteLine(c);
We think if you keep on this trail you should be able to crack the code! You are expected to fill in the
public static string Decode(string p_what)
function. Good luck ! General Patron is counting on you!
*/
}
public static string Decode(string p_what) {
Dictionary<int, string> theKey = AcquireTheKeyTable();
string decodedString = string.Concat(p_what.Select((c, ind) =>
theKey[ind % 66].Contains(c) ? theKey[-1][theKey[ind % 66].IndexOf(c)] : (char)c));
return decodedString;
}
public static Dictionary<int, string> AcquireTheKeyTable() {
string alfabet = " " + string.Concat(
Enumerable.Range(44, 79)
.Where(n => n != 45 && n != 47 && !(n > 57 && n < 63) && !(n > 90 && n < 97))
.Select(n => (char)n)
);
List<string> encodedCharLines = new();
foreach (char ch in alfabet) {
string longStr = new string(ch, 66);
string encoded = Encoder.Encode(longStr);
encodedCharLines.Add(encoded);
}
Dictionary<int, string> dic = new();
dic.Add(-1, alfabet); //= decoded character
for (int ptr = 0; ptr < encodedCharLines.First().Length; ptr++) dic.Add(ptr, "");
for (int ptr = 0; ptr < encodedCharLines.First().Length; ptr++)
for (int i = 0; i < encodedCharLines.Count(); i++) dic[ptr] += encodedCharLines[i][ptr];
return dic;
}
// Someone else's solution that I chose after completion.
public static string Decode_v2(string p_what) {
var key = "abdhpF,82QsLirJejtNmzZKgnB3SwTyXG ?.6YIcflxVC5WE94UA1OoD70MkvRuPqH";
var response = new StringBuilder();
for (int i = 0; i < p_what.Length; i++) {
var idx = key.IndexOf(p_what[i]);
if (idx == -1) {
response.Append(p_what[i]);
continue;
}
var locator = (idx + 65 - i) % 66;
while (locator < 0) {
locator += 66;
}
response.Append(key[locator]);
}
return response.ToString();
}
}
static class Encoder {
// This method is proveded by the challenge author
public static string Encode(string str) => str;
}