Skip to content

Instantly share code, notes, and snippets.

@kashi
Created November 14, 2013 13:57
Show Gist options
  • Save kashi/7467196 to your computer and use it in GitHub Desktop.
Save kashi/7467196 to your computer and use it in GitHub Desktop.
/*
http://nabetani.sakura.ne.jp/hena/ord11bitamida/
*/
#include <stdio.h>
#include <string.h>
void permutate(char *s, char *got)
{
int i, j, f, w, x, z, t[4];
sscanf(s, "%02x-%02x-%02x-%02x", &t[0], &t[1], &t[2], &t[3]);
for (i=0; i<9; i++) got[i] = '0' + i;
got[9] = '\0';
for (i=0; i<4; i++) {
x = 1 << 7;
f = 0;
for (j=0; j<9; j++) {
if (x & t[i]) {
if (!f) {
z = j;
f = !0;
}
} else {
if (f) {
w = got[z];
got[z] = got[j];
got[j] = w;
f = 0;
}
}
x >>= 1;
}
}
}
void test(char *s, char *expected)
{
char got[10];
permutate(s, got);
if (strcmp(got, expected) == 0) {
printf("%s : OK\n", s);
} else {
printf("%s : NG : got=%s, expected=%s\n", s, got, expected);
}
}
int main()
{
/*0*/ test( "d6-7b-e1-9e", "740631825" );
/*1*/ test( "83-4c-20-10", "123805476" );
/*2*/ test( "fb-f7-7e-df", "274056813" );
/*3*/ test( "55-33-0f-ff", "123456780" );
/*4*/ test( "00-00-00-00", "012345678" );
/*5*/ test( "00-00-00-55", "021436587" );
/*6*/ test( "40-10-04-01", "021436587" );
/*7*/ test( "00-00-aa-00", "103254768" );
/*8*/ test( "80-20-08-02", "103254768" );
/*9*/ test( "ff-7e-3c-18", "876543210" );
/*10*/ test( "aa-55-aa-55", "351708264" );
/*11*/ test( "55-aa-aa-55", "012345678" );
/*12*/ test( "db-24-db-e7", "812543670" );
/*13*/ test( "00-01-00-40", "021345687" );
/*14*/ test( "00-00-80-00", "102345678" );
/*15*/ test( "01-40-00-00", "021345687" );
/*16*/ test( "00-00-00-02", "012345768" );
/*17*/ test( "00-00-02-00", "012345768" );
/*18*/ test( "00-14-00-00", "012436578" );
/*19*/ test( "00-00-01-40", "021345687" );
/*20*/ test( "00-80-01-00", "102345687" );
/*21*/ test( "c8-00-00-81", "120354687" );
/*22*/ test( "05-48-08-14", "021435687" );
/*23*/ test( "24-05-00-f0", "413205687" );
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment