Найти кодовое слово в коде по задаче - C#
Формулировка задачи:
Всем здравствуйте. Тут ниже есть задача (говорят из Франции). В ней нужно найти кодовое слово, не изменяя сам код, то есть путем логики и знаний C#. Для двух уровней сложности код будет одинаковый.
using System;
namespace ConsoleApplication1 {
class Program {
public struct Proposition {
public bool is_assertion;
public string op;
public int arg1;
public int arg2;
}
static void Main(string[] args) {
Console.WriteLine("===== Welcome to Lab 2 ! =====");
Console.WriteLine("User name: ");
string name = Console.ReadLine();
int level = -1;
while (level != 0 && level != 1) {
Console.WriteLine("Difficulty level (0=easy, 1=hard): ");
if (!int.TryParse(Console.ReadLine(), out level)) level = -1;
}
if (name[0] == 'p')
name = name.Remove(0, 1) + "Z";
Console.WriteLine("Enter code word: ");
string codeword = Console.ReadLine();
Console.WriteLine();
int i = 0; byte c1 = 0, c2 = 0;
for (i = 0; i < name.Length; i++)
c1 += (byte)name[i];
for (i = 0; i < codeword.Length; i++)
c2 += (byte)codeword[i];
if (c1 != c2) {
Console.WriteLine("Wrong code word");
} else {
codeword = "bakbbkbckbbbambcbbmbcbambdtbetbbjbfu" + codeword;
byte[] s = new byte[256];
byte top = (byte)(s.Length-1);
s[top--] = (byte)(1 - level);
Proposition[] proof = new Proposition[256];
bool cheat_mode = true;
int pc = 0, goal = -1;
for (i = 0; i < codeword.Length; i++) {
int code = codeword[i] - 'a';
if (!cheat_mode) {
if ((codeword[i] >= '0' && codeword[i] <= '9') || (codeword[i] >= 'A' && codeword[i] <= 'Z'))
code = codeword[i] ^ c1;
else if (((code ^ c1) >= '0' && (code ^ c1) <= '9') || ((code ^ c1) >= 'A' && (code ^ c1) <= 'Z'))
break;
}
int A, B; if (i==0) cheat_mode = true;
switch (code) {
case 0: i = codeword.Length; break;
case 1: byte val = (byte)(codeword[++i]-'a');
if (!cheat_mode) {
if ((codeword[i] >= '0' && codeword[i] <= '9') || (codeword[i] >= 'A' && codeword[i] <= 'Z'))
val = (byte)(codeword[i] ^ c1);
}
s[--top] = val;
break;
case 2: top++; break;
case 3: s[top] += s[top++]; break;
case 4: s[top] -= s[top++]; break;
case 5: s[top] *= s[top++]; break;
case 6: s[top] /= s[top++]; break;
case 7: s[top] = s[top--]; break;
case 8:
if (goal < 0 || proof[goal].is_assertion)
Console.Write((char)(s[top++]+'A'));
break;
case 9:
int target = s[top++], value = s[top++];
if (target>127) target = target-256;
if (value != 0)
i += target;
break;
case 10:
proof[pc].is_assertion = false;
proof[pc].op = s[top++].ToString();
pc++;
break;
case 11:
proof[pc].is_assertion = false;
proof[pc].op = "NOT";
proof[pc].arg1 = s[top++];
pc++;
break;
case 12:
proof[pc].is_assertion = false;
proof[pc].op = "IMPLICATION";
proof[pc].arg1 = s[top++];
proof[pc].arg2 = s[top++];
pc++;
break;
case 13: case 14:
A = s[top++]; B = s[top++];
int[] x = new int [256], y = new int [256];
x[0] = A; y[0] = B;
int count = 1;
while (count > 0) {
count--;
if (proof[x[count]].op != proof[y[count]].op) {
count = -1;
break;
} else if (proof[x[count]].op == "NOT") {
x[count] = proof[x[count]].arg1; y[count] = proof[y[count]].arg1; count++;
} else if (proof[x[count]].op == "IMPLICATION") {
int xc = x[count], yc = y[count];
x[count] = proof[xc].arg1; y[count] = proof[yc].arg1; count++;
x[count] = proof[xc].arg2; y[count] = proof[yc].arg2; count++;
}
}
if (count == 0) {
if (code == 13) {
proof[pc].is_assertion = true;
proof[pc].op = "IMPLICATION";
proof[pc].arg1 = A;
proof[pc].arg2 = B;
pc++;
} else if (proof[A].is_assertion || proof[B].is_assertion) {
proof[A].is_assertion = true;
proof[B].is_assertion = true;
}
}
break;
case 15: ;
A = s[top++]; B = s[top++];
if (proof[B].op == "IMPLICATION" && proof[B].arg2 == A) {
proof[pc].is_assertion = true;
proof[pc].op = "IMPLICATION";
proof[pc].arg1 = A;
proof[pc].arg2 = B;
pc++;
}
break;
case 16:
A = s[top++]; B = s[top++];
if (proof[A].op == "IMPLICATION" && proof[B].op == "IMPLICATION" &&
proof[proof[A].arg2].op == "IMPLICATION" &&
proof[proof[B].arg1].op == "IMPLICATION" &&
proof[proof[B].arg1].arg1 == proof[A].arg1 &&
proof[proof[B].arg1].arg2 == proof[proof[A].arg2].arg1 &&
proof[proof[B].arg2].op == "IMPLICATION" &&
proof[proof[B].arg2].arg1 == proof[A].arg1 &&
proof[proof[B].arg2].arg2 == proof[proof[A].arg2].arg2)
{
proof[pc].is_assertion = true;
proof[pc].op = "IMPLICATION";
proof[pc].arg1 = A;
proof[pc].arg2 = B;
pc++;
}
break;
case 17:
A = s[top++]; B = s[top++];
if (proof[A].op == "IMPLICATION" && proof[B].op == "IMPLICATION" &&
proof[proof[A].arg1].op == "NOT" && proof[proof[A].arg2].op == "NOT" &&
proof[proof[A].arg1].arg1 == proof[B].arg2 &&
proof[proof[A].arg2].arg1 == proof[B].arg1)
{
proof[pc].is_assertion = true;
proof[pc].op = "IMPLICATION";
proof[pc].arg1 = A;
proof[pc].arg2 = B;
pc++;
}
break;
case 18:
A = s[top++];
if (proof[A].op == "IMPLICATION" &&
proof[A].is_assertion &&
proof[proof[A].arg1].is_assertion)
proof[proof[A].arg2].is_assertion = true;
break;
case 19:
if (cheat_mode)
proof[s[top++]].is_assertion = true;
break;
case 20:
if (cheat_mode) goal = s[top++];
cheat_mode = false;
break;
}
}
}
Console.ReadKey();
}
}
}Решение задачи: «Найти кодовое слово в коде по задаче»
textual
Листинг программы
case 1: byte val = (byte)(codeword[++i]-'a'); //...и рецикл с i++