1
0

first commit

This commit is contained in:
KeyZox
2023-08-06 20:12:38 +02:00
parent 1fe062c970
commit eb6b113e51
125 changed files with 2987 additions and 0 deletions

138
ended/rush01/ex00/checks.c Normal file
View File

@ -0,0 +1,138 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* checks.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: kjimenez <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/24 14:33:44 by kjimenez #+# #+# */
/* Updated: 2022/07/24 15:14:35 by kjimenez ### ########.fr */
/* */
/* ************************************************************************** */
int left_to_right(int **arr_answer, int row, int expected)
{
int i;
int j;
int count;
int if_increasing;
i = 0;
count = 1;
while (arr_answer[row][i] <= 3)
{
j = i;
if_increasing = i;
while (j > 0)
{
if (arr_answer[row][i] > arr_answer[row][j - 1])
if_increasing--;
j--;
}
if (j == if_increasing)
count++;
i++;
}
return (count == expected);
}
int up_to_down(int **arr_answer, int col, int expected)
{
int i;
int j;
int count;
int if_increasing;
i = 0;
count = 1;
while (i < 4 && arr_answer[i][col] <= 3)
{
j = i;
if_increasing = i;
while (j > 0)
{
if (arr_answer[i][col] > arr_answer[j - 1][col])
if_increasing--;
j--;
}
if (j == if_increasing)
count++;
i++;
}
return (count == expected);
}
int right_to_left(int **arr_answer, int row, int expected)
{
int i;
int j;
int count;
int if_increasing;
i = 3;
count = 1;
while (arr_answer[row][i] <= 3)
{
j = i;
if_increasing = i;
while (j < 3)
{
if (arr_answer[row][i] > arr_answer[row][j + 1])
if_increasing++;
j++;
}
if (j == if_increasing)
count++;
i--;
}
return (count == expected);
}
int down_to_up(int **arr_answer, int col, int expected)
{
int i;
int j;
int count;
int if_increasing;
i = 3;
count = 1;
while (i > 0 && arr_answer[i][col] <= 3)
{
j = i;
if_increasing = i;
while (j < 3)
{
if (arr_answer[i][col] > arr_answer[j + 1][col])
if_increasing++;
j++;
}
if (j == if_increasing)
count++;
i--;
}
return (count == expected);
}
int check_duplicates(int **board)
{
int i;
int j;
int k;
i = -1;
while (++i < 4)
{
j = -1;
while (++j < 4)
{
k = j;
while (++k < 4)
{
if (board[j][i] == board[k][i])
return (0);
}
}
}
return (1);
}

View File

@ -0,0 +1,22 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* checks.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: kjimenez <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/24 14:45:31 by kjimenez #+# #+# */
/* Updated: 2022/07/24 15:14:11 by kjimenez ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef CHECKS_H
# define CHECKS_H
int left_to_right(int **arr_answer, int row, int expected);
int up_to_down(int **arr_answer, int col, int expected);
int right_to_left(int **arr_answer, int row, int expected);
int down_to_up(int **arr_answer, int col, int expected);
int check_duplicates(int **board);
#endif

36
ended/rush01/ex00/lib.c Normal file
View File

@ -0,0 +1,36 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* lib.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: kjimenez <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/24 14:26:42 by kjimenez #+# #+# */
/* Updated: 2022/07/24 16:16:26 by kjimenez ### ########.fr */
/* */
/* ************************************************************************** */
#include <unistd.h>
void ft_putchar(char c)
{
write(1, &c, 1);
}
void ft_putstr(char *str)
{
int i;
i = -1;
while (str[++i])
write(1, &str[i], 1);
}
void ft_swap(int *i, int *j)
{
int temp;
temp = *i;
*i = *j;
*j = temp;
}

20
ended/rush01/ex00/lib.h Normal file
View File

@ -0,0 +1,20 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* lib.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: kjimenez <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/24 14:44:57 by kjimenez #+# #+# */
/* Updated: 2022/07/24 14:44:59 by kjimenez ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef LIB_H
# define LIB_H
void ft_putchar(char c);
void ft_putstr(char *str);
void ft_swap(int *i, int *j);
#endif

137
ended/rush01/ex00/main.c Normal file
View File

@ -0,0 +1,137 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: kjimenez <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/23 18:04:54 by kjimenez #+# #+# */
/* Updated: 2022/07/24 13:58:55 by kjimenez ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdlib.h>
#include "checks.h"
#include "parser.h"
#include "permutations.h"
#include "lib.h"
void print_board(int **board, int count)
{
int i;
int j;
if (count == 4)
{
i = -1;
while (++i < 4)
{
j = -1;
while (++j < 4)
{
ft_putchar(board[i][j] + '0');
if (j < 3)
ft_putchar(' ');
}
ft_putchar('\n');
}
exit(0);
}
free(board);
}
int **create_board(int *permut_1, int *permut_2, int *permut_3, int *permut_4)
{
int **board;
int i;
board = (int **) malloc(4 * sizeof(int *));
i = -1;
while (++i < 4)
board[i] = (int *) malloc(4 * sizeof(int));
i = -1;
while (++i < 4)
board[0][i] = permut_1[i];
i = -1;
while (++i < 4)
board[1][i] = permut_2[i];
i = -1;
while (++i < 4)
board[2][i] = permut_3[i];
i = -1;
while (++i < 4)
board[3][i] = permut_4[i];
return (board);
}
void solve_board(int **board, int **arr_expected)
{
int i;
int count;
i = 0;
count = 0;
if (check_duplicates(board))
{
while (i < 4)
{
if (up_to_down(board, i, arr_expected[0][i])
&& down_to_up(board, i, arr_expected[1][i])
&& left_to_right(board, i, arr_expected[2][i])
&& right_to_left(board, i, arr_expected[3][i]))
count++;
i++;
}
}
print_board(board, count);
}
void solve_all_boards(int **permutations, int **arr_expected)
{
int line_1;
int line_2;
int line_3;
int line_4;
line_1 = -1;
while (++line_1 < 24)
{
line_2 = -1;
while (++line_2 < 24)
{
line_3 = -1;
while (++line_3 < 24)
{
line_4 = -1;
while (++line_4 < 24)
{
solve_board(create_board(permutations[line_1],
permutations[line_2], permutations[line_3],
permutations[line_4]), arr_expected);
}
}
}
}
ft_putstr("Error\n");
}
int main(int argc, char **argv)
{
int **permutations;
int **check;
if (argc == 2)
{
if (check_input(argv[1]))
{
check = str_to_tab(argv[1]);
permutations = generate_permutations();
solve_all_boards(permutations, check);
free(permutations);
free(check);
return (0);
}
}
ft_putstr("Error\n");
return (0);
}

View File

@ -0,0 +1,61 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* parser.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: kjimenez <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/24 14:28:23 by kjimenez #+# #+# */
/* Updated: 2022/07/24 14:28:26 by kjimenez ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdlib.h>
int check_input(char *str)
{
int i;
i = -1;
while (str[++i])
{
if (i % 2 == 1)
{
if (str[i] != ' ')
return (0);
}
else if (i % 2 == 0)
{
if (str[i] < '1' || str[i] > '4')
return (0);
}
}
if (i != 31)
return (0);
return (1);
}
int **str_to_tab(char *str)
{
int i;
int j;
int k;
int **check;
i = -1;
check = (int **) malloc(4 * sizeof(int *));
while (++i < 4)
check[i] = (int *) malloc(4 * sizeof(int));
i = -1;
k = 0;
while (++i < 4)
{
j = -1;
while (++j < 4)
{
check[i][j] = str[k] - '0';
k += 2;
}
}
return (check);
}

View File

@ -0,0 +1,19 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* parser.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: kjimenez <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/24 14:45:05 by kjimenez #+# #+# */
/* Updated: 2022/07/24 14:45:08 by kjimenez ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef PARSER_H
# define PARSER_H
int check_input(char *str);
int **str_to_tab(char *str);
#endif

View File

@ -0,0 +1,67 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* permutations.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: kjimenez <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/24 14:35:22 by kjimenez #+# #+# */
/* Updated: 2022/07/24 14:35:25 by kjimenez ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdlib.h>
#include "lib.h"
int perm_next(int n[])
{
int tail;
int i;
int j;
i = 3;
while (i > 0 && n[i - 1] >= n[i])
i--;
tail = i;
if (tail > 0)
{
j = 3;
while (j > tail && n[j] <= n[tail - 1])
j--;
ft_swap(&n[tail - 1], &n[j]);
}
i = tail;
j = 3;
while (i < j)
{
ft_swap(&n[i], &n[j]);
i++;
j--;
}
return (tail != 0);
}
int **generate_permutations(void)
{
int **permut;
int perm[4];
int i;
int j;
permut = (int **) malloc(24 * sizeof(int *));
j = 0;
i = 0;
while (++i <= 4)
perm[i - 1] = i;
while (1)
{
permut[j] = (int *) malloc(4 * sizeof(int));
i = -1;
while (++i < 4)
permut[j][i] = perm[i];
j++;
if (!perm_next(perm))
break ;
}
return (permut);
}

View File

@ -0,0 +1,19 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* permutations.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: kjimenez <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/24 14:45:13 by kjimenez #+# #+# */
/* Updated: 2022/07/24 15:50:47 by kjimenez ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef PERMUTATIONS_H
# define PERMUTATIONS_H
int **generate_permutations(void);
int perm_next(int n[], int count);
#endif