Commit eca514e3 authored by rforder's avatar rforder

Initial commit

parent 179b62a1
all:
gcc dominos.c -lm -o dominos
\ No newline at end of file
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
typedef struct _domino {
char a[1024];
char b[1024];
struct _domino *next;
} domino;
domino *parse_dominos(char *buffer, long size)
{
long i = 0, j;
domino *head = NULL;
domino *current = NULL;
int first = 1;
while (i < size)
{
if (isspace(buffer[i]))
{
i++;
}
else
{
if (first)
{
head = current = (domino *) malloc(sizeof(domino));
first = 0;
}
else
{
current->next = (domino *) malloc(sizeof(domino));
current = current->next;
current->next = NULL;
}
j = 0;
while (!isspace(buffer[i]))
{
current->a[j] = buffer[i];
i++;
j++;
if (i >= size)
{
fprintf(stderr, "parsing error\n");
exit(1);
}
if (j >= 1023)
{
fprintf(stderr, "parsing error\n");
exit(1);
}
}
current->a[j] = 0;
while (isspace(buffer[i]))
{
i++;
if (i >= size)
{
fprintf(stderr, "parsing error\n");
exit(1);
}
}
j = 0;
while (!isspace(buffer[i]))
{
current->b[j] = buffer[i];
i++;
j++;
if (i >= size)
{
return head;
}
if (j >= 1023)
{
fprintf(stderr, "parsing error\n");
exit(1);
}
}
current->b[j] = 0;
}
}
return head;
}
int list_length(domino *domino_list)
{
int i = 0;
domino *d = domino_list;
while (d)
{
i++;
d = d-> next;
}
return i;
}
domino *get_ith_domino(int i, domino *domino_list)
{
domino *d = domino_list;
while (i > 0 && d->next)
{
d = d->next;
i--;
}
if (i > 0)
return NULL;
else
return d;
}
int is_accepted(int *sequence, int seq_len, domino *domino_list, char *input)
{
int i, j, k, ex, dx;
ex = dx = 0;
i = j = k = 0;
while (dx < seq_len && ex < seq_len && (!input || k < strlen(input)))
{
domino *e = get_ith_domino(sequence[ex], domino_list);
domino *d = get_ith_domino(sequence[dx], domino_list);
while (i < strlen(e->a) && j < strlen(d->b) && (!input || k < strlen(input)))
{
if (input && !((e->a[i] == d->b[j]) && (e->a[i] == input[k])))
{
return 0;
}
if (!input && (e->a[i] != d->b[j]))
{
return 0;
}
i++;
j++;
k++;
}
if (i == strlen(e->a))
{
i = 0;
ex++;
}
if (j == strlen(d->b))
{
j = 0;
dx++;
}
}
if (dx == seq_len && ex == seq_len && (!input || k == strlen(input)))
{
return 1;
}
else
{
return 0;
}
}
int test_seq_length_k(domino *domino_list, int k, char *input)
{
int *seq = calloc(sizeof(int), k);
int dom_cnt = list_length(domino_list);
int i;
while (1)
{
if (is_accepted(seq, k, domino_list, input))
{
int x;
for (x = 0; x < k; x++)
{
domino *d = get_ith_domino(seq[x], domino_list);
printf("%s:%s ", d->a, d->b);
}
printf("\n");
if (input)
{
free(seq);
return 1;
}
}
i = 0;
while (1)
{
if (i == k)
{
free(seq);
return 0;
}
seq[i]++;
if (seq[i] >= dom_cnt)
{
seq[i] = 0;
i++;
}
else
{
break;
}
}
}
}
int main(int argc, char **argv)
{
FILE *input;
if (argc < 2 || argc > 3)
{
printf("usage: %s file [input]\n", argv[0]);
exit(1);
}
input = fopen(argv[1], "rb");
if (!input)
{
fprintf(stderr, "Cannot open file \"%s\" for reading\n", argv[1]);
exit(1);
}
fseek(input, 0L, SEEK_END);
long int file_size = ftell(input);
fseek(input, 0L, SEEK_SET);
char *input_buffer = malloc(file_size);
fread(input_buffer, file_size, 1, input);
fclose(input);
domino *domino_list = parse_dominos(input_buffer, file_size);
int k = 1;
int accepted = 0;
while (k < (argc == 3 ? strlen(argv[2]) : 999))
{
if (test_seq_length_k(domino_list, k, (argc == 3 ? argv[2] : NULL)))
{
if (argc == 3)
{
accepted = 1;
break;
}
}
k++;
}
if (accepted)
{
printf("accepted\n");
}
else
{
printf("rejected\n");
}
domino *d = domino_list;
domino *e;
while (d)
{
e = d->next;
free(d);
d = e;
}
free(input_buffer);
}
\ No newline at end of file
File added
1 111
10111 10
10 0
\ No newline at end of file
1 1101
101 10
01 1
1101 1
\ No newline at end of file
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment