Advertisement
Guest User

Untitled

a guest
Nov 20th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.71 KB | None | 0 0
  1. //TODO: tale tet instead of tt?
  2. //int checkUp(int tt[4][4], int x, int y) { return ((y == 0 || !(tt[x][y - 1]) ? false : true)); }
  3. //int checkRight(int a[4][4], int x, int y) { return ((x == 3) || !(a[x + 1][y]) ? false : true); }
  4. //int checkLeft(int tt[4][4], int x, int y) { return ((x == 0) || !(tt[x - 1][y]) ? false : true); }
  5. //int checkDown(int tt[4][4], int x, int y) { return ((y == 3) || (tt[x][y + 1] != 1) ? false : true); }
  6. //
  7. //int isValidHash()
  8. //{
  9. // int myarr[4][4];
  10. // int array[4][2] = { {1,1}, {2,1}, {3,1}, {4,1} };
  11. // int x1 = array[0][0]; int y1 = array[0][1];
  12. // int x2 = array[1][0]; int y2 = array[1][1];
  13. // int x3 = array[2][0]; int y3 = array[2][1];
  14. // int x4 = array[3][0]; int y4 = array[3][1];
  15. //
  16. // for (int j = 0; j < 4; j++) //TODO: convert these to while
  17. // for (int i = 0; i < 4; i++)
  18. // myarr[i][j] = ((x1 - 1 == i && y1 - 1 == j) ||
  19. // (x2 - 1 == i && y2 - 1 == j) ||
  20. // (x3 - 1 == i && y3 - 1 == j) ||
  21. // (x4 - 1 == i && y4 - 1 == j)) ? true : false;
  22. //
  23. // int isValid;
  24. // for (int y = 0; y < 4; y++) //TODO: convert these to while
  25. // for (int x = 0; x < 4; x++) {
  26. // if (myarr[x][y+1] == true)
  27. // isValid = (checkUp(myarr, x, y) == false && checkDown(myarr, x,y+1) == false
  28. // && checkLeft(myarr, x, y) == false && checkLeft(myarr, x, y+1) == false
  29. // && checkRight(myarr, x, y) == false && checkRight(myarr, x, y+1) == false) ? false : true;
  30. // if (myarr[x][y] == true) {
  31. // isValid = (checkUp(myarr, x, y) == false && checkDown(myarr, x, y) == false)
  32. // && checkRight(myarr, x, y) == false && checkLeft(myarr, x, y) == false ? false : true;
  33. // if (myarr[x + 1][y] == true)
  34. // isValid = (checkDown(myarr, x, y) == false && checkDown(myarr, x + 1, y) == false
  35. // && checkRight(myarr, x + 1, y) == false && checkLeft(myarr, x, y) == false
  36. // && checkUp(myarr, x, y) == false && checkUp(myarr, x + 1, y) == false) ? false : true;
  37. // }
  38. // }
  39. // return (isValid);
  40. //}
  41. # include <unistd.h>
  42. # include <stdlib.h>
  43. # include <stdio.h>
  44. # include <fcntl.h>
  45.  
  46. # define up 0
  47. # define down 1
  48. # define left 2
  49. # define right 3
  50.  
  51. void *ft_memset(void *b, int c, size_t len)
  52. {
  53. int i;
  54. unsigned char *p;
  55.  
  56. i = 0;
  57. p = b;
  58. while (i < (int)len)
  59. p[i++] = (unsigned char)c;
  60. return (b);
  61. }
  62. void ft_bzero(void *s, size_t n)
  63. {
  64. ft_memset(s, 0, n);
  65. }
  66. void *ft_memalloc(size_t size)
  67. {
  68. void *alloc;
  69.  
  70. if ((int)size < 0 || !size)
  71. return (0);
  72. alloc = malloc(size);
  73. if (alloc)
  74. ft_bzero(alloc, size);
  75. return (alloc ? alloc : 0);
  76. }
  77.  
  78. typedef struct tet {
  79. struct tet *next;
  80. int value[4][2];
  81. } ttet;
  82.  
  83. int has(int tet[4][2], int x, int y, char flag) {
  84. int i;
  85.  
  86. i = -1;
  87. y = (flag == up) ? y + 1 : y;
  88. y = (flag == down) ? y - 1 : y;
  89. x = (flag == left) ? x + 1 : x;
  90. x = (flag == right) ? x - 1 : x;
  91. printf("X:%d, Y: %d\n", x, y);
  92. while (++i < 4)
  93. if (tet[i][0] == x && tet[i][1] == y)
  94. return 1;
  95. return 0;
  96. }
  97.  
  98. int check(int tet[4][2], int x, int y, char *flags) {
  99. int connexions;
  100.  
  101. connexions = 0;
  102. while (*flags)
  103. connexions += has(tet, x, y, *flags++);
  104. return (connexions);
  105. }
  106.  
  107. int isValid(int tet[4][2])
  108. {
  109. int i;
  110. int tetx;
  111. int tety;
  112. char tetsum;
  113. char *flag;
  114. char *dflags;
  115.  
  116. dflags = ft_memalloc(4);
  117. i = -1;
  118. while (++i < 4)
  119. {
  120. flag = dflags;
  121. tetx = tet[i][0];
  122. tety = tet[i][1];
  123.  
  124. if (tety != 4)
  125. *flag++ = up;
  126. if (tety != 1)
  127. *flag++ = down;
  128. if (tetx != 4)
  129. *flag++ = left;
  130. if (tetx != 1)
  131. *flag++ = right;
  132. tetsum += check(tet, tetx, tety, dflags);
  133. ft_bzero(dflags, 4);
  134. }
  135. return (tetsum == 6 || tetsum == 8);
  136. }
  137.  
  138. int validate(int fd, ttet *tets)
  139. {
  140. int x, y;
  141. int valids;
  142. char value;
  143. short checksum;
  144.  
  145. x = y = 4;
  146. valids = 0;
  147. checksum = 742;
  148. while(checksum > -1 && read(fd, &value, 1))
  149. {
  150. if (checksum == 742)
  151. checksum = (tets = (ttet *)malloc(sizeof(ttet))) ? 0 : -1;
  152. if (value == '#')
  153. {
  154. checksum += value;
  155. tets->value[valids][0] = x;
  156. tets->value[valids][1] = y;
  157. if (!(valids = (++valids == 4) ? 0 : valids))
  158. if (isValid((tets->value)))
  159. tets = tets->next;
  160. }
  161. else if (value == '\n')
  162. checksum = (x && x != 4 && y != 4) ? -1 : checksum + value;
  163. else
  164. checksum = (value != '.') ? -1 : checksum + value;
  165. checksum = (checksum > 742) ? -1 : checksum;
  166. y = (value == '\n' && x == 0) ? y - 1 : y;
  167. x = (value == '\n') ? 4 : x - 1;
  168. y = (y == 0) ? 4 : y;
  169. }
  170. return (checksum == 732);
  171. }
  172.  
  173. void prnt_list(ttet *tets)
  174. {
  175. int i;
  176.  
  177. while (tets)
  178. {
  179. i = 4;
  180. while (--i)
  181. printf("x: %d, y: %d\n", tets->value[i][0], tets->value[i][1]);
  182. tets = tets->next;
  183. }
  184. }
  185.  
  186. int main(int argc, char **argv)
  187. {
  188. struct tet *tets;
  189.  
  190. tets = 0;
  191. if (argc == 2 && validate(open(argv[1], O_RDONLY), tets))
  192. prnt_list(tets);
  193. else
  194. printf("usage: fillit target_file\n");
  195. return 0;
  196. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement