Skip to content

Instantly share code, notes, and snippets.

@sholev
Created October 24, 2015 19:56
Show Gist options
  • Save sholev/35c6dd25f2e76382e4e7 to your computer and use it in GitHub Desktop.
Save sholev/35c6dd25f2e76382e4e7 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
class DetectiveBoev
{
public static int SumDigits(int value)
{
int sum = 0;
while (value != 0)
{
int rem;
value = Math.DivRem(value, 10, out rem);
sum += rem;
}
return sum;
}
public static string Reverse(string s)
{
char[] charArray = s.ToCharArray();
Array.Reverse(charArray);
return new string(charArray);
}
static void Main(string[] args)
{
string codeword = Console.ReadLine();
string message = Console.ReadLine();
//string message = "|wfvohyfqX#hodzwclV";
int[] codewordvalues = new int[codeword.Length];
for (int c = 0; c < codeword.Length; c++)
{
codewordvalues[c] = Convert.ToInt32(codeword[c]);
}
int codevaluesum = codewordvalues.Sum();
int mask = codevaluesum;
while (mask > 9)
{
mask = SumDigits(mask);
}
// Console.WriteLine("Mask value: " +mask);
int[] messagevalues = new int[message.Length];
for (int i = 0; i < message.Length; i++)
{
messagevalues[i] = Convert.ToInt16(message[i]);
}
//int ca = 0;
//do
//{
// ca++;
// messagevalues[ca] = Convert.ToInt16(message[ca]);
// // Console.WriteLine(message[ca] + "=" + messagevalues[ca]);
//}
//while (ca < message.Length - 1);
int[] dectyptmessagevalues = new int[message.Length];
for (int c1 = 0; c1 < message.Length; c1++)
{
if (messagevalues[c1] % mask == 0)
{
dectyptmessagevalues[c1] = messagevalues[c1] + mask;
}
if (messagevalues[c1] % mask != 0)
{
dectyptmessagevalues[c1] = messagevalues[c1] - mask;
}
}
char[] encodedmessage = new char[message.Length];
for (int count = 0; count < message.Length; count++)
{
encodedmessage[count] = Convert.ToChar(dectyptmessagevalues[count]);
}
string finalmessage_reverse = new string(encodedmessage);
// Console.WriteLine(finalmessage_reverse);
string final_result = Reverse(finalmessage_reverse);
Console.WriteLine(final_result);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment