Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* ************************************************************************** */
- /* */
- /* ::: :::::::: */
- /* ft_anything_but.c :+: :+: :+: */
- /* +:+ +:+ +:+ */
- /* By: vsanchez <marvin@42.fr> +#+ +:+ +#+ */
- /* +#+#+#+#+#+ +#+ */
- /* Created: 2018/07/14 17:11:11 by vsanchez #+# #+# */
- /* Updated: 2018/07/15 12:17:06 by vsanchez ### ########.fr */
- /* */
- /* ************************************************************************** */
- #include <stdlib.h>
- char *ft_anything_but(char *str)
- {
- char *to_return;
- int j;
- int index;
- int i;
- i = 1;
- index = 0;
- to_return = malloc(sizeof(char) * 10);
- while (i < 10)
- {
- j = 0;
- while (str[j] != '\0' && str[j] != i + 48)
- {
- j++;
- }
- if (str[j] != i + 48)
- {
- to_return[index] = i + 48;
- index++;
- }
- i++;
- }
- to_return[index] = '\0';
- return (to_return);
- }
- /* ************************************************************************** */
- /* */
- /* ::: :::::::: */
- /* ft_check_argument.c :+: :+: :+: */
- /* +:+ +:+ +:+ */
- /* By: ibenchei <marvin@42.fr> +#+ +:+ +#+ */
- /* +#+#+#+#+#+ +#+ */
- /* Created: 2018/07/15 16:29:56 by ibenchei #+# #+# */
- /* Updated: 2018/07/15 17:46:38 by vsanchez ### ########.fr */
- /* */
- /* ************************************************************************** */
- void ft_putstrerror(char *str);
- int ft_check_argument(char **grid_argv)
- {
- int y;
- int x;
- y = 0;
- while (y <= 8)
- {
- x = 0;
- while (x <= 8)
- {
- if (grid_argv[x][y] != '.')
- {
- if (!(grid_argv[x][y] <= '9' && grid_argv[x][y] >= '1'))
- {
- ft_putstrerror("Error\n");
- return (0);
- }
- }
- x++;
- }
- y++;
- }
- return (1);
- }
- /* ************************************************************************** */
- /* */
- /* ::: :::::::: */
- /* ft_check_column.c :+: :+: :+: */
- /* +:+ +:+ +:+ */
- /* By: vsanchez <marvin@42.fr> +#+ +:+ +#+ */
- /* +#+#+#+#+#+ +#+ */
- /* Created: 2018/07/14 17:12:16 by vsanchez #+# #+# */
- /* Updated: 2018/07/15 17:30:49 by vsanchez ### ########.fr */
- /* */
- /* ************************************************************************** */
- #include <stdlib.h>
- char *ft_anything_but(char *str);
- char *ft_check_column(char **grid, int y)
- {
- char *values;
- int i_values;
- int i;
- values = malloc(sizeof(char) * 10);
- i = 0;
- i_values = 0;
- while (i < 9)
- {
- if (grid[i][y] != '.')
- {
- values[i_values] = grid[i][y];
- i_values++;
- }
- i++;
- }
- values[i_values] = '\0';
- return (ft_anything_but(values));
- }
- /* ************************************************************************** */
- /* */
- /* ::: :::::::: */
- /* ft_check_if_possible.c :+: :+: :+: */
- /* +:+ +:+ +:+ */
- /* By: vsanchez <marvin@42.fr> +#+ +:+ +#+ */
- /* +#+#+#+#+#+ +#+ */
- /* Created: 2018/07/15 16:23:59 by vsanchez #+# #+# */
- /* Updated: 2018/07/15 18:14:36 by vsanchez ### ########.fr */
- /* */
- /* ************************************************************************** */
- #include <stdlib.h>
- char *ft_check_column(char **grid, int y);
- char *ft_check_line(char **grid, int x);
- char *ft_check_square(char **grid, int x, int y);
- char *ft_check_if_possible1(char *line, char *column, char *square)
- {
- int i;
- int k;
- int j;
- int index;
- char *to_check;
- to_check = malloc(sizeof(char) * 10);
- index = 0;
- i = -1;
- while (line[++i] != '\0')
- {
- j = 0;
- while (column[j] != '\0' && line[i] != column[j])
- j++;
- if (column[j] != '\0')
- {
- k = 0;
- while (square[k] != '\0' && line[i] != square[k])
- k++;
- if (square[k] != '\0')
- to_check[index++] = line[i];
- }
- }
- to_check[index] = '\0';
- return (to_check);
- }
- char *ft_check_if_possible(char **grid, int x, int y)
- {
- return (ft_check_if_possible1(ft_check_line(grid, x),
- ft_check_column(grid, y),
- ft_check_square(grid, x, y)));
- }
- /* ************************************************************************** */
- /* */
- /* ::: :::::::: */
- /* ft_check_line.c :+: :+: :+: */
- /* +:+ +:+ +:+ */
- /* By: ibenchei <marvin@42.fr> +#+ +:+ +#+ */
- /* +#+#+#+#+#+ +#+ */
- /* Created: 2018/07/14 18:04:34 by ibenchei #+# #+# */
- /* Updated: 2018/07/15 17:31:30 by vsanchez ### ########.fr */
- /* */
- /* ************************************************************************** */
- #include <stdlib.h>
- char *ft_anything_but(char *str);
- char *ft_check_line(char **grid, int x)
- {
- char *str;
- int i;
- int k;
- i = 0;
- k = 0;
- str = malloc(sizeof(char) * 10);
- while (i <= 8)
- {
- if (grid[x][i] >= '0' && grid[x][i] <= '9')
- {
- str[k] = grid[x][i];
- k++;
- }
- i++;
- }
- str[k] = '\0';
- return (ft_anything_but(str));
- }
- /* ************************************************************************** */
- /* */
- /* ::: :::::::: */
- /* ft_check_square.c :+: :+: :+: */
- /* +:+ +:+ +:+ */
- /* By: chcoutur <marvin@42.fr> +#+ +:+ +#+ */
- /* +#+#+#+#+#+ +#+ */
- /* Created: 2018/07/14 12:39:35 by chcoutur #+# #+# */
- /* Updated: 2018/07/15 15:34:19 by vsanchez ### ########.fr */
- /* */
- /* ************************************************************************** */
- #include <stdlib.h>
- char *ft_anything_but(char *str);
- char *ft_check_square(char **tab, int x, int y)
- {
- int i;
- int j;
- int k;
- char *str;
- k = 0;
- i = y - (y % 3);
- j = x - (x % 3);
- str = malloc(sizeof(char) * 10);
- while (i <= y - (y % 3) + 2)
- {
- j = x - (x % 3);
- while (j <= x - (x % 3) + 2)
- {
- if (tab[j][i] != '.')
- {
- str[k] = tab[j][i];
- k++;
- }
- j++;
- }
- i++;
- }
- str[k] = '\0';
- return (ft_anything_but(str));
- }
- /* ************************************************************************** */
- /* */
- /* ::: :::::::: */
- /* ft_print.h :+: :+: :+: */
- /* +:+ +:+ +:+ */
- /* By: chcoutur <marvin@42.fr> +#+ +:+ +#+ */
- /* +#+#+#+#+#+ +#+ */
- /* Created: 2018/07/14 11:24:35 by chcoutur #+# #+# */
- /* Updated: 2018/07/14 13:37:57 by vsanchez ### ########.fr */
- /* */
- /* ************************************************************************** */
- #ifndef FT_PRINT_H
- # define FT_PRINT_H
- # include <unistd.h>
- void ft_putchar(char c);
- void ft_putstr(char *str);
- void ft_putstrerror(char *str);
- #endif
- /* ************************************************************************** */
- /* */
- /* ::: :::::::: */
- /* ft_putchar.c :+: :+: :+: */
- /* +:+ +:+ +:+ */
- /* By: chcoutur <marvin@42.fr> +#+ +:+ +#+ */
- /* +#+#+#+#+#+ +#+ */
- /* Created: 2018/07/14 11:30:48 by chcoutur #+# #+# */
- /* Updated: 2018/07/14 11:33:00 by chcoutur ### ########.fr */
- /* */
- /* ************************************************************************** */
- #include "ft_print.h"
- void ft_putchar(char c)
- {
- write(1, &c, 1);
- }
- /* ************************************************************************** */
- /* */
- /* ::: :::::::: */
- /* ft_putstr.c :+: :+: :+: */
- /* +:+ +:+ +:+ */
- /* By: chcoutur <marvin@42.fr> +#+ +:+ +#+ */
- /* +#+#+#+#+#+ +#+ */
- /* Created: 2018/07/14 11:33:27 by chcoutur #+# #+# */
- /* Updated: 2018/07/14 11:40:08 by chcoutur ### ########.fr */
- /* */
- /* ************************************************************************** */
- #include "ft_print.h"
- void ft_putstr(char *str)
- {
- int i;
- i = 0;
- while (str[i])
- {
- ft_putchar(str[i]);
- if (i < 8)
- ft_putchar(' ');
- i++;
- }
- }
- /* ************************************************************************** */
- /* */
- /* ::: :::::::: */
- /* ft_pustrerror.c :+: :+: :+: */
- /* +:+ +:+ +:+ */
- /* By: chcoutur <marvin@42.fr> +#+ +:+ +#+ */
- /* +#+#+#+#+#+ +#+ */
- /* Created: 2018/07/14 12:09:30 by chcoutur #+# #+# */
- /* Updated: 2018/07/14 12:13:57 by chcoutur ### ########.fr */
- /* */
- /* ************************************************************************** */
- #include "ft_print.h"
- void ft_putstrerror(char *str)
- {
- int i;
- i = 0;
- while (str[i])
- {
- ft_putchar(str[i]);
- i++;
- }
- }
- /* ************************************************************************** */
- /* */
- /* ::: :::::::: */
- /* ft_replace_if_possible.c :+: :+: :+: */
- /* +:+ +:+ +:+ */
- /* By: vsanchez <marvin@42.fr> +#+ +:+ +#+ */
- /* +#+#+#+#+#+ +#+ */
- /* Created: 2018/07/14 17:42:12 by vsanchez #+# #+# */
- /* Updated: 2018/07/15 16:56:19 by vsanchez ### ########.fr */
- /* */
- /* ************************************************************************** */
- #include <stdlib.h>
- int ft_strlen(char *str)
- {
- int i;
- i = 0;
- while (str[i])
- i++;
- return (i);
- }
- char *ft_check_if_possible(char **grid, int x, int y);
- int ft_replace_if_possible(char **grid, int x, int y)
- {
- char *suspected;
- int i;
- suspected = malloc(sizeof(char) * 10);
- if (grid[x][y] == '.')
- {
- suspected = ft_check_if_possible(grid, x, y);
- i = 0;
- while (suspected[i])
- i++;
- if (i == 1)
- {
- grid[x][y] = suspected[0];
- return (1);
- }
- }
- return (0);
- }
- /* ************************************************************************** */
- /* */
- /* ::: :::::::: */
- /* ft_solve_sudoku.c :+: :+: :+: */
- /* +:+ +:+ +:+ */
- /* By: vsanchez <marvin@42.fr> +#+ +:+ +#+ */
- /* +#+#+#+#+#+ +#+ */
- /* Created: 2018/07/15 11:50:55 by vsanchez #+# #+# */
- /* Updated: 2018/07/15 18:28:29 by vsanchez ### ########.fr */
- /* */
- /* ************************************************************************** */
- #include "ft_print.h"
- int ft_replace_if_possible(char **grid, int x, int y);
- void ft_solve_checking(char **grid)
- {
- int x;
- int y;
- int modified;
- x = 0;
- modified = 1;
- while (modified == 1)
- {
- modified = 0;
- while (x < 9)
- {
- y = 0;
- while (y < 9)
- {
- if (!modified)
- modified = ft_replace_if_possible(grid, x, y);
- else
- ft_replace_if_possible(grid, x, y);
- y++;
- }
- x++;
- }
- }
- }
- int ft_not_full_grid(char **grid)
- {
- int x;
- int y;
- y = 0;
- while (y <= 8)
- {
- x = 0;
- while (x <= 8)
- {
- if (grid[y][x] == '.')
- return (1);
- x++;
- }
- y++;
- }
- return (0);
- }
- void ft_solve_sudoku(char **grid)
- {
- int max_loop;
- max_loop = 0;
- while (ft_not_full_grid(grid) == 1 && max_loop < 100)
- {
- ft_solve_checking(grid);
- max_loop++;
- }
- }
- /* ************************************************************************** */
- /* */
- /* ::: :::::::: */
- /* main.c :+: :+: :+: */
- /* +:+ +:+ +:+ */
- /* By: chcoutur <marvin@42.fr> +#+ +:+ +#+ */
- /* +#+#+#+#+#+ +#+ */
- /* Created: 2018/07/14 11:10:38 by chcoutur #+# #+# */
- /* Updated: 2018/07/15 18:29:56 by vsanchez ### ########.fr */
- /* */
- /* ************************************************************************** */
- #include "ft_print.h"
- void ft_solve_sudoku(char **grid);
- int ft_check_argument(char **grid_argv);
- int ft_not_full_grid(char **grid);
- int main(int argc, char **argv)
- {
- int i;
- if (argc == 10)
- {
- if (ft_check_argument(&argv[1]))
- {
- ft_solve_sudoku(&argv[1]);
- i = 1;
- if (ft_not_full_grid(&argv[1]) == 1)
- ft_putstrerror("Error\n");
- else
- {
- while (argv[i])
- {
- ft_putstr(argv[i]);
- ft_putchar('\n');
- i++;
- }
- }
- }
- }
- return (0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement