HUUUUUUUUUUUUUUUUUUUUUUGE

This commit is contained in:
Arian Karami 2023-03-26 20:51:57 +02:00
parent 8e2c3ab995
commit faaa653c17
15 changed files with 134 additions and 266 deletions

View File

@ -4,33 +4,32 @@ include config.mk
NAME?=rush-01
SRC=gen_util.c handle_input.c handle_mem.c main.c print_array.c
SRC=gen_util.c handle_input.c handle_mem.c main.c backtrack.c
HEADERS=
_OBJ=$(SRC:.c=.o)
OBJ=$(patsubst %,$(OBJDIR)/%,$(_OBJ))
DEPS=$(patsubst %,$(INCDIR)/%,$(HEADERS))
.PHONY: clean install uninstall fclean all re makedirs
.PHONY: clean install uninstall fclean all re
all: $(BUILDDIR)/$(NAME)
build/rush-01:
$(BUILDDIR)/$(NAME): $(OBJ)
$(CC) $(LDFLAGS) -o $@ $^
$(OBJDIR)/%.o: %.c $(DEPS)
norminette $<
# norminette $<
$(CC) $(CFLAGS) -o $@ -c $<
clean:
$(RM) $(OBJ)
$(RM) $(INCDIR)/*~
$(RM) *~
$(RMDIR) $(OBJDIR)
fclean: clean
$(RM) $(BUILDDIR)/*
$(RMDIR) $(BUILDDIR)
install: all
$(MKDIR) $(DESTDIR)$(PREFIX)/bin

View File

@ -1,17 +1,29 @@
#include "helpers.h"
#include "backtrack.h"
#include <stdio.h>
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* backtrack.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: akarami <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/26 19:50:55 by akarami #+# #+# */
/* Updated: 2023/03/26 20:38:16 by akarami ### ########.fr */
/* */
/* ************************************************************************** */
int is_solved(int board[SIZE][SIZE])
#include <stdio.h>
#include <stdlib.h>
#include "include/backtrack.h"
int is_solved(int **board, int n)
{
int i;
int j;
i = 0;
while (i < SIZE)
while (i < n)
{
j = 0;
while (j < SIZE)
while (j < n)
{
if (!board[i][j])
return (0);
@ -22,49 +34,38 @@ int is_solved(int board[SIZE][SIZE])
return (1);
}
int next_cell_pos(int board[SIZE][SIZE])
int *next_cell_pos(int **board, int n)
{
int pos;
int i;
int *pos;
pos = 0;
while (board[pos / SIZE][pos % SIZE])
pos++;
pos = malloc(2 * 4);
i = 0;
while (board[i / n][i % n])
i++;
pos[0] = i / n;
pos[1] = i % n;
return (pos);
}
void *get_candidates(int board[SIZE][SIZE], int next_cell)
{
return (0);
}
// void *get_candidates(int **board, int n, int next_cell)
// {
// return (0);
// }
int backtrack(int board[SIZE][SIZE], int clues[SIZE * 4])
int backtrack(int **board, int n, int *borders)
{
int next_cell;
void *candidates;
int *next_cell;
// void *candidates;
if (is_solved(board))
borders++;
if (is_solved(board, n))
return (1);
next_cell = next_cell_pos(board);
candidates = get_candidates(board, next_cell);
printf("%d\n", candidates[0];
next_cell = next_cell_pos(board, n);
printf("x:%d\n", next_cell[0]);
printf("y:%d\n", next_cell[1]);
free(next_cell);
// candidates = get_candidates(board, n, next_cell);
// printf("%d\n", candidates[0]);
return (0);
}
void print_board(int board[SIZE][SIZE])
{
int i;
int j;
i = -1;
while (++i < SIZE)
{
j = -1;
while (++j < SIZE)
{
ft_putchar(board[i][j] + '0');
ft_putchar(' ');
}
ft_putchar('\b');
ft_putchar('\n');
}
}

View File

@ -17,7 +17,7 @@ INCS=-I$(INCDIR)
CFLAGS=
CFLAGS+=-Wall
CFLAGS+=-Wextra
#CFLAGS+=-Werror
CFLAGS+=-Werror
CFLAGS+=$(INCS)
LDFLAGS=
@ -26,7 +26,6 @@ CC=cc
# other executables
RM=/bin/rm -f
RMDIR=/bin/rmdir --ignore-fail-on-non-empty
MKDIR=/bin/mkdir -p
CP=/bin/cp -f
CHMOD=/bin/chmod

View File

@ -1,37 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* error_check.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: akarami <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/26 18:49:07 by akarami #+# #+# */
/* Updated: 2023/03/26 18:49:08 by akarami ### ########.fr */
/* */
/* ************************************************************************** */
#include "helpers.h"
int is_valid_input(int argc, char **argv)
{
char *input;
int valid_input_len;
int i;
if (argc != 2)
return (0);
input = argv[1];
valid_input_len = SIZE * 4 * 2 - 1;
if (ft_strlen(input) != valid_input_len)
return (0);
i = 0;
while (i < valid_input_len)
{
if (i % 2 == 0 && (input[i] < '1' || input[i] > '4'))
return (0);
if (i % 2 == 1 && input[i] != ' ')
return (0);
i++;
}
return (1);
}

View File

@ -6,11 +6,12 @@
/* By: smatthes <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/25 19:41:44 by smatthes #+# #+# */
/* Updated: 2023/03/26 06:13:02 by tischmid ### ########.fr */
/* Updated: 2023/03/26 20:31:24 by akarami ### ########.fr */
/* */
/* ************************************************************************** */
#include <unistd.h>
#include "include/gen_util.h"
int str_len(char *str)
{
@ -48,3 +49,29 @@ int *find_position(int **board, int n, int *pos)
}
return (0);
}
void ft_putchar(char c)
{
write(1, &c, 1);
}
void print_board(int **board, int n)
{
int x;
int y;
x = 0;
while (x < n)
{
y = 0;
while (y < n)
{
ft_putchar(board[x][y] + '0');
if (y != n)
ft_putchar(' ');
++y;
}
ft_putchar('\n');
++x;
}
}

View File

@ -6,11 +6,12 @@
/* By: smatthes <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/25 18:58:21 by smatthes #+# #+# */
/* Updated: 2023/03/26 07:55:41 by tischmid ### ########.fr */
/* Updated: 2023/03/26 19:49:33 by akarami ### ########.fr */
/* */
/* ************************************************************************** */
#include "include/handle_input.h"
#include "include/gen_util.h"
// handle sum of opposite inputs
int handle_input(int argc, char **argv, int *borders, int n)

View File

@ -6,23 +6,23 @@
/* By: smatthes <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/25 19:45:42 by smatthes #+# #+# */
/* Updated: 2023/03/26 07:58:12 by tischmid ### ########.fr */
/* Updated: 2023/03/26 20:36:47 by akarami ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdlib.h>
#include <stdio.h>
#include "include/handle_mem.h"
int **alloc_mem(int **board, int n)
int **alloc_mem(int n)
{
int i;
int **board;
board = malloc(n * 8);
i = -1;
while (++i < n)
{
board[i] = malloc(4);
}
return (board);
}

View File

@ -1,85 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* helpers.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: akarami <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/26 18:49:56 by akarami #+# #+# */
/* Updated: 2023/03/26 18:50:40 by akarami ### ########.fr */
/* */
/* ************************************************************************** */
#include <unistd.h>
#include "helpers.h"
void ft_putchar(char c)
{
write(1, &c, 1);
}
void init_board(int board[SIZE][SIZE])
{
int i;
int j;
i = -1;
while (++i < SIZE)
{
j = -1;
while (++j < SIZE)
board[i][j] = 0;
}
}
int ft_puterr(int exit_code)
{
write(1, "Error\n", 6);
return (exit_code);
}
int ft_strlen(char *str)
{
int i;
i = 0;
while (str[i])
i++;
return (i);
}
void get_clues(char *input, int *border)
{
int i;
int len;
len = ft_strlen(input);
i = 0;
while (i < len)
{
*border++ = input[i] - '0';
i += 2;
}
}
/*
board[0][0] = 1;
board[0][1] = 2;
board[0][2] = 3;
board[0][3] = 4;
board[1][0] = 2;
board[1][1] = 3;
board[1][2] = 4;
board[1][3] = 1;
board[2][0] = 3;
board[2][1] = 4;
board[2][2] = 1;
board[2][3] = 2;
board[3][0] = 4;
board[3][1] = 1;
board[3][2] = 2;
board[3][3] = 3;
board[3][3] = 0;
*/

View File

@ -6,14 +6,13 @@
/* By: akarami <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/26 18:48:28 by akarami #+# #+# */
/* Updated: 2023/03/26 18:48:35 by akarami ### ########.fr */
/* Updated: 2023/03/26 20:12:22 by akarami ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef BACKTRACK_H
# define BACKTRACK_H
int backtrack(int board[SIZE][SIZE], int clues[SIZE]);
void print_board(int board[SIZE][SIZE]);
int backtrack(int **board, int n, int *borders);
#endif

View File

@ -1,23 +1,22 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* helpers.h :+: :+: :+: */
/* gen_util.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: akarami <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/26 18:50:47 by akarami #+# #+# */
/* Updated: 2023/03/26 18:50:55 by akarami ### ########.fr */
/* Created: 2023/03/26 19:35:52 by akarami #+# #+# */
/* Updated: 2023/03/26 19:59:09 by akarami ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef HELPERS_H
# define HELPERS_H
# define SIZE 4
#ifndef GEN_UTIL_H
# define GEN_UTIL_H
int str_len(char *str);
void put_error(void);
void ft_putchar(char c);
int ft_strlen(char *str);
int ft_puterr(int exit_code);
void init_board(int board[SIZE][SIZE]);
void get_clues(char *input, int *border);
int *find_position(int **board, int n, int *pos);
void print_board(int **board, int n);
#endif

View File

@ -6,15 +6,15 @@
/* By: tischmid <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/26 07:48:00 by tischmid #+# #+# */
/* Updated: 2023/03/26 07:48:15 by tischmid ### ########.fr */
/* Updated: 2023/03/26 20:00:10 by akarami ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef HANDLE_INPUT_H
# define HANDLE_INPUT_H
int handle_input(int argc, char **argv, int *borders, int n);
int check_inp_valid(int argc, char **argv, int n);
int str_len(char *str);
void atoi_arr(char **argv, int *borders, int n);
int check_opposite_sum(int *borders, int n);

View File

@ -1,19 +1,19 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* error_check.h :+: :+: :+: */
/* handle_mem.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: akarami <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/26 18:48:43 by akarami #+# #+# */
/* Updated: 2023/03/26 18:48:44 by akarami ### ########.fr */
/* Created: 2023/03/26 19:46:53 by akarami #+# #+# */
/* Updated: 2023/03/26 20:36:34 by akarami ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef ERROR_CHECK_H
# define ERROR_CHECK_H
# include "helpers.h"
#ifndef HANDLE_MEM_H
# define HANDLE_MEM_H
int is_valid_input(int argc, char **argv);
int **alloc_mem(int n);
void free_mem(int *borders, int **board, int n);
#endif

View File

@ -1,21 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tischmid <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/26 07:43:49 by tischmid #+# #+# */
/* Updated: 2023/03/26 07:58:02 by tischmid ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef MAIN_H
# define MAIN_H
int **alloc_mem(int **board, int n);
void free_mem(int *borders, int **board, int n);
int handle_input(int argc, char **argv, int *borders, int n);
void put_error(void);
#endif

View File

@ -6,13 +6,39 @@
/* By: tischmid <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/26 06:13:07 by tischmid #+# #+# */
/* Updated: 2023/03/26 18:51:24 by akarami ### ########.fr */
/* Updated: 2023/03/26 20:38:09 by akarami ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdlib.h>
#include "include/main.h"
#include <stdio.h>
#include "include/handle_mem.h"
#include "include/handle_input.h"
#include "include/backtrack.h"
#include "include/gen_util.h"
void print_board(int **board, int n);
void init_board(int **board)
{
board[0][0] = 1;
board[0][1] = 2;
board[0][2] = 3;
board[0][3] = 4;
board[1][0] = 2;
board[1][1] = 3;
board[1][2] = 4;
board[1][3] = 1;
board[2][0] = 3;
board[2][1] = 4;
board[2][2] = 1;
board[2][3] = 2;
board[3][0] = 4;
board[3][1] = 1;
board[3][2] = 2;
board[3][3] = 3;
board[3][3] = 3;
}
int main(int argc, char **argv)
{
@ -20,19 +46,20 @@ int main(int argc, char **argv)
int *borders;
int **board;
board = 0;
n = 4;
borders = malloc(n * 4 * 4);
board = alloc_mem(board, n);
board = alloc_mem(n);
init_board(board);
print_board(board, n);
if (!handle_input(argc, argv, borders, n))
{
put_error();
return (1);
}
if (!backtrack(borders, board, n))
if (!backtrack(board, n, borders))
{
put_error();
return (1);
return (2);
}
free_mem(borders, board, n);
return (0);

View File

@ -1,41 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* print_array.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: akarami <akarami@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/25 17:15:08 by akarami #+# #+# */
/* Updated: 2023/03/26 07:48:42 by tischmid ### ########.fr */
/* */
/* ************************************************************************** */
#include <unistd.h>
void ft_putchar(char c)
{
write(1, &c, 1);
}
void ft_print_arrays(int **board, int size)
{
int x;
int y;
x = 1;
while (x <= size)
{
y = 1;
while (y <= size)
{
ft_putchar(board[x][y]);
if (y != size)
{
ft_putchar(' ');
}
++y;
}
ft_putchar('\n');
++x;
}
}