Advertisement
Guest User

Untitled

a guest
Nov 20th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.52 KB | None | 0 0
  1. # include <unistd.h>
  2. # include <stdlib.h>
  3. # include <stdio.h>
  4. # include <fcntl.h>
  5.  
  6. # define up 1
  7. # define down 2
  8. # define left 3
  9. # define right 4
  10.  
  11. void *ft_memset(void *b, int c, size_t len)
  12. {
  13. int i;
  14. unsigned char *p;
  15.  
  16. i = 0;
  17. p = b;
  18. while (i < (int)len)
  19. p[i++] = (unsigned char)c;
  20. return (b);
  21. }
  22. void ft_bzero(void *s, size_t n)
  23. {
  24. ft_memset(s, 0, n);
  25. }
  26. void *ft_memalloc(size_t size)
  27. {
  28. void *alloc;
  29.  
  30. if ((int)size < 0 || !size)
  31. return (0);
  32. alloc = malloc(size);
  33. if (alloc)
  34. ft_bzero(alloc, size);
  35. return (alloc ? alloc : 0);
  36. }
  37.  
  38. typedef struct tet {
  39. struct tet *next;
  40. int value[4][2];
  41. } ttet;
  42.  
  43. int has(int tet[4][2], int x, int y, char flag) {
  44. int i;
  45.  
  46. i = -1;
  47. y = (flag == up) ? y + 1 : y;
  48. y = (flag == down) ? y - 1 : y;
  49. x = (flag == left) ? x + 1 : x;
  50. x = (flag == right) ? x - 1 : x;
  51. while (++i < 4)
  52. if (x == tet[i][0] && tet[i][1] == y)
  53. return 1;
  54. return 0;
  55. }
  56.  
  57. char check(int tet[4][2], int x, int y, char *flags) {
  58. char connexions;
  59.  
  60. connexions = 0;
  61. while (*flags)
  62. connexions += has(tet, x, y, *flags++);
  63. return (connexions);
  64. }
  65.  
  66. int isValid(int tet[4][2])
  67. {
  68. int i;
  69. char tetsum;
  70. char *flag;
  71. char *dflags;
  72.  
  73. dflags = ft_memalloc(5);
  74. tetsum = 0;
  75. i = -1;
  76. while (++i < 4)
  77. {
  78. flag = dflags;
  79. if (tet[i][1] != 4)
  80. *flag++ = up;
  81. if (tet[i][1] != 1)
  82. *flag++ = down;
  83. if (tet[i][0] != 4)
  84. *flag++ = left;
  85. if (tet[i][0] != 1)
  86. *flag++ = right;
  87. *flag = check(tet, tet[i][0], tet[i][1], dflags);
  88. if (*flag == 0)
  89. return 0;
  90. tetsum += *flag;
  91. ft_bzero(dflags, 5);
  92. }
  93. return (tetsum == 6 || tetsum == 8);
  94. }
  95.  
  96. int validate(int fd, ttet *tets)
  97. {
  98. int x, y;
  99. int valids;
  100. char value;
  101. short checksum;
  102.  
  103. x = y = 4;
  104. valids = 0;
  105. checksum = 0;
  106. while(checksum > -1 && read(fd, &value, 1))
  107. {
  108. if (checksum == 742)
  109. {
  110. if (!(tets->next = (ttet *) malloc(sizeof(ttet))))
  111. break;
  112. tets = tets->next;
  113. checksum = 0;
  114. }
  115. if (value == '#' && (checksum += value))
  116. {
  117. tets->value[valids][0] = x;
  118. tets->value[valids][1] = y;
  119. if (!(valids = (++valids == 4) ? 0 : valids))
  120. checksum = (isValid(tets->value)) ? checksum : -1;
  121. }
  122. else if (value == '\n')
  123. checksum = (x && x != 4 && y != 4) ? -1 : checksum + value;
  124. else
  125. checksum = (value != '.') ? -1 : checksum + value;
  126. checksum = (checksum > 742) ? -1 : checksum;
  127. y = (value == '\n' && x == 0) ? y - 1 : y;
  128. x = (value == '\n') ? 4 : x - 1;
  129. y = (y == 0) ? 4 : y;
  130. }
  131. tets->next = NULL;
  132. return (checksum == 742 - 'n');
  133. }
  134.  
  135. void prnt_list(ttet *tets)
  136. {
  137. int i;
  138.  
  139. while (tets)
  140. {
  141. i = 4;
  142. while (i--)
  143. printf("x: %d, y: %d\n", tets->value[i][0], tets->value[i][1]);
  144. printf("\n");
  145. tets = tets->next;
  146. }
  147. }
  148.  
  149. int main(int argc, char **argv)
  150. {
  151. struct tet *tets;
  152.  
  153. tets = (ttet *)malloc(sizeof(ttet));
  154. if (argc == 2 && validate(open(argv[1], O_RDONLY), tets))
  155. prnt_list(tets);
  156. else
  157. printf("usage: fillit target_file\n");
  158. return 0;
  159. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement