34 lines
858 B
C
34 lines
858 B
C
int validate(const char *key) {
|
|
int magic = 3;
|
|
int count = 0;
|
|
for (char c; (c = *key); key++) {
|
|
if (c < '0' || c > '9') continue;
|
|
int v = c - '0';
|
|
if (++count == 13) {
|
|
// final character.
|
|
return magic % 10 == v;
|
|
} else {
|
|
v ^= magic * 2;
|
|
magic += v;
|
|
}
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
int main(int argc, char **argv) {
|
|
if (argc == 1) {
|
|
// self-test.
|
|
if (!validate("0000-00000-0003")) return -1;
|
|
if (!validate("1234-56789-0123")) return -1;
|
|
if (!validate("1337-42069-0008")) return -1;
|
|
if (!validate("1998-00000-1997")) return -1;
|
|
if (!validate("3333-33333-3333")) return -1;
|
|
return 0;
|
|
}
|
|
|
|
int ret = 0;
|
|
for (int i = 1; i < argc; i++) {
|
|
if (!validate(argv[i])) ret++;
|
|
}
|
|
return ret;
|
|
}
|