Skip to content

Instantly share code, notes, and snippets.

@Prince781
Created January 13, 2015 05:02
Show Gist options
  • Save Prince781/e27315ae96adf889b775 to your computer and use it in GitHub Desktop.
Save Prince781/e27315ae96adf889b775 to your computer and use it in GitHub Desktop.
Validate 2sat data
/* validate 2sat input data */
#include <stdio.h>
#include <stdlib.h>
#define abs(x) ((x) < 0 ? -(x) : (x))
int main(int argc, char *argv[])
{
if (argc < 2) {
printf("usage: %s [file]\n", argv[0]);
return 1;
}
FILE *fin = fopen(argv[1], "r");
int fail = 0;
int nv = 0, nc = 0;
int n;
int i, j, k;
int *vals;
fscanf(fin, "%d %d", &nv, &nc);
vals = calloc((nv > 0 ? nv : 1), sizeof(int));
if (nc < nv) {
printf("Number of clauses < number of values\n");
fail = 1;
goto end;
}
for (i=1; (n = fscanf(fin, "%d %d", &j, &k)) == 2; ++i) {
if (abs(j) == 0 || abs(k) == 0) {
printf("Clause contains a zero\n");
fail = 1;
goto end;
} else if (abs(j) > nv || abs(k) > nv) {
printf("Line %d contains out-of-bounds value(s)\n",
i+1);
fail = 1;
goto end;
} else if (i > nc) {
printf("Number of lines > number of clauses\n");
fail = 1;
goto end;
}
vals[abs(j)-1] = abs(j);
vals[abs(k)-1] = abs(k);
}
if (n != EOF) {
printf("Line %d contains %d vars\n", i, n);
fail = 1;
goto end;
}
if (i <= nc) {
printf("Number of lines < number of clauses\n");
fail = 1;
goto end;
}
for (i=0; i<nv; ++i)
if (vals[i] != i+1) {
printf("var %d not present\n", i+1);
fail = 1;
}
end:
if (fail)
printf("Invalid data\n");
else
printf("Valid data\n");
free(vals);
fclose(fin);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment