Skip to content

Instantly share code, notes, and snippets.

@kashwaa
Created March 22, 2014 11:03
Show Gist options
  • Save kashwaa/9705264 to your computer and use it in GitHub Desktop.
Save kashwaa/9705264 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace codejam
{
class T9
{
public static void Main()
{
List<string> output = new List<string>();
var data = File.ReadAllLines("data.txt");
int caseno = 0;
var map = getMap();
for (int i = 1; i < data.Length; i++)
{
string res = convert(data[i], map);
output.Add(String.Format("Case #{0}: {1}", ++caseno, res));
}
File.WriteAllLines("output", output);
}
public static string convert(string input, Dictionary<char, string> map)
{
StringBuilder output = new StringBuilder();
char[] word = input.ToCharArray();
string last = " ";
foreach (var ch in word)
{
output.Append(((last[0] == map[ch][0]) ? ' ' + map[ch] : map[ch]));
last = map[ch];
}
return output.ToString(); ;
}
public static Dictionary<char, string> getMap()
{
Dictionary<char, string> map = new Dictionary<char, string>();
int skip = 0;
for (char i = 'a'; i <= 'z'; i++)
{
int newVal = i - 91;
if (newVal == 24 || newVal == 31) skip++;
int key = (newVal - skip) / 3;
int rep = (newVal - skip) % 3 + 1;
map.Add(i, (i == 's' || i == 'z' ? repeat((key).ToString(), 4) : repeat(key.ToString(), rep)));
}
map.Add(' ', "0");
return map;
}
public static string repeat(string s, int t)
{
StringBuilder sb = new StringBuilder();
for (int i = 0; i < t; i++)
{
sb.Append(s);
}
return sb.ToString();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment