diff options
| author | Charles <sircharlesaze@gmail.com> | 2019-07-15 08:15:37 +0200 |
|---|---|---|
| committer | Charles <sircharlesaze@gmail.com> | 2019-07-15 08:15:37 +0200 |
| commit | 3b9a1d7dcc5683b962f2bf24795e80e1c449cd1f (patch) | |
| tree | 25b02c02f5140dbefbabd7720f292d8be3d5cc51 /rush01_6x6_try | |
| parent | c2bf9fcefbb4453cee271ccd1af9674ad2f3a181 (diff) | |
| download | piscine-3b9a1d7dcc5683b962f2bf24795e80e1c449cd1f.tar.gz piscine-3b9a1d7dcc5683b962f2bf24795e80e1c449cd1f.tar.bz2 piscine-3b9a1d7dcc5683b962f2bf24795e80e1c449cd1f.zip | |
c07 passed, c08 in progress, rush01(+ 6x6 try)
Diffstat (limited to 'rush01_6x6_try')
| -rw-r--r-- | rush01_6x6_try/ex00/board.c | 77 | ||||
| -rw-r--r-- | rush01_6x6_try/ex00/helper.c | 46 | ||||
| -rw-r--r-- | rush01_6x6_try/ex00/include.h | 50 | ||||
| -rw-r--r-- | rush01_6x6_try/ex00/main.c | 56 | ||||
| -rw-r--r-- | rush01_6x6_try/ex00/solve.c | 146 | ||||
| -rw-r--r-- | rush01_6x6_try/ex00/square.c | 134 |
6 files changed, 509 insertions, 0 deletions
diff --git a/rush01_6x6_try/ex00/board.c b/rush01_6x6_try/ex00/board.c new file mode 100644 index 0000000..573d459 --- /dev/null +++ b/rush01_6x6_try/ex00/board.c @@ -0,0 +1,77 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* board.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/07/13 13:23:16 by cacharle #+# #+# */ +/* Updated: 2019/07/14 13:31:15 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "include.h" + +/*void fill_clue1n(t_board board, t_views views)*/ +/*{*/ + /*int i;*/ + /*int j;*/ + + /*i = 0;*/ + /*while (i < 4)*/ + /*{*/ + /*j = 0;*/ + /*while (j < 4)*/ + /*{*/ + /*if (views[i][j] == 1)*/ + /*line_at(i);*/ + + /*}*/ + /*}*/ +/*}*/ + +/*int *line_at(t_board board, t_view_side side, int line_index);*/ +/*{*/ + /*int *line;*/ + /*int i;*/ + + /*line = malloc(sizeof(int) * 4);*/ + /*i = 0*/ + /*if (side == row_left)*/ + /*while (i < 4)*/ + /*line[i] = board[line_index][i++];*/ + /*if (side == row_right)*/ + /*while (i < 4)*/ + /*line[i] = board[line_index][size - i++];*/ + /*if (side == col_up)*/ + /*while (i < 4)*/ + /*line[i] = board[line_index][i++];*/ + + + +/*}*/ + +/*void set_at(t_board board, t_view_side side, int side_index, int line_index, int value)*/ +/*{*/ + /*if (side == row_left)*/ + /*board[side_index][line_index] = value;*/ + /*if (side == row_right)*/ + /*board[side_index][3 - line_index] = value;*/ + /*if (side == col_up)*/ + /*board[line_index][side_index] = value;*/ + /*if (side == col_down)*/ + /*board[3 - line_index][side_index] = value;*/ +/*}*/ + +int get_with_view(t_board board, t_view_point view, int view_line, int line_index) +{ + if (view == row_left) + return board.self[view_line][line_index]; + if (view == row_right) + return board.self[view_line][board.size - 1 - line_index]; + if (view == col_up) + return board.self[line_index][view_line]; + if (view == col_down) + return board.self[board.size - 1 - line_index][view_line]; + return (-1); +} diff --git a/rush01_6x6_try/ex00/helper.c b/rush01_6x6_try/ex00/helper.c new file mode 100644 index 0000000..eb391ea --- /dev/null +++ b/rush01_6x6_try/ex00/helper.c @@ -0,0 +1,46 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* helper.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/07/13 12:52:42 by cacharle #+# #+# */ +/* Updated: 2019/07/14 10:36:19 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include <unistd.h> + +void ft_putchar(char c) +{ + write(1, &c, 1); +} + +void ft_putnbr(int nb) +{ + unsigned int p_nb; + + p_nb = nb; + if (nb < 0) + { + write(1, "-", 1); + p_nb = -nb; + } + if (p_nb > 9) + ft_putnbr(p_nb / 10); + ft_putchar(p_nb % 10 + '0'); +} + +int strlen_ignore_space(char *str) +{ + int counter; + + counter = 0; + while (*str) + { + if (*str != ' ') + counter++; + str++; + } +} diff --git a/rush01_6x6_try/ex00/include.h b/rush01_6x6_try/ex00/include.h new file mode 100644 index 0000000..85f595f --- /dev/null +++ b/rush01_6x6_try/ex00/include.h @@ -0,0 +1,50 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* include.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/07/13 12:07:59 by cacharle #+# #+# */ +/* Updated: 2019/07/14 13:22:42 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef INCLUDE_H +# define INCLUDE_H + +//typedef int** t_board; +//typedef int** t_board; + +typedef enum +{ + col_up, + col_down, + row_left, + row_right +} t_view_point; +typedef struct +{ + int size; + int **self; +} t_square; +typedef t_square t_board; +typedef t_square t_views; + +int solve(t_board board, t_views views); +int find_next_unassigned(t_board board, int *row, int *col); +int is_alone(t_board board, int bfloor, int row, int col); +int check_viewpoints(t_board board, t_views views); +int check_line(t_board board, t_view_point view, int view_line, + int building_viewed); +t_views parse_arg(char *arg); +int **init_square(int size); +int **dup_square(int **square); +void destroy_square(int **square); +void print_square(t_board board); +int get_with_view(t_board board, t_view_point view, int view_line, + int line_index); +void ft_putchar(char c); +void ft_putnbr(int nb); + +#endif diff --git a/rush01_6x6_try/ex00/main.c b/rush01_6x6_try/ex00/main.c new file mode 100644 index 0000000..4e4c239 --- /dev/null +++ b/rush01_6x6_try/ex00/main.c @@ -0,0 +1,56 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* main.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/07/13 11:30:35 by cacharle #+# #+# */ +/* Updated: 2019/07/14 10:24:10 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include <unistd.h> +#include "include.h" + +#include <string.h> +#include <stdio.h> +int main(int argc, char ** argv) +{ + t_views views; + t_board board; + + if (argc != 2) + { + write(1, "Error\n", 6); + return (0); + } + /*int tb[4][4] = {*/ + /*{1, 2, 3, 4},*/ + /*{2, 3, 4, 1},*/ + /*{3, 4, 1, 2},*/ + /*{4, 1, 2, 3}*/ + /*};*/ + views = parse_arg(argv[1]); + /*print_square(views);*/ + board = init_square(4); + /*memcpy(board[0], tb[0], sizeof(int) * 4);*/ + /*memcpy(board[1], tb[1], sizeof(int) * 4);*/ + /*memcpy(board[2], tb[2], sizeof(int) * 4);*/ + /*memcpy(board[3], tb[3], sizeof(int) * 4);*/ + /*print_square(board);*/ + /*printf("check views %d\n", check_viewpoints(board, views));*/ + /*print_square(board);*/ + /*printf("%d\n", get_with_view(board, col_down, 0, 2));*/ + /*ft_putchar('\n');*/ + solve(board, views); + /*ft_putchar('\n');*/ + /*board = init_square(4);*/ + /*board[0][0] = 2;*/ + /*ft_putchar('\n');*/ + /*print_square(board);*/ + /*solve(board);*/ + /*ft_putchar('\n');*/ + /*print_square(board);*/ + return (0); +} diff --git a/rush01_6x6_try/ex00/solve.c b/rush01_6x6_try/ex00/solve.c new file mode 100644 index 0000000..d08e4aa --- /dev/null +++ b/rush01_6x6_try/ex00/solve.c @@ -0,0 +1,146 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* solve.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/07/13 14:25:32 by cacharle #+# #+# */ +/* Updated: 2019/07/14 13:30:12 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "include.h" +#include <stdio.h> + +#define UNASSIGNED 0 +#define TRUE 1 +#define FALSE 0 + +/* +** Find all the sudoku 4x4 grid recursively +** print the one that checks out with the given viewpoints +*/ + +int solve(t_board board, t_views views) +{ + int row; + int col; + int i; + t_board board_clone; + + if (!find_next_unassigned(board, &row, &col)) + return (TRUE); + i = 1; + while (i <= size) + { + board_clone = dup_square(board); + if (is_alone(board_clone, row, col, i)) + { + board_clone.self[row][col] = i; + if (solve(board_clone, views)) + { + if (check_viewpoints(board_clone, views)) + { + print_square(board_clone); + return (TRUE); + } + return (FALSE); + } + } + destroy_square(board_clone); + i++; + } + return (FALSE); +} + +/* +** Move `row` and `col` to the next unassigned(== 0) position +** returns FALSE if the board is already filled with number, TRUE otherwise +*/ + +int find_next_unassigned(t_board board, int *row, int *col) +{ + *row = 0; + while (*row < board.size) + { + *col = 0; + while (*col < board.size) + { + if (board.self[*row][*col] == UNASSIGNED) + return (TRUE); + (*col)++; + } + (*row)++; + } + return (FALSE); +} + +/* +** Check if `building_floor` is already is unique in the row and column +*/ + +int is_alone(t_board board, int row, int col, int building_floor) +{ + int i; + + i = 0; + while (i < board.size) + if (board.self[row][i++] == building_floor) + return (FALSE); + i = 0; + while (i < board.size) + if (board.self[i++][col] == building_floor) + return (FALSE); + return (TRUE); +} + +/* +** Checks if the grid is valid according to the viewpoints +*/ + +int check_viewpoints(t_board board, t_views views) +{ + t_view_point view; + int j; + + view = col_up; + while (view <= row_right) + { + j = 0; + while (j < board.size) + { + if (!check_line(board, view, j, views[view][j])) + return (FALSE); + j++; + } + view++; + } + return (TRUE); +} + +/* +** Returns TRUE if the number buildings viewed +** with some viewpoint on a line are equal to `building_viewed`. +*/ + +int check_line(t_board board, t_view_point view, int view_line, + int building_viewed) +{ + int i; + int tmp_building_floor; + + i = 0; + while (i < board.size) + { + tmp_building_floor = get_with_view(board, view, view_line, i); + if (tmp_building_floor == UNASSIGNED) + return (FALSE); + building_viewed--; + while (i + 1 < board.size && tmp_building_floor > get_with_view( + board, view, view_line, i + 1)) + i++; + i++; + } + return (building_viewed == 0); +} diff --git a/rush01_6x6_try/ex00/square.c b/rush01_6x6_try/ex00/square.c new file mode 100644 index 0000000..0260f73 --- /dev/null +++ b/rush01_6x6_try/ex00/square.c @@ -0,0 +1,134 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* helpers.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/07/13 12:31:20 by cacharle #+# #+# */ +/* Updated: 2019/07/14 13:26:55 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include <stdlib.h> +#include "include.h" + +/* +** Parse the program's first argument where each row is in order +** col up, col down, row left, row right +*/ + +t_views parse_arg(char *arg) +{ + int i; + int j; + t_views views; + + views = init_square(4); + views.size = 4; + i = 0; + j = 0; + while (i < 16) + { + if (arg[i] == ' ') + { + arg++; + continue; + } + views.self[j][i % 4] = arg[i] - '0'; + if (i % 4 == 3) + j++; + i++; + } + return (views); +} + +/* +** Allocate memory for a 2D array(square) of `size` +** initialize each cell to 0. +*/ + +t_square init_square(int size) +{ + int i; + int j; + t_square square; + + square.self = malloc(sizeof(int*) * size); + square.size = size + i = 0; + while (i < size) + { + square.self[i] = malloc(sizeof(int) * size); + j = 0; + while (j < size) + square.self[i][j++] = 0; + i++; + } + return (square); +} + +/* +** Duplicate the square, create an empty clone +** and copy each element of `square` in it. +*/ + +t_square dup_square(t_square square) +{ + t_square clone; + int i; + int j; + + clone = init_square(4); + i = 0; + while (i < 4) + { + j = 0; + while (j < 4) + { + clone.self[i][j] = square.self[i][j]; + j++; + } + i++; + } + return (clone); +} + +/* +** Free each row of the square and the square itself. +*/ + +void destroy_square(int **square) +{ + int i; + + i = 0; + while (i < 4) + free(square.self[i++]); + free(square.self); +} + +/* +** Print each row followed by a line break +** and each element of the row but the last followed by a space. +*/ + +void print_square(t_square square) +{ + int i; + int j; + + i = 0; + while (i < square.size) + { + j = 0; + while (j < square.size) + { + ft_putnbr(square.self[i][j++]); + if (j != 4) + ft_putchar(' '); + } + ft_putchar('\n'); + i++; + } +} |
