Advertisement
Guest User

Untitled

a guest
Nov 20th, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.37 KB | None | 0 0
  1. include <unistd.h>
  2. # include <stdlib.h>
  3. # include <stdio.h>
  4. # include <fcntl.h>
  5.  
  6. # define up 0
  7. # define down 1
  8. # define left 2
  9. # define right 3
  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. printf("X:%d, Y: %d\n", x, y);
  52. while (++i < 4)
  53. if (tet[i][0] == x && tet[i][1] == y)
  54. return 1;
  55. return 0;
  56. }
  57.  
  58. int check(int tet[4][2], int x, int y, char *flags) {
  59. int connexions;
  60.  
  61. connexions = 0;
  62. while (*flags)
  63. connexions += has(tet, x, y, *flags++);
  64. return (connexions);
  65. }
  66.  
  67. int isValid(int tet[4][2])
  68. {
  69. int i;
  70. int tetx;
  71. int tety;
  72. char tetsum;
  73. char *flag;
  74. char *dflags;
  75.  
  76. dflags = ft_memalloc(4);
  77. i = -1;
  78. while (++i < 4)
  79. {
  80. flag = dflags;
  81. tetx = tet[i][0];
  82. tety = tet[i][1];
  83.  
  84. if (tety != 4)
  85. *flag++ = up;
  86. if (tety != 1)
  87. *flag++ = down;
  88. if (tetx != 4)
  89. *flag++ = left;
  90. if (tetx != 1)
  91. *flag++ = right;
  92. tetsum += check(tet, tetx, tety, dflags);
  93. ft_bzero(dflags, 4);
  94. }
  95. return (tetsum == 6 || tetsum == 8);
  96. }
  97.  
  98. int validate(int fd, ttet *tets)
  99. {
  100. int x, y;
  101. int valids;
  102. char value;
  103. short checksum;
  104.  
  105. x = y = 4;
  106. valids = 0;
  107. checksum = 742;
  108. while(checksum > -1 && read(fd, &value, 1))
  109. {
  110. if (checksum == 742)
  111. checksum = (tets = (ttet *)malloc(sizeof(ttet))) ? 0 : -1;
  112. if (value == '#')
  113. {
  114. checksum += value;
  115. tets->value[valids][0] = x;
  116. tets->value[valids][1] = y;
  117. if (!(valids = (++valids == 4) ? 0 : valids))
  118. if (isValid((tets->value)))
  119. tets = tets->next;
  120. }
  121. else if (value == '\n')
  122. checksum = (x && x != 4 && y != 4) ? -1 : checksum + value;
  123. else
  124. checksum = (value != '.') ? -1 : checksum + value;
  125. checksum = (checksum > 742) ? -1 : checksum;
  126. y = (value == '\n' && x == 0) ? y - 1 : y;
  127. x = (value == '\n') ? 4 : x - 1;
  128. y = (y == 0) ? 4 : y;
  129. }
  130. return (checksum == 732);
  131. }
  132.  
  133. void prnt_list(ttet *tets)
  134. {
  135. int i;
  136.  
  137. while (tets)
  138. {
  139. i = 4;
  140. while (--i)
  141. printf("x: %d, y: %d\n", tets->value[i][0], tets->value[i][1]);
  142. tets = tets->next;
  143. }
  144. }
  145.  
  146. int main(int argc, char **argv)
  147. {
  148. struct tet *tets;
  149.  
  150. tets = 0;
  151. if (argc == 2 && validate(open(argv[1], O_RDONLY), tets))
  152. prnt_list(tets);
  153. else
  154. printf("usage: fillit target_file\n");
  155. return 0;
  156. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement