Advertisement
Guest User

Untitled

a guest
Jul 15th, 2018
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.70 KB | None | 0 0
  1. /* ************************************************************************** */
  2. /* */
  3. /* ::: :::::::: */
  4. /* ft_anything_but.c :+: :+: :+: */
  5. /* +:+ +:+ +:+ */
  6. /* By: vsanchez <marvin@42.fr> +#+ +:+ +#+ */
  7. /* +#+#+#+#+#+ +#+ */
  8. /* Created: 2018/07/14 17:11:11 by vsanchez #+# #+# */
  9. /* Updated: 2018/07/15 12:17:06 by vsanchez ### ########.fr */
  10. /* */
  11. /* ************************************************************************** */
  12.  
  13. #include <stdlib.h>
  14.  
  15. char *ft_anything_but(char *str)
  16. {
  17. char *to_return;
  18. int j;
  19. int index;
  20. int i;
  21.  
  22. i = 1;
  23. index = 0;
  24. to_return = malloc(sizeof(char) * 10);
  25. while (i < 10)
  26. {
  27. j = 0;
  28. while (str[j] != '\0' && str[j] != i + 48)
  29. {
  30. j++;
  31. }
  32. if (str[j] != i + 48)
  33. {
  34. to_return[index] = i + 48;
  35. index++;
  36. }
  37. i++;
  38. }
  39. to_return[index] = '\0';
  40. return (to_return);
  41. }
  42. /* ************************************************************************** */
  43. /* */
  44. /* ::: :::::::: */
  45. /* ft_check_argument.c :+: :+: :+: */
  46. /* +:+ +:+ +:+ */
  47. /* By: ibenchei <marvin@42.fr> +#+ +:+ +#+ */
  48. /* +#+#+#+#+#+ +#+ */
  49. /* Created: 2018/07/15 16:29:56 by ibenchei #+# #+# */
  50. /* Updated: 2018/07/15 17:46:38 by vsanchez ### ########.fr */
  51. /* */
  52. /* ************************************************************************** */
  53.  
  54. void ft_putstrerror(char *str);
  55.  
  56. int ft_check_argument(char **grid_argv)
  57. {
  58. int y;
  59. int x;
  60.  
  61. y = 0;
  62. while (y <= 8)
  63. {
  64. x = 0;
  65. while (x <= 8)
  66. {
  67. if (grid_argv[x][y] != '.')
  68. {
  69. if (!(grid_argv[x][y] <= '9' && grid_argv[x][y] >= '1'))
  70. {
  71. ft_putstrerror("Error\n");
  72. return (0);
  73. }
  74. }
  75. x++;
  76. }
  77. y++;
  78. }
  79. return (1);
  80. }
  81. /* ************************************************************************** */
  82. /* */
  83. /* ::: :::::::: */
  84. /* ft_check_column.c :+: :+: :+: */
  85. /* +:+ +:+ +:+ */
  86. /* By: vsanchez <marvin@42.fr> +#+ +:+ +#+ */
  87. /* +#+#+#+#+#+ +#+ */
  88. /* Created: 2018/07/14 17:12:16 by vsanchez #+# #+# */
  89. /* Updated: 2018/07/15 17:30:49 by vsanchez ### ########.fr */
  90. /* */
  91. /* ************************************************************************** */
  92.  
  93. #include <stdlib.h>
  94.  
  95. char *ft_anything_but(char *str);
  96.  
  97. char *ft_check_column(char **grid, int y)
  98. {
  99. char *values;
  100. int i_values;
  101. int i;
  102.  
  103. values = malloc(sizeof(char) * 10);
  104. i = 0;
  105. i_values = 0;
  106. while (i < 9)
  107. {
  108. if (grid[i][y] != '.')
  109. {
  110. values[i_values] = grid[i][y];
  111. i_values++;
  112. }
  113. i++;
  114. }
  115. values[i_values] = '\0';
  116. return (ft_anything_but(values));
  117. }
  118. /* ************************************************************************** */
  119. /* */
  120. /* ::: :::::::: */
  121. /* ft_check_if_possible.c :+: :+: :+: */
  122. /* +:+ +:+ +:+ */
  123. /* By: vsanchez <marvin@42.fr> +#+ +:+ +#+ */
  124. /* +#+#+#+#+#+ +#+ */
  125. /* Created: 2018/07/15 16:23:59 by vsanchez #+# #+# */
  126. /* Updated: 2018/07/15 18:14:36 by vsanchez ### ########.fr */
  127. /* */
  128. /* ************************************************************************** */
  129.  
  130. #include <stdlib.h>
  131.  
  132. char *ft_check_column(char **grid, int y);
  133.  
  134. char *ft_check_line(char **grid, int x);
  135.  
  136. char *ft_check_square(char **grid, int x, int y);
  137.  
  138. char *ft_check_if_possible1(char *line, char *column, char *square)
  139. {
  140. int i;
  141. int k;
  142. int j;
  143. int index;
  144. char *to_check;
  145.  
  146. to_check = malloc(sizeof(char) * 10);
  147. index = 0;
  148. i = -1;
  149. while (line[++i] != '\0')
  150. {
  151. j = 0;
  152. while (column[j] != '\0' && line[i] != column[j])
  153. j++;
  154. if (column[j] != '\0')
  155. {
  156. k = 0;
  157. while (square[k] != '\0' && line[i] != square[k])
  158. k++;
  159. if (square[k] != '\0')
  160. to_check[index++] = line[i];
  161. }
  162. }
  163. to_check[index] = '\0';
  164. return (to_check);
  165. }
  166.  
  167. char *ft_check_if_possible(char **grid, int x, int y)
  168. {
  169. return (ft_check_if_possible1(ft_check_line(grid, x),
  170. ft_check_column(grid, y),
  171. ft_check_square(grid, x, y)));
  172. }
  173. /* ************************************************************************** */
  174. /* */
  175. /* ::: :::::::: */
  176. /* ft_check_line.c :+: :+: :+: */
  177. /* +:+ +:+ +:+ */
  178. /* By: ibenchei <marvin@42.fr> +#+ +:+ +#+ */
  179. /* +#+#+#+#+#+ +#+ */
  180. /* Created: 2018/07/14 18:04:34 by ibenchei #+# #+# */
  181. /* Updated: 2018/07/15 17:31:30 by vsanchez ### ########.fr */
  182. /* */
  183. /* ************************************************************************** */
  184.  
  185. #include <stdlib.h>
  186.  
  187. char *ft_anything_but(char *str);
  188.  
  189. char *ft_check_line(char **grid, int x)
  190. {
  191. char *str;
  192. int i;
  193. int k;
  194.  
  195. i = 0;
  196. k = 0;
  197. str = malloc(sizeof(char) * 10);
  198. while (i <= 8)
  199. {
  200. if (grid[x][i] >= '0' && grid[x][i] <= '9')
  201. {
  202. str[k] = grid[x][i];
  203. k++;
  204. }
  205. i++;
  206. }
  207. str[k] = '\0';
  208. return (ft_anything_but(str));
  209. }
  210. /* ************************************************************************** */
  211. /* */
  212. /* ::: :::::::: */
  213. /* ft_check_square.c :+: :+: :+: */
  214. /* +:+ +:+ +:+ */
  215. /* By: chcoutur <marvin@42.fr> +#+ +:+ +#+ */
  216. /* +#+#+#+#+#+ +#+ */
  217. /* Created: 2018/07/14 12:39:35 by chcoutur #+# #+# */
  218. /* Updated: 2018/07/15 15:34:19 by vsanchez ### ########.fr */
  219. /* */
  220. /* ************************************************************************** */
  221.  
  222. #include <stdlib.h>
  223.  
  224. char *ft_anything_but(char *str);
  225.  
  226. char *ft_check_square(char **tab, int x, int y)
  227. {
  228. int i;
  229. int j;
  230. int k;
  231. char *str;
  232.  
  233. k = 0;
  234. i = y - (y % 3);
  235. j = x - (x % 3);
  236. str = malloc(sizeof(char) * 10);
  237. while (i <= y - (y % 3) + 2)
  238. {
  239. j = x - (x % 3);
  240. while (j <= x - (x % 3) + 2)
  241. {
  242. if (tab[j][i] != '.')
  243. {
  244. str[k] = tab[j][i];
  245. k++;
  246. }
  247. j++;
  248. }
  249. i++;
  250. }
  251. str[k] = '\0';
  252. return (ft_anything_but(str));
  253. }
  254. /* ************************************************************************** */
  255. /* */
  256. /* ::: :::::::: */
  257. /* ft_print.h :+: :+: :+: */
  258. /* +:+ +:+ +:+ */
  259. /* By: chcoutur <marvin@42.fr> +#+ +:+ +#+ */
  260. /* +#+#+#+#+#+ +#+ */
  261. /* Created: 2018/07/14 11:24:35 by chcoutur #+# #+# */
  262. /* Updated: 2018/07/14 13:37:57 by vsanchez ### ########.fr */
  263. /* */
  264. /* ************************************************************************** */
  265.  
  266. #ifndef FT_PRINT_H
  267. # define FT_PRINT_H
  268.  
  269. # include <unistd.h>
  270.  
  271. void ft_putchar(char c);
  272. void ft_putstr(char *str);
  273. void ft_putstrerror(char *str);
  274.  
  275. #endif
  276. /* ************************************************************************** */
  277. /* */
  278. /* ::: :::::::: */
  279. /* ft_putchar.c :+: :+: :+: */
  280. /* +:+ +:+ +:+ */
  281. /* By: chcoutur <marvin@42.fr> +#+ +:+ +#+ */
  282. /* +#+#+#+#+#+ +#+ */
  283. /* Created: 2018/07/14 11:30:48 by chcoutur #+# #+# */
  284. /* Updated: 2018/07/14 11:33:00 by chcoutur ### ########.fr */
  285. /* */
  286. /* ************************************************************************** */
  287.  
  288. #include "ft_print.h"
  289.  
  290. void ft_putchar(char c)
  291. {
  292. write(1, &c, 1);
  293. }
  294. /* ************************************************************************** */
  295. /* */
  296. /* ::: :::::::: */
  297. /* ft_putstr.c :+: :+: :+: */
  298. /* +:+ +:+ +:+ */
  299. /* By: chcoutur <marvin@42.fr> +#+ +:+ +#+ */
  300. /* +#+#+#+#+#+ +#+ */
  301. /* Created: 2018/07/14 11:33:27 by chcoutur #+# #+# */
  302. /* Updated: 2018/07/14 11:40:08 by chcoutur ### ########.fr */
  303. /* */
  304. /* ************************************************************************** */
  305.  
  306. #include "ft_print.h"
  307.  
  308. void ft_putstr(char *str)
  309. {
  310. int i;
  311.  
  312. i = 0;
  313. while (str[i])
  314. {
  315. ft_putchar(str[i]);
  316. if (i < 8)
  317. ft_putchar(' ');
  318. i++;
  319. }
  320. }
  321. /* ************************************************************************** */
  322. /* */
  323. /* ::: :::::::: */
  324. /* ft_pustrerror.c :+: :+: :+: */
  325. /* +:+ +:+ +:+ */
  326. /* By: chcoutur <marvin@42.fr> +#+ +:+ +#+ */
  327. /* +#+#+#+#+#+ +#+ */
  328. /* Created: 2018/07/14 12:09:30 by chcoutur #+# #+# */
  329. /* Updated: 2018/07/14 12:13:57 by chcoutur ### ########.fr */
  330. /* */
  331. /* ************************************************************************** */
  332.  
  333. #include "ft_print.h"
  334.  
  335. void ft_putstrerror(char *str)
  336. {
  337. int i;
  338.  
  339. i = 0;
  340. while (str[i])
  341. {
  342. ft_putchar(str[i]);
  343. i++;
  344. }
  345. }
  346. /* ************************************************************************** */
  347. /* */
  348. /* ::: :::::::: */
  349. /* ft_replace_if_possible.c :+: :+: :+: */
  350. /* +:+ +:+ +:+ */
  351. /* By: vsanchez <marvin@42.fr> +#+ +:+ +#+ */
  352. /* +#+#+#+#+#+ +#+ */
  353. /* Created: 2018/07/14 17:42:12 by vsanchez #+# #+# */
  354. /* Updated: 2018/07/15 16:56:19 by vsanchez ### ########.fr */
  355. /* */
  356. /* ************************************************************************** */
  357.  
  358. #include <stdlib.h>
  359.  
  360. int ft_strlen(char *str)
  361. {
  362. int i;
  363.  
  364. i = 0;
  365. while (str[i])
  366. i++;
  367. return (i);
  368. }
  369.  
  370. char *ft_check_if_possible(char **grid, int x, int y);
  371.  
  372. int ft_replace_if_possible(char **grid, int x, int y)
  373. {
  374. char *suspected;
  375. int i;
  376.  
  377. suspected = malloc(sizeof(char) * 10);
  378. if (grid[x][y] == '.')
  379. {
  380. suspected = ft_check_if_possible(grid, x, y);
  381. i = 0;
  382. while (suspected[i])
  383. i++;
  384. if (i == 1)
  385. {
  386. grid[x][y] = suspected[0];
  387. return (1);
  388. }
  389. }
  390. return (0);
  391. }
  392. /* ************************************************************************** */
  393. /* */
  394. /* ::: :::::::: */
  395. /* ft_solve_sudoku.c :+: :+: :+: */
  396. /* +:+ +:+ +:+ */
  397. /* By: vsanchez <marvin@42.fr> +#+ +:+ +#+ */
  398. /* +#+#+#+#+#+ +#+ */
  399. /* Created: 2018/07/15 11:50:55 by vsanchez #+# #+# */
  400. /* Updated: 2018/07/15 18:28:29 by vsanchez ### ########.fr */
  401. /* */
  402. /* ************************************************************************** */
  403.  
  404. #include "ft_print.h"
  405.  
  406. int ft_replace_if_possible(char **grid, int x, int y);
  407.  
  408. void ft_solve_checking(char **grid)
  409. {
  410. int x;
  411. int y;
  412. int modified;
  413.  
  414. x = 0;
  415. modified = 1;
  416. while (modified == 1)
  417. {
  418. modified = 0;
  419. while (x < 9)
  420. {
  421. y = 0;
  422. while (y < 9)
  423. {
  424. if (!modified)
  425. modified = ft_replace_if_possible(grid, x, y);
  426. else
  427. ft_replace_if_possible(grid, x, y);
  428. y++;
  429. }
  430. x++;
  431. }
  432. }
  433. }
  434.  
  435. int ft_not_full_grid(char **grid)
  436. {
  437. int x;
  438. int y;
  439.  
  440. y = 0;
  441. while (y <= 8)
  442. {
  443. x = 0;
  444. while (x <= 8)
  445. {
  446. if (grid[y][x] == '.')
  447. return (1);
  448. x++;
  449. }
  450. y++;
  451. }
  452. return (0);
  453. }
  454.  
  455. void ft_solve_sudoku(char **grid)
  456. {
  457. int max_loop;
  458.  
  459. max_loop = 0;
  460. while (ft_not_full_grid(grid) == 1 && max_loop < 100)
  461. {
  462. ft_solve_checking(grid);
  463. max_loop++;
  464. }
  465. }
  466. /* ************************************************************************** */
  467. /* */
  468. /* ::: :::::::: */
  469. /* main.c :+: :+: :+: */
  470. /* +:+ +:+ +:+ */
  471. /* By: chcoutur <marvin@42.fr> +#+ +:+ +#+ */
  472. /* +#+#+#+#+#+ +#+ */
  473. /* Created: 2018/07/14 11:10:38 by chcoutur #+# #+# */
  474. /* Updated: 2018/07/15 18:29:56 by vsanchez ### ########.fr */
  475. /* */
  476. /* ************************************************************************** */
  477.  
  478. #include "ft_print.h"
  479.  
  480. void ft_solve_sudoku(char **grid);
  481.  
  482. int ft_check_argument(char **grid_argv);
  483.  
  484. int ft_not_full_grid(char **grid);
  485.  
  486. int main(int argc, char **argv)
  487. {
  488. int i;
  489.  
  490. if (argc == 10)
  491. {
  492. if (ft_check_argument(&argv[1]))
  493. {
  494. ft_solve_sudoku(&argv[1]);
  495. i = 1;
  496. if (ft_not_full_grid(&argv[1]) == 1)
  497. ft_putstrerror("Error\n");
  498. else
  499. {
  500. while (argv[i])
  501. {
  502. ft_putstr(argv[i]);
  503. ft_putchar('\n');
  504. i++;
  505. }
  506. }
  507. }
  508. }
  509. return (0);
  510. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement