Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //TODO: tale tet instead of tt?
- //int checkUp(int tt[4][4], int x, int y) { return ((y == 0 || !(tt[x][y - 1]) ? false : true)); }
- //int checkRight(int a[4][4], int x, int y) { return ((x == 3) || !(a[x + 1][y]) ? false : true); }
- //int checkLeft(int tt[4][4], int x, int y) { return ((x == 0) || !(tt[x - 1][y]) ? false : true); }
- //int checkDown(int tt[4][4], int x, int y) { return ((y == 3) || (tt[x][y + 1] != 1) ? false : true); }
- //
- //int isValidHash()
- //{
- // int myarr[4][4];
- // int array[4][2] = { {1,1}, {2,1}, {3,1}, {4,1} };
- // int x1 = array[0][0]; int y1 = array[0][1];
- // int x2 = array[1][0]; int y2 = array[1][1];
- // int x3 = array[2][0]; int y3 = array[2][1];
- // int x4 = array[3][0]; int y4 = array[3][1];
- //
- // for (int j = 0; j < 4; j++) //TODO: convert these to while
- // for (int i = 0; i < 4; i++)
- // myarr[i][j] = ((x1 - 1 == i && y1 - 1 == j) ||
- // (x2 - 1 == i && y2 - 1 == j) ||
- // (x3 - 1 == i && y3 - 1 == j) ||
- // (x4 - 1 == i && y4 - 1 == j)) ? true : false;
- //
- // int isValid;
- // for (int y = 0; y < 4; y++) //TODO: convert these to while
- // for (int x = 0; x < 4; x++) {
- // if (myarr[x][y+1] == true)
- // isValid = (checkUp(myarr, x, y) == false && checkDown(myarr, x,y+1) == false
- // && checkLeft(myarr, x, y) == false && checkLeft(myarr, x, y+1) == false
- // && checkRight(myarr, x, y) == false && checkRight(myarr, x, y+1) == false) ? false : true;
- // if (myarr[x][y] == true) {
- // isValid = (checkUp(myarr, x, y) == false && checkDown(myarr, x, y) == false)
- // && checkRight(myarr, x, y) == false && checkLeft(myarr, x, y) == false ? false : true;
- // if (myarr[x + 1][y] == true)
- // isValid = (checkDown(myarr, x, y) == false && checkDown(myarr, x + 1, y) == false
- // && checkRight(myarr, x + 1, y) == false && checkLeft(myarr, x, y) == false
- // && checkUp(myarr, x, y) == false && checkUp(myarr, x + 1, y) == false) ? false : true;
- // }
- // }
- // return (isValid);
- //}
- # include <unistd.h>
- # include <stdlib.h>
- # include <stdio.h>
- # include <fcntl.h>
- # define up 0
- # define down 1
- # define left 2
- # define right 3
- void *ft_memset(void *b, int c, size_t len)
- {
- int i;
- unsigned char *p;
- i = 0;
- p = b;
- while (i < (int)len)
- p[i++] = (unsigned char)c;
- return (b);
- }
- void ft_bzero(void *s, size_t n)
- {
- ft_memset(s, 0, n);
- }
- void *ft_memalloc(size_t size)
- {
- void *alloc;
- if ((int)size < 0 || !size)
- return (0);
- alloc = malloc(size);
- if (alloc)
- ft_bzero(alloc, size);
- return (alloc ? alloc : 0);
- }
- typedef struct tet {
- struct tet *next;
- int value[4][2];
- } ttet;
- int has(int tet[4][2], int x, int y, char flag) {
- int i;
- i = -1;
- y = (flag == up) ? y + 1 : y;
- y = (flag == down) ? y - 1 : y;
- x = (flag == left) ? x + 1 : x;
- x = (flag == right) ? x - 1 : x;
- printf("X:%d, Y: %d\n", x, y);
- while (++i < 4)
- if (tet[i][0] == x && tet[i][1] == y)
- return 1;
- return 0;
- }
- int check(int tet[4][2], int x, int y, char *flags) {
- int connexions;
- connexions = 0;
- while (*flags)
- connexions += has(tet, x, y, *flags++);
- return (connexions);
- }
- int isValid(int tet[4][2])
- {
- int i;
- int tetx;
- int tety;
- char tetsum;
- char *flag;
- char *dflags;
- dflags = ft_memalloc(4);
- i = -1;
- while (++i < 4)
- {
- flag = dflags;
- tetx = tet[i][0];
- tety = tet[i][1];
- if (tety != 4)
- *flag++ = up;
- if (tety != 1)
- *flag++ = down;
- if (tetx != 4)
- *flag++ = left;
- if (tetx != 1)
- *flag++ = right;
- tetsum += check(tet, tetx, tety, dflags);
- ft_bzero(dflags, 4);
- }
- return (tetsum == 6 || tetsum == 8);
- }
- int validate(int fd, ttet *tets)
- {
- int x, y;
- int valids;
- char value;
- short checksum;
- x = y = 4;
- valids = 0;
- checksum = 742;
- while(checksum > -1 && read(fd, &value, 1))
- {
- if (checksum == 742)
- checksum = (tets = (ttet *)malloc(sizeof(ttet))) ? 0 : -1;
- if (value == '#')
- {
- checksum += value;
- tets->value[valids][0] = x;
- tets->value[valids][1] = y;
- if (!(valids = (++valids == 4) ? 0 : valids))
- if (isValid((tets->value)))
- tets = tets->next;
- }
- else if (value == '\n')
- checksum = (x && x != 4 && y != 4) ? -1 : checksum + value;
- else
- checksum = (value != '.') ? -1 : checksum + value;
- checksum = (checksum > 742) ? -1 : checksum;
- y = (value == '\n' && x == 0) ? y - 1 : y;
- x = (value == '\n') ? 4 : x - 1;
- y = (y == 0) ? 4 : y;
- }
- return (checksum == 732);
- }
- void prnt_list(ttet *tets)
- {
- int i;
- while (tets)
- {
- i = 4;
- while (--i)
- printf("x: %d, y: %d\n", tets->value[i][0], tets->value[i][1]);
- tets = tets->next;
- }
- }
- int main(int argc, char **argv)
- {
- struct tet *tets;
- tets = 0;
- if (argc == 2 && validate(open(argv[1], O_RDONLY), tets))
- prnt_list(tets);
- else
- printf("usage: fillit target_file\n");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement