Created
January 27, 2015 01:44
-
-
Save lardratboy/2963b1c48bf5d5d87a29 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace ironictest | |
{ | |
class Program | |
{ | |
const int KEYBOARD_COLS = 10; | |
const int KEYBOARD_ROWS = 4; | |
const int KEYS = KEYBOARD_ROWS * KEYBOARD_COLS; | |
static readonly char [] standard_key_layout = { | |
'1', '2', '3', '4', '5', '6', '7', '8', '9', '0', | |
'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', | |
'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';', | |
'z', 'x', 'c', 'v', 'b', 'n', 'm', ',', '.', '/' | |
}; | |
static readonly string [] transform_seperators = { "," }; | |
// -------------------------------------------------------------------------------- | |
#if DEBUG | |
static void DEBUG_LAYOUT( string header, char [] layout ) { | |
StringBuilder sb = new StringBuilder(header); | |
sb.Append("\n"); | |
int idx = 0; | |
for (int r = 0; r < KEYBOARD_ROWS; ++r) | |
{ | |
for (int c = 0; c < KEYBOARD_COLS; ++c) | |
{ | |
sb.AppendFormat("{0}, ", layout[idx++]); | |
} | |
sb.Append("\n"); | |
} | |
System.Diagnostics.Debug.WriteLine( sb ); | |
} | |
#endif | |
static char[] HorizontalTransform(char[] layout) | |
{ | |
#if DEBUG | |
System.Diagnostics.Debug.WriteLine("HT"); | |
#endif | |
char[] output = new char[layout.Length]; | |
int roffset = 0; | |
for (int r = 0; r < KEYBOARD_ROWS; ++r) | |
{ | |
for (int c = 0; c < KEYBOARD_COLS; ++c) | |
{ | |
int odx = (KEYBOARD_COLS - 1 - c); | |
output[roffset + odx] = layout[roffset + c]; | |
} | |
roffset += KEYBOARD_COLS; | |
} | |
#if DEBUG | |
DEBUG_LAYOUT("BEFORE", layout); | |
DEBUG_LAYOUT("AFTER", output); | |
#endif | |
return output; | |
} | |
static char[] VerticalTransform(char[] layout) | |
{ | |
#if DEBUG | |
System.Diagnostics.Debug.WriteLine("VT"); | |
#endif | |
char[] output = new char[layout.Length]; | |
int srcroffset = 0; | |
int dstroffset = (KEYBOARD_ROWS - 1) * KEYBOARD_COLS; | |
for (int r = 0; r < KEYBOARD_ROWS; ++r) | |
{ | |
for (int c = 0; c < KEYBOARD_COLS; ++c) | |
{ | |
int odx = (KEYBOARD_COLS - 1 - c); | |
output[dstroffset + c] = layout[srcroffset + c]; | |
} | |
srcroffset += KEYBOARD_COLS; | |
dstroffset -= KEYBOARD_COLS; | |
} | |
#if DEBUG | |
DEBUG_LAYOUT("BEFORE", layout); | |
DEBUG_LAYOUT("AFTER", output); | |
#endif | |
return output; | |
} | |
static char[] ShiftTransform(char[] layout, int shift) | |
{ | |
#if DEBUG | |
System.Diagnostics.Debug.WriteLine(string.Format("SHIFT {0}", shift)); | |
#endif | |
int boundedShift = ((layout.Length - shift) % layout.Length); | |
char[] output = new char[layout.Length]; | |
for (int i = 0; i < layout.Length; i++) | |
{ | |
output[i] = layout[(i + boundedShift) % layout.Length]; | |
} | |
#if DEBUG | |
DEBUG_LAYOUT("BEFORE", layout); | |
DEBUG_LAYOUT("AFTER", output); | |
#endif | |
return output; | |
} | |
// -------------------------------------------------------------------------------- | |
static Dictionary<char, char> GetTransformDictionary(string[] transforms) | |
{ | |
Dictionary<char, char> encodeDict = new Dictionary<char, char>(); | |
char[] transformed_keys_layout = (char [])standard_key_layout.Clone(); | |
foreach ( string t in transforms ) | |
{ | |
Console.WriteLine(t); | |
switch (t.ToLower()) | |
{ | |
case "h": | |
transformed_keys_layout = HorizontalTransform(transformed_keys_layout); | |
break; | |
case "v": | |
transformed_keys_layout = VerticalTransform(transformed_keys_layout); | |
break; | |
default: | |
{ | |
int shift = int.Parse(t); | |
transformed_keys_layout = ShiftTransform(transformed_keys_layout, shift); | |
} | |
break; | |
} | |
} | |
for ( int i = 0; i < standard_key_layout.Length; i++ ) | |
{ | |
char src = standard_key_layout[ i ]; | |
char dst = transformed_keys_layout[ i ]; | |
encodeDict[src] = dst; | |
} | |
return encodeDict; | |
} | |
// -------------------------------------------------------------------------------- | |
static String Encode(string input, string [] transforms ) | |
{ | |
Dictionary<char, char> transformDict = GetTransformDictionary( transforms ); | |
StringBuilder encoded = new StringBuilder(); | |
foreach (char c in input) | |
{ | |
if (transformDict.ContainsKey(c)) | |
{ | |
encoded.Append(transformDict[c]); | |
} | |
else | |
{ | |
encoded.Append(c); | |
} | |
} | |
return encoded.ToString(); | |
} | |
static string ProcessEncodeRequest(string[] args) | |
{ | |
if (args.Length < 2) | |
{ | |
return ""; | |
} | |
string input = args[0]; | |
string rawTransforms = args[1]; | |
string [] transforms = rawTransforms.Split( transform_seperators, StringSplitOptions.RemoveEmptyEntries ); | |
string output = Encode(input, transforms); | |
#if DEBUG | |
System.Diagnostics.Debug.Print("INPUT \"{0}\"", input); | |
System.Diagnostics.Debug.Print("RAW TRANSFORMS \"{0}\"", input); | |
System.Diagnostics.Debug.Print("TRANSFORMS {0}", transforms); | |
System.Diagnostics.Debug.Print("OUTPUT \"{0}\"", output); | |
#endif | |
return output; | |
} | |
// -------------------------------------------------------------------- | |
#if DEBUG | |
static void TEST(string input, string transforms, string output) | |
{ | |
string[] args = { input, transforms }; | |
System.Diagnostics.Debug.WriteLine("\"{0}\" => \"{1}\" == \"{2}\" is {3}", input, transforms, output, ProcessEncodeRequest(args).Equals(output)); | |
} | |
#endif | |
static void Main(string[] args) | |
{ | |
if (2 <= args.Length) | |
{ | |
Console.WriteLine( ProcessEncodeRequest( args ) ); | |
} | |
else | |
{ | |
#if DEBUG | |
TEST( "the quick brown fox jumps over the lazy dog", "H", "ygi pre,d nuwob jw. frvql wmiu ygi s;/t kwh" ); | |
TEST( "the quick brown fox jumps over the lazy dog", "V", "gyd ajk3i 5fls6 rl2 uj7;w l4df gyd oq1h elt" ); | |
TEST( "the quick brown fox jumps over the lazy dog", "1", "rgw 0yuxj veiqb diz hynoa icwe rgw kp;t sif" ); | |
TEST( "the quick brown fox jumps over the lazy dog", "-1", "yjr wiovl ntpem gpc ki,ad pbrt yjr ;sxu fph" ); | |
#else | |
Console.WriteLine("BADINPUT: expects 2 arguments, string, CVS-string-transforms"); | |
#endif | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment