Skip to content

Instantly share code, notes, and snippets.

@tluyben

tluyben/verhoef.cs

Last active Aug 21, 2020
Embed
What would you like to do?
Verhoeff's Dihedral Group D5 Check raw port from JS
using System;
public class Program
{
int[][] F = new int[8][];
int[][] Op = new int[10][];
int[] Inv = new int[] { 0, 4, 3, 2, 1, 5, 6, 7, 8, 9 };
public Program()
{
F[0] = new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
F[1] = new int[] { 1, 5, 7, 6, 2, 8, 3, 0, 9, 4 };
for ( var i = 2; i < 8; i++ )
{
F[i] = new int[10];
for ( var j = 0; j < 10; j++ )
F[i][j] = F[i-1][F[1][j]];
}
Op[0] = new int[]{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
Op[1] = new int[]{ 1, 2, 3, 4, 0, 6, 7, 8, 9, 5 };
Op[2] = new int[]{ 2, 3, 4, 0, 1, 7, 8, 9, 5, 6 };
Op[3] = new int[]{ 3, 4, 0, 1, 2, 8, 9, 5, 6, 7 };
Op[4] = new int[]{ 4, 0, 1, 2, 3, 9, 5, 6, 7, 8 };
Op[5] = new int[]{ 5, 9, 8, 7, 6, 0, 4, 3, 2, 1 };
Op[6] = new int[]{ 6, 5, 9, 8, 7, 1, 0, 4, 3, 2 };
Op[7] = new int[]{ 7, 6, 5, 9, 8, 2, 1, 0, 4, 3 };
Op[8] = new int[]{ 8, 7, 6, 5, 9, 3, 2, 1, 0, 4 };
Op[9] = new int[]{ 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 };
}
string ReverseStr(string str)
{
var rev = "";
for ( var i = str.Length - 1; i >= 0; i-- )
rev = rev + str[i];
return rev;
}
bool Check(string code)
{
var a = ReverseStr(code);
var check = 0;
for ( var i=0; i < a.Length; i++ )
check = Op[check][F[i%8][int.Parse(a[i]+"")]];
return check == 0;
}
string Compute(string code)
{
var a = "x"+ReverseStr(code);
var check = 0;
for ( var i = 1; i < a.Length; i++ )
check = Op[check][F[i%8][int.Parse(a[i]+"")]];
return code + Inv[check];
}
public static void Main()
{
var code = "4535";
var p = new Program();
var code1 = p.Compute(code);
var code2 = p.Compute(code1);
Console.WriteLine(code2);
Console.WriteLine(p.Check(code2));
Console.WriteLine(p.Check(code2.Substring(0, code2.Length-1)));
Console.WriteLine("Hello World");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.