diff options
| author | Cabergs Charles <cacharle@e-r6-p7.s19.be> | 2019-07-15 08:15:37 +0200 |
|---|---|---|
| committer | Cabergs Charles <cacharle@e-r6-p7.s19.be> | 2019-07-15 08:15:37 +0200 |
| commit | 6bf5dd01493dd6bf644a4896c666535e5eaa8d67 (patch) | |
| tree | 25b02c02f5140dbefbabd7720f292d8be3d5cc51 | |
| parent | e8ab18a2c724c0acfa1688a62bc2042471d68e39 (diff) | |
| download | piscine-6bf5dd01493dd6bf644a4896c666535e5eaa8d67.tar.gz piscine-6bf5dd01493dd6bf644a4896c666535e5eaa8d67.tar.bz2 piscine-6bf5dd01493dd6bf644a4896c666535e5eaa8d67.zip | |
c07 passed, c08 in progress, rush01(+ 6x6 try)
41 files changed, 1591 insertions, 200 deletions
diff --git a/c04/ex03/ft_atoi.c b/c04/ex03/ft_atoi.c index a26308e..39dfbb1 100644 --- a/c04/ex03/ft_atoi.c +++ b/c04/ex03/ft_atoi.c @@ -6,7 +6,7 @@ /* By: cacharle <charles.cabergs@gmail.com> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/07/06 07:31:38 by cacharle #+# #+# */ -/* Updated: 2019/07/06 15:48:43 by cacharle ### ########.fr */ +/* Updated: 2019/07/11 07:29:42 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -36,6 +36,7 @@ int main() char *garbage_tail = "76iqu21#!@"; char *all = "\n\t \v++++---12341234#3%^@"; char *subject_test = " ---+--+1234ab567"; + char *nothing = " --+qwer"; printf("%d\n", ft_atoi(s_happypath)); printf("%d\n", ft_atoi(s_happypathn)); printf("%d\n", ft_atoi(s_int0)); @@ -47,6 +48,7 @@ int main() printf("%d\n", ft_atoi(garbage_tail)); printf("%d\n", ft_atoi(all)); printf("%d\n", ft_atoi(subject_test)); + printf("%d\n", ft_atoi(nothing)); printf("----------------------\n"); ft_putnbr_base(42, "0123456789"); printf("\n"); @@ -57,6 +59,7 @@ int main() ft_putnbr_base(42, "01234567"); printf("\n"); ft_putnbr_base(INT_MAX, "0123456789abcdef"); printf("\n"); ft_putnbr_base(INT_MIN, "0123456789abcdef"); printf("\n"); + ft_putnbr_base(0, "0123456789"); printf("\n"); ft_putnbr_base(INT_MAX, ""); ft_putnbr_base(INT_MAX, "a"); ft_putnbr_base(INT_MAX, "abb"); diff --git a/c05/ex06/ft_is_prime.c b/c05/ex06/ft_is_prime.c index 73ca59b..3c83061 100644 --- a/c05/ex06/ft_is_prime.c +++ b/c05/ex06/ft_is_prime.c @@ -6,7 +6,7 @@ /* By: cacharle <charles.cabergs@gmail.com> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/07/06 20:17:59 by cacharle #+# #+# */ -/* Updated: 2019/07/09 10:17:58 by cacharle ### ########.fr */ +/* Updated: 2019/07/12 07:43:36 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -23,7 +23,7 @@ int ft_is_prime(int nb) return (0); i = 1; nbu = nb; - while (i * i <= nbu) + while (i * i <= nbu) // ne fonctionne pas de 7 a 41 { if (nbu % (i * 6 - 1) == 0) return (0); @@ -80,12 +80,16 @@ int main() printf("prime(%d) = %d\n", 2147483647, ft_is_prime(2147483647)); printf("prime(%d) = %d\n", 899, ft_is_prime(899)); printf("prime(%d) = %d\n", 289, ft_is_prime(289)); - /*for (int i = INT_MAX; i > INT_MAX - 1000; i--)*/ + printf("prime(%d) = %d\n", 2147483424, ft_is_prime(2147483424)); + /*for (int i = INT_MAX; i > INT_MAX - 1000000; i--)*/ /*printf("%d is %d\n", i, ft_is_prime(i));*/ + for (int i = 3; i <= 41; i++) + printf("%d is %d\n", i, ft_is_prime(i)); printf("---------------------\n"); printf("nextp(%d) = %d\n", 21, ft_find_next_prime(21)); printf("nextp(%d) = %d\n", 23, ft_find_next_prime(23)); + printf("prime(%d) = %d\n", 2147483424, ft_find_next_prime(2147483424)); printf("nextp(%d) = %d\n", 2147483600, ft_find_next_prime(2147483600)); /*for (int i = INT_MAX; i > INT_MAX - 1000000; i--)*/ /*printf("%d is %d\n", i, ft_find_next_prime(i));*/ diff --git a/c07/ex03/ft_strjoin.c b/c07/ex03/ft_strjoin.c index ea97bb6..7801820 100644 --- a/c07/ex03/ft_strjoin.c +++ b/c07/ex03/ft_strjoin.c @@ -6,7 +6,7 @@ /* By: cacharle <charles.cabergs@gmail.com> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/07/07 16:14:25 by cacharle #+# #+# */ -/* Updated: 2019/07/09 09:08:52 by cacharle ### ########.fr */ +/* Updated: 2019/07/12 14:41:33 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -24,16 +24,11 @@ int my_strlen(char *str) int cummulative_strlen(int size, char **strs) { - int i; int len; len = 0; - i = 0; - while (size > 0) - { - len += my_strlen(strs[size - 1]); - size--; - } + while (size-- > 0) + len += my_strlen(strs[size]); return (len); } @@ -42,26 +37,24 @@ char *ft_strjoin(int size, char **strs, char *sep) int i; int j; int k; - int l; char *join; join = (char*)malloc(sizeof(char) * (cummulative_strlen(size, strs) - + my_strlen(sep) * (size - 1) + 1)); + + my_strlen(sep) * (size - (size == 0 ? 0 : 1) + 1))); + if (join == NULL) + return (NULL); j = 0; - l = 0; - while (l < size) + k = 0; + while (k < size) { i = 0; - while (strs[l][i]) - { - join[j] = strs[l][i]; - i++; - j++; - } - k = 0; - while (l != size - 1 && sep[k]) - join[j++] = sep[k++]; - l++; + while (strs[k][i]) + join[j++] = strs[k][i++]; + i = 0; + while (k != size - 1 && sep[i]) + join[j++] = sep[i++]; + k++; } + join[j] = '\0'; return (join); } diff --git a/c07/ex04/ft_convert_base.c b/c07/ex04/ft_convert_base.c index f30c3e5..0abdbe8 100644 --- a/c07/ex04/ft_convert_base.c +++ b/c07/ex04/ft_convert_base.c @@ -6,118 +6,60 @@ /* By: cacharle <charles.cabergs@gmail.com> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/07/08 16:16:53 by cacharle #+# #+# */ -/* Updated: 2019/07/10 06:37:09 by cacharle ### ########.fr */ +/* Updated: 2019/07/12 12:07:28 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ -int check_base(char *base) -{ - int i; - int j; - - i = 0; - while (base[i]) - { - if (base[i] == '-' || base[i] == '+' || base[i] == ' ' - || base[i] == '\t' || base[i] == '\n' || base[i] == '\v' - || base[i] == '\f' || base[i] == '\r') - return (0); - j = 0; - while (base[j]) - { - if (j != i && base[j] == base[i]) - return (0); - j++; - } - i++; - } - if (i < 2) - return (0); - return (1); -} - -int ft_pow(int base, int exponent) -{ - int accumulator; - - accumulator = 1; - while (exponent-- > 0) - accumulator *= base; - return (accumulator); -} - - -int position_in_base(char digit, char *base) -{ - int i; +#include <stdlib.h> - i = 0; - while (base[i] != digit) - i++; - return (i); -} +int check_base(char *base); +int ft_pow(int base, int exponent); +int position_in_base(char digit, char *base); +int some_strlen(char *str); -// CHECK THE STR NOT JUST THE BASE (you idiot) -int ft_atoi_base(char *str, char *base) +int from_base(char *str, char *base) { int radix; int i; int j; int nb; + int is_negative; - if (!check_base(base)) - return (0); + while (*str == ' ' || *str == '\t' || *str == '\n' + || *str == '\v' || *str == '\f' || *str == '\r') + str++; + is_negative = 0; + while (*str == '-' || *str == '+') + if (*str++ == '-') + is_negative = !is_negative; nb = 0; radix = 0; while (base[radix]) radix++; i = 0; - while (str[i]) + while (position_in_base(str[i], base) != -1) i++; j = 0; while (--i >= 0) - { - nb += ft_pow(radix, i) * position_in_base(str[j], base); - j++; - } - return (nb); -} - -int some_strlen(char *str) -{ - int counter; - - counter = 0; - while (str[counter]) - counter++; - return (counter); + nb += ft_pow(radix, i) * position_in_base(str[j++], base); + return (is_negative ? -nb : nb); } -char *to_base(int nbr, char *base) +char *to_base(int nbr, char *base, int radix, int is_negative) { - int radix; int i; + int j; unsigned int nbu; char rev_digits[1024]; char *ret; - int j; - int is_negative; - /*if (!check_base(base))*/ - /*return (NULL);*/ - radix = some_strlen(base); nbu = nbr; - is_negative = 0; - if (nbr < 0) - { - is_negative = 1; + if (is_negative) nbu = -nbr; - } - i = 0; - printf("%d %s %d\n", radix, base, nbr); + i = nbu == 0 ? 1 : 0; + rev_digits[0] = base[0]; while (nbu > 0) { - printf("%u\n", nbu % radix); rev_digits[i] = base[nbu % radix]; nbu /= radix; i++; @@ -125,27 +67,21 @@ char *to_base(int nbr, char *base) ret = malloc(sizeof(char) * i + (is_negative ? 1 : 0)); j = 0; if (is_negative) - { ret[0] = '-'; - j++; - } - while (i-- > 0) - ret[j++] = rev_digits[i]; + while (i-- + (is_negative ? 1 : 0) > 0) + ret[j++ + (is_negative ? 1 : 0)] = rev_digits[i]; ret[j] = '\0'; - for (int i = 0; i < 4; i++) - printf("%c ", rev_digits[i]); - printf("\n"); - for (int i = 0; i < 4; i++) - printf("%c ", ret[i]); - printf("\n"); return (ret); } char *ft_convert_base(char *nbr, char *base_from, char *base_to) { - int converted_nb; - - int nb; - return NULL; - + int nb; + char *parsed_nbr; + + if (!check_base(base_from) || !check_base(base_to)) + return (NULL); + parsed_nbr = nbr; + nb = from_base(parsed_nbr, base_from); + return (to_base(nb, base_to, some_strlen(base_to), nb < 0)); } diff --git a/c07/ex04/ft_convert_base2.c b/c07/ex04/ft_convert_base2.c index e69de29..be9a03e 100644 --- a/c07/ex04/ft_convert_base2.c +++ b/c07/ex04/ft_convert_base2.c @@ -0,0 +1,69 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_convert_base2.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/07/10 20:22:09 by cacharle #+# #+# */ +/* Updated: 2019/07/11 10:10:08 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +int check_base(char *base) +{ + int i; + int j; + + i = 0; + while (base[i]) + { + if (base[i] == '-' || base[i] == '+' || base[i] == ' ' + || base[i] == '\t' || base[i] == '\n' || base[i] == '\v' + || base[i] == '\f' || base[i] == '\r') + return (0); + j = 0; + while (base[j]) + { + if (j != i && base[j] == base[i]) + return (0); + j++; + } + i++; + } + if (i < 2) + return (0); + return (1); +} + +int ft_pow(int base, int exponent) +{ + int accumulator; + + accumulator = 1; + while (exponent-- > 0) + accumulator *= base; + return (accumulator); +} + +int position_in_base(char digit, char *base) +{ + int i; + + i = 0; + while (digit != base[i] && base[i]) + i++; + if (base[i] == '\0') + return (-1); + return (i); +} + +int some_strlen(char *str) +{ + int counter; + + counter = 0; + while (str[counter]) + counter++; + return (counter); +} diff --git a/c07/ex05/ft_split.c b/c07/ex05/ft_split.c index 956782f..3ff2e43 100644 --- a/c07/ex05/ft_split.c +++ b/c07/ex05/ft_split.c @@ -6,7 +6,7 @@ /* By: cacharle <charles.cabergs@gmail.com> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/07/08 15:58:03 by cacharle #+# #+# */ -/* Updated: 2019/07/09 12:42:21 by cacharle ### ########.fr */ +/* Updated: 2019/07/13 08:15:31 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -24,41 +24,51 @@ int count_segment(char *str, char *charset) { int counter; - counter = 1; + counter = 0; while (*str) { - if (in_charset(*str, charset)) + if (!in_charset(*str, charset)) + { counter++; + while (!in_charset(*str, charset) && *str) + str++; + if (!*str) + break ; + } str++; } return (counter); } -char **ft_split(char *str, char *charset) +char **heck(char *str, char *charset, char *tmp, int i) { char **strs; - char *tmp; - int i; int j; - int k; - strs = (char**)malloc(sizeof(char*) * (count_segment(str, charset) + 1)); - printf("%d\n", count_segment(str, charset)); - k = 0; + if ((strs = (char**)malloc(sizeof(char*) + * (count_segment(str, charset) + 1))) == NULL) + return (NULL); + j = 0; while (*str) { + while (in_charset(*str, charset)) + str++; i = 0; - while (!in_charset(str[i], charset)) + while (!in_charset(str[i], charset) && str[i]) i++; - tmp = (char*)malloc(sizeof(char) * i); - if (tmp == NULL) - printf("bonjour"); + if ((tmp = (char*)malloc(sizeof(char) * i + 1)) == NULL) + return (NULL); i = 0; - while (!in_charset(*str, charset)) + while (!in_charset(*str, charset) && *str) tmp[i++] = *str++; - strs[k++] = tmp; - str++; + tmp[i] = '\0'; + strs[j++] = tmp; } - strs[k] = 0; + strs[j] = 0; return (strs); } + +char **ft_split(char *str, char *charset) +{ + return (heck(str, charset, NULL, 0)); +} @@ -6,6 +6,7 @@ #include "ex02/ft_ultimate_range.c" #include "ex03/ft_strjoin.c" #include "ex04/ft_convert_base.c" +#include "ex04/ft_convert_base2.c" #include "ex05/ft_split.c" int main() @@ -37,28 +38,55 @@ int main() printf("---------------\n"); char *strs[] = {"bon", "je", "suis", "charles"}; - char *sep = ", "; + char *sep = "%&^"; int size = 4; char *join; join = ft_strjoin(size, strs, sep); printf("[%d] %s\n", my_strlen(join), join); - /*for (int i = 0; i < my_strlen(join) + 1; i++)*/ - /*printf("%d ", join[i]);*/ + for (int i = 0; i < my_strlen(join) + 1; i++) + printf("%d ", join[i]); free(join); printf("\n---------------\n"); - printf("%s\n", ft_convert_base("101010", "01", "0123456789")); - printf("%s\n", to_base(34, "10")); + char *converted; + converted = ft_convert_base("-101010", "01", "0123456789"); + printf("%s\n", converted); + free(converted); + converted = ft_convert_base(" -+1010102345678ff", "01", "0123456789abcdef"); + printf("%s\n", converted); + free(converted); + converted = ft_convert_base(" \t\f\v +34589qw", "01234567", "012"); + printf("%s\n", converted); + free(converted); + converted = ft_convert_base(" \t\f\v +asdf", "fgh", ".?"); + printf("%s\n", converted); + free(converted); + printf("%s\n", ft_convert_base(" \t\f\v +34589qw", "01234567", "0+12")); + printf("%s\n", ft_convert_base(" \t\f\v +34589qw", "01234567", "012 ")); + printf("%s\n", ft_convert_base(" \t\f\v +34589qw", "012345-67", "012")); + printf("%s\n", ft_convert_base(" \t\f\v +34589qw", "012\t34567", "012")); + printf("%s\n", ft_convert_base(" \t\f\v +34589qw", "0", "012")); + printf("%s\n", ft_convert_base(" \t\f\v +34589qw", "01", "")); + printf("%s\n", ft_convert_base(" \t\f\v +34w", "01", "0123456789")); + printf("%s\n", ft_convert_base(" \t\f\v +34w", "01", "!?")); - /*printf("\n---------------\n");*/ - /*char *str = "bon.je.suis,asdofoisafj.ladjsf";*/ - /*char *charset = "";*/ - /*char **sstrs = ft_split(str, charset);*/ - /*for (int i = 0; sstrs[i] != 0; i++)*/ - /*printf("%s\n", sstrs[i]);*/ - /*for (int i = 0; sstrs[i] != 0; i++)*/ - /*free(sstrs[i]);*/ - /*free(sstrs);*/ + printf("\n---------------\n"); + char *const str = "3YaZkAP30iGoBWv L asdf h LbpX8Hx FWHwB2u1FH0S5"; + char *begin = "\n \t hgonjour"; + char *end = "jesuis\n\t hhh"; + char *empty = ""; + char *charset = "\n\t "; + char **sstrs = ft_split(begin, charset); + for (int i = 0; i < count_segment(begin, charset) + 1; i++) + { + printf("\n%s: ", sstrs[i]); + /*for (int j = 0; j < 10; j++)*/ + /*printf("%d ", sstrs[i][j]);*/ + } + if (sstrs) + for (int i = 0; sstrs[i] != 0; i++) + free(sstrs[i]); + free(sstrs); return 0; } diff --git a/c08/boolean_main.c b/c08/boolean_main.c new file mode 100644 index 0000000..9e8e428 --- /dev/null +++ b/c08/boolean_main.c @@ -0,0 +1,34 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* boolean_main.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/07/11 14:40:38 by cacharle #+# #+# */ +/* Updated: 2019/07/11 14:40:40 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "./ex01/ft_boolean.h" + +void ft_putstr(char *str) +{ + while (*str) + write(1, str++, 1); +} + +t_bool ft_is_even(int nbr) +{ + return ((EVEN(nbr)) ? TRUE : FALSE); +} + +int main(int argc, char **argv) +{ + (void)argv; + if (ft_is_even(argc - 1) == TRUE) + ft_putstr(EVEN_MSG); + else + ft_putstr(ODD_MSG); + return (SUCCESS); +} diff --git a/c08/ex00/ft.h b/c08/ex00/ft.h index 5dbbb51..9575169 100644 --- a/c08/ex00/ft.h +++ b/c08/ex00/ft.h @@ -6,12 +6,12 @@ /* By: cacharle <charles.cabergs@gmail.com> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/07/07 16:33:44 by cacharle #+# #+# */ -/* Updated: 2019/07/07 16:34:58 by cacharle ### ########.fr */ +/* Updated: 2019/07/12 14:50:28 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ #ifndef FT_H -#define FT_H +# define FT_H void ft_putchar(char c); void ft_swap(int *a, int *b); diff --git a/c08/ex01/ft_boolean.h b/c08/ex01/ft_boolean.h index 43a3c4f..6728409 100644 --- a/c08/ex01/ft_boolean.h +++ b/c08/ex01/ft_boolean.h @@ -6,29 +6,28 @@ /* By: cacharle <charles.cabergs@gmail.com> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/07/07 16:35:29 by cacharle #+# #+# */ -/* Updated: 2019/07/07 17:07:47 by cacharle ### ########.fr */ +/* Updated: 2019/07/11 14:37:46 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ #ifndef FT_BOOLEAN_H -#define FT_BOOLEAN_H +# define FT_BOOLEAN_H -#include <unistd.h> +# include <unistd.h> -#define TRUE 1 -#define FALSE 1 -#define SUCCESS 0 -#define EVEN_MSG "I have an even number of arguments.\n" -#define ODD_MSG "I have an odd number of arguments.\n" -#define EVEN(x) (x % 2 == 0 ? 1 : 0) +# define TRUE 1 +# define FALSE 0 +# define SUCCESS 0 +# define EVEN_MSG "I have an even number of arguments.\n" +# define ODD_MSG "I have an odd number of arguments.\n" +# define EVEN(x) (x % 2 == 0 ? 1 : 0) typedef enum { - FALSE, - TRUE -} t_bool; - -void ft_putstr(char *str); -t_bool ft_is_even(int nbr); + true, + false +} t_bool; +void ft_putstr(char *str); +t_bool ft_is_even(int nbr); #endif diff --git a/c08/ex02/ft_abs.h b/c08/ex02/ft_abs.h index 9a41241..ba01929 100644 --- a/c08/ex02/ft_abs.h +++ b/c08/ex02/ft_abs.h @@ -6,13 +6,13 @@ /* By: cacharle <charles.cabergs@gmail.com> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/07/07 17:09:37 by cacharle #+# #+# */ -/* Updated: 2019/07/07 17:11:50 by cacharle ### ########.fr */ +/* Updated: 2019/07/13 08:56:29 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ #ifndef FT_ABS_H -#define FT_ABS_H +# define FT_ABS_H -#define ABS(x) (x < 0 ? -x : x) +# define ABS(x) ( ( (x) < 0) ? -(x) : (x) ) #endif diff --git a/c08/ex03/ft_point.h b/c08/ex03/ft_point.h index 088b792..5129a4f 100644 --- a/c08/ex03/ft_point.h +++ b/c08/ex03/ft_point.h @@ -6,16 +6,16 @@ /* By: cacharle <charles.cabergs@gmail.com> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/07/07 17:12:47 by cacharle #+# #+# */ -/* Updated: 2019/07/07 17:15:20 by cacharle ### ########.fr */ +/* Updated: 2019/07/11 14:13:47 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ #ifndef FT_POINT_H -#define FT_POINT_H +# define FT_POINT_H typedef struct { int x; int y; -} t_point; +} t_point; #endif diff --git a/c08/ex04/ft_stock_str.h b/c08/ex04/ft_stock_str.h index ccadd2f..2ce09f4 100644 --- a/c08/ex04/ft_stock_str.h +++ b/c08/ex04/ft_stock_str.h @@ -6,18 +6,18 @@ /* By: cacharle <charles.cabergs@gmail.com> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/07/08 16:54:59 by cacharle #+# #+# */ -/* Updated: 2019/07/08 16:56:02 by cacharle ### ########.fr */ +/* Updated: 2019/07/11 14:14:13 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ #ifndef FT_STOCK_STR_H -#define FT_STOCK_STR_H +# define FT_STOCK_STR_H -typedef struct s_stock_str +typedef struct s_stock_str { int size; char *str; char *copy; -} t_stock_str; +} t_stock_str; #endif diff --git a/c08/ex04/ft_strs_to_tab.c b/c08/ex04/ft_strs_to_tab.c index 67a0304..f6fbfaf 100644 --- a/c08/ex04/ft_strs_to_tab.c +++ b/c08/ex04/ft_strs_to_tab.c @@ -6,35 +6,58 @@ /* By: cacharle <charles.cabergs@gmail.com> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/07/07 17:15:32 by cacharle #+# #+# */ -/* Updated: 2019/07/08 17:10:21 by cacharle ### ########.fr */ +/* Updated: 2019/07/14 11:12:08 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ #include <stdlib.h> #include "ft_stock_str.h" -struct s_stock_str *ft_strs_to_tab(int ac, char **av) +int ft_strlen(char *str) +{ + int counter; + + counter = 0; + while (str[counter]) + counter++; + return (counter); +} + +char *ft_strdup(char *src) +{ + int i; + char *dup_ptr; + + dup_ptr = (char*)malloc(sizeof(char) * ft_strlen(src)); + if (dup_ptr == NULL) + return (NULL); + i = 0; + while (src[i]) + { + dup_ptr[i] = src[i]; + i++; + } + dup_ptr[i] = '\0'; + return (dup_ptr); +} + +struct s_stock_str *ft_strs_to_tab(int ac, char **av) { int i; - char *copy; t_stock_str tmp_stock; - t_stock_str *strs_stocks + t_stock_str *strs_stocks; - strs_stocks = malloc(sizeof(t_stock_str) * ac); - while (ac-- > 0) + if ((strs_stocks = malloc(sizeof(t_stock_str) * (ac + 1))) == NULL) + return (NULL); + i = 0; + while (i < ac) { - i = 0 - while (av[ac][i]) - i++; - tmp_stock = malloc(sizeof(t_stock_str)); - tmp_stock->size = i; - tmp_stock->str = av[ac]; - copy = malloc(sizeof(char) * i); - i = -1; - while (av[ac][i++]) - copy[i] = av[ac][i]; - tmp_stock->copy = copy; - strs_stocks[ac] = tmp_stock; + tmp_stock.size = ft_strlen(av[i]); + tmp_stock.str = av[i]; + if ((tmp_stock.copy = ft_strdup(av[i])) == NULL) + return (NULL); + strs_stocks[i++] = tmp_stock; } + strs_stocks[ac].str = 0; return (strs_stocks); } diff --git a/c08/ex05/ft_show_tab.c b/c08/ex05/ft_show_tab.c new file mode 100644 index 0000000..4af1843 --- /dev/null +++ b/c08/ex05/ft_show_tab.c @@ -0,0 +1,57 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_show_tab.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/07/11 15:55:41 by cacharle #+# #+# */ +/* Updated: 2019/07/14 11:12:21 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include <unistd.h> +#include "ft_stock_str.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'); +} + +void ft_putstr(char *str) +{ + while (*str) + write(1, str++, 1); +} + +void ft_show_tab(struct s_stock_str *par) +{ + int i; + + i = 0; + while (par[i].str != 0) + { + ft_putstr(par[i].str); + write(1, "\n", 1); + ft_putnbr(par[i].size); + write(1, "\n", 1); + ft_putstr(par[i].copy); + write(1, "\n", 1); + i++; + } +} diff --git a/c08/ex05/ft_stock_str.h b/c08/ex05/ft_stock_str.h new file mode 100644 index 0000000..2ce09f4 --- /dev/null +++ b/c08/ex05/ft_stock_str.h @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_stock_str.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/07/08 16:54:59 by cacharle #+# #+# */ +/* Updated: 2019/07/11 14:14:13 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef FT_STOCK_STR_H +# define FT_STOCK_STR_H + +typedef struct s_stock_str +{ + int size; + char *str; + char *copy; +} t_stock_str; + +#endif diff --git a/c08/main.c b/c08/main.c new file mode 100644 index 0000000..67a40dc --- /dev/null +++ b/c08/main.c @@ -0,0 +1,51 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* main.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/07/12 15:24:19 by cacharle #+# #+# */ +/* Updated: 2019/07/13 08:55:06 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include <stdio.h> +#include <string.h> +#include "ex02/ft_abs.h" +#include "ex03/ft_point.h" +#include "ex04/ft_strs_to_tab.c" +#include "ex05/ft_show_tab.c" + +int main() +{ + printf("%d\n", ABS(42)); + printf("%d\n", ABS(-42)); + printf("%d\n", ABS(30 - 42)); + printf("%d\n", ABS(50 * -45)); + printf("%d\n", ABS(-24) - 20); + + printf("--------------------\n"); + char **a = (char**)malloc(sizeof(char*) * 3); + a[0] = (char*)malloc(sizeof(char) * 32); + a[1] = (char*)malloc(sizeof(char) * 32); + a[2] = (char*)malloc(sizeof(char) * 32); + strcpy(a[0], "binjour"); + strcpy(a[1], "je"); + strcpy(a[2], "suis"); + t_stock_str *b = ft_strs_to_tab(3, a); + for (int i = 0; b[i].str != 0; i++) + printf("%s | %s | %d\n", b[i].str, b[i].copy, b[i].size); + + b[0].copy = "bafj"; + ft_show_tab(b); + /*for (int i = 0; b[i].str != 0; i++)*/ + /*free(b[i].copy);*/ + /*free(b);*/ + /*free(a[0]);*/ + /*free(a[1]);*/ + /*free(a[2]);*/ + /*free(a);*/ + + return 0; +} diff --git a/c08/point_main.c b/c08/point_main.c new file mode 100644 index 0000000..ec651f4 --- /dev/null +++ b/c08/point_main.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* point_main.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/07/11 14:50:27 by cacharle #+# #+# */ +/* Updated: 2019/07/11 14:51:48 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ex03/ft_point.h" + +void set_point(t_point *point) +{ + point->x = 42; + point->y = 21; +} + +int main(void) +{ + t_point point; + + set_point(&point); + return (0); +} diff --git a/c09/ex00/ft_putchar.c b/c09/ex00/ft_putchar.c new file mode 100644 index 0000000..8a53dc8 --- /dev/null +++ b/c09/ex00/ft_putchar.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putchar.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/07/02 22:03:32 by cacharle #+# #+# */ +/* Updated: 2019/07/03 14:21:40 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include <unistd.h> + +void ft_putchar(char c) +{ + write(1, &c, 1); +} diff --git a/c09/ex00/ft_putstr.c b/c09/ex00/ft_putstr.c new file mode 100644 index 0000000..c4f4564 --- /dev/null +++ b/c09/ex00/ft_putstr.c @@ -0,0 +1,19 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putstr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/07/11 16:17:16 by cacharle #+# #+# */ +/* Updated: 2019/07/11 16:18:11 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include <unistd.h> + +void ft_putstr(char *str) +{ + while (*str) + write(1, str++, 1); +} diff --git a/c09/ex00/ft_strcmp.c b/c09/ex00/ft_strcmp.c new file mode 100644 index 0000000..8870811 --- /dev/null +++ b/c09/ex00/ft_strcmp.c @@ -0,0 +1,21 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strcmp.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/07/11 16:20:33 by cacharle #+# #+# */ +/* Updated: 2019/07/11 16:21:55 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +int ft_strcmp(char *s1, char *s2) +{ + while (*s1 == *s2 && *s1 && *s2) + { + s1++; + s2++; + } + return (*s1 - *s2); +} diff --git a/c09/ex00/ft_strlen.c b/c09/ex00/ft_strlen.c new file mode 100644 index 0000000..91e6948 --- /dev/null +++ b/c09/ex00/ft_strlen.c @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strlen.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/07/11 16:19:43 by cacharle #+# #+# */ +/* Updated: 2019/07/11 16:20:18 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +int ft_strlen(char *str) +{ + int i; + + i = 0; + while (str[i]) + i++; + return (i); +} + diff --git a/c09/ex00/ft_swap.c b/c09/ex00/ft_swap.c new file mode 100644 index 0000000..989b2c2 --- /dev/null +++ b/c09/ex00/ft_swap.c @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_swap.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/07/11 16:18:42 by cacharle #+# #+# */ +/* Updated: 2019/07/11 16:19:31 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +void ft_swap(int *a, int *b) +{ + int tmp; + + tmp = *a; + *a = *b; + *b = tmp; +} diff --git a/c09/ex00/libft_creator.sh b/c09/ex00/libft_creator.sh new file mode 100644 index 0000000..f799ee2 --- /dev/null +++ b/c09/ex00/libft_creator.sh @@ -0,0 +1,3 @@ +#!/bin/sh +gcc -c *.c +ar -rcs libft.a *.o diff --git a/c09/ex01/Makefile b/c09/ex01/Makefile new file mode 100644 index 0000000..0fec395 --- /dev/null +++ b/c09/ex01/Makefile @@ -0,0 +1,28 @@ +SRCDIR = srcs +SRC = ft_putchar.c ft_swap.c ft_putstr.c ft_strlen.c ft_strcmp.c +OBJ = $(SRC:.c = .o) +INCLUDEDIR = includes +INCLUDES = ft.h +CC = gcc +CCFLAGS = -Wall -Wextra -Werror +OUT = libft.a + +all: OUT +.PHONY: all + +$(OUT): $(OBJ) + ar -crs $(OBJ) + +$(OBJ): %.o: %.c + $(CC) $(CCFLAGS) -c $< -o $@ + +.PHONY: clean +clean: + rm -f srcs/*.o + +.PHONY: fclean +fclean: clean + rm $(OUT) + +.PHONY: re +re: fclean all diff --git a/c10/ex00/Makefile b/c10/ex00/Makefile new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/c10/ex00/Makefile diff --git a/c10/ex00/bonjour b/c10/ex00/bonjour new file mode 100644 index 0000000..918a5ef --- /dev/null +++ b/c10/ex00/bonjour @@ -0,0 +1,4 @@ +bonjour +je +suis +charles diff --git a/c10/ex00/main.c b/c10/ex00/main.c new file mode 100644 index 0000000..8fee64a --- /dev/null +++ b/c10/ex00/main.c @@ -0,0 +1,63 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* main.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/07/11 18:06:43 by cacharle #+# #+# */ +/* Updated: 2019/07/11 18:45:01 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include <unistd.h> +#include <fcntl.h> +#define BUFFER_SIZE 1 +#include <stdio.h> + +int write_buffer(char *buf) +{ + int i; + + i = 0; + while (buf[i]) + { + write(STDOUT_FILENO, &buf[i], 1); + /*printf("%d %c\n", buf[i], buf[i]);*/ + if (buf[i] == -1) + return (1); + i++; + } + return (0); +} + +int main(int argc, char **argv) +{ + int fildes; + char buf[BUFFER_SIZE]; + int reading; + + if (argc == 1) + { + write(STDERR_FILENO, "File name missing.\n", 20); + return (1); + } + else if (argc > 2) + { + write(STDERR_FILENO, "Too many arguments.\n", 21); + return (1); + } + if ((fildes = open(argv[1], O_RDONLY)) < 0) + { + write(STDERR_FILENO, "Cannot read file.\n", 18); + return (1); + } + while (1) + { + if (read(fildes, buf, BUFFER_SIZE) == 0) + break; + write_buffer(buf); + } + close(fildes); + return (0); +} diff --git a/rush01/ex00/helper.c b/rush01/ex00/helper.c new file mode 100644 index 0000000..bab12d5 --- /dev/null +++ b/rush01/ex00/helper.c @@ -0,0 +1,69 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* helper.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/07/13 12:52:42 by cacharle #+# #+# */ +/* Updated: 2019/07/14 17:44:58 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include <unistd.h> +#include "include.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 get_with_view(t_board board, t_view_point view, int view_line, + int line_index) +{ + if (view == row_left) + return (board[view_line][line_index]); + if (view == row_right) + return (board[view_line][3 - line_index]); + if (view == col_up) + return (board[line_index][view_line]); + if (view == col_down) + return (board[3 - line_index][view_line]); + return (-1); +} + +int check_arg(char *str) +{ + int i; + + i = 0; + while (str[i]) + { + if (!(str[i] >= '1' && str[i] <= '4')) + return (FALSE); + i++; + if (!str[i]) + break ; + if (str[i] != ' ') + return (FALSE); + i++; + } + if (i != 31) + return (FALSE); + return (TRUE); +} diff --git a/rush01/ex00/include.h b/rush01/ex00/include.h new file mode 100644 index 0000000..4d0bf1c --- /dev/null +++ b/rush01/ex00/include.h @@ -0,0 +1,48 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* include.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/07/13 12:07:59 by cacharle #+# #+# */ +/* Updated: 2019/07/14 22:49:33 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef INCLUDE_H +# define INCLUDE_H + +# define UNASSIGNED 0 +# define TRUE 1 +# define FALSE 0 + +enum e_view_point +{ + col_up, + col_down, + row_left, + row_right +}; +typedef enum e_view_point t_view_point; +typedef int** t_board; +typedef int** t_views; + +int solve(t_board board, t_views views, int *slv); +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); +int check_arg(char *str); + +#endif diff --git a/rush01/ex00/main.c b/rush01/ex00/main.c new file mode 100644 index 0000000..638e958 --- /dev/null +++ b/rush01/ex00/main.c @@ -0,0 +1,39 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* main.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/07/13 11:30:35 by cacharle #+# #+# */ +/* Updated: 2019/07/14 22:36:55 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include <unistd.h> +#include "include.h" + +int main(int argc, char **argv) +{ + t_views views; + t_board board; + int solved; + + if (argc != 2) + { + write(1, "Error\n", 6); + return (0); + } + if (!check_arg(argv[1])) + { + write(1, "Error\n", 6); + return (0); + } + views = parse_arg(argv[1]); + board = init_square(4); + solved = FALSE; + solve(board, views, &solved); + if (!solved) + write(1, "Error\n", 6); + return (0); +} diff --git a/rush01/ex00/solve.c b/rush01/ex00/solve.c new file mode 100644 index 0000000..b430a6d --- /dev/null +++ b/rush01/ex00/solve.c @@ -0,0 +1,138 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* solve.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/07/13 14:25:32 by cacharle #+# #+# */ +/* Updated: 2019/07/14 22:39:24 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "include.h" + +/* +** 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 *solved) +{ + int row; + int col; + int i; + t_board board_clone; + + if (!find_next_unassigned(board, &row, &col)) + return (TRUE); + i = 0; + while (++i <= 4) + { + if (!is_alone(board, row, col, i)) + continue ; + board_clone = dup_square(board); + board_clone[row][col] = i; + if (solve(board_clone, views, solved) + && check_viewpoints(board_clone, views)) + { + *solved = TRUE; + print_square(board_clone); + destroy_square(board_clone); + return (TRUE); + } + destroy_square(board_clone); + } + 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 < 4) + { + *col = 0; + while (*col < 4) + { + if (board[*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 < 4) + if (board[row][i++] == building_floor) + return (FALSE); + i = 0; + while (i < 4) + if (board[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 < 4) + { + 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 < 4) + { + tmp_building_floor = get_with_view(board, view, view_line, i); + if (tmp_building_floor == UNASSIGNED) + return (FALSE); + building_viewed--; + while (i + 1 < 4 && tmp_building_floor > get_with_view( + board, view, view_line, i + 1)) + i++; + i++; + } + return (building_viewed == 0); +} diff --git a/rush01/ex00/square.c b/rush01/ex00/square.c new file mode 100644 index 0000000..86842f6 --- /dev/null +++ b/rush01/ex00/square.c @@ -0,0 +1,133 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* helpers.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/07/13 12:31:20 by cacharle #+# #+# */ +/* Updated: 2019/07/14 15:04:11 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); + i = 0; + j = 0; + while (i < 16) + { + if (arg[i] == ' ') + { + arg++; + continue; + } + views[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. +*/ + +int **init_square(int size) +{ + int i; + int j; + int **square; + + square = malloc(sizeof(int*) * size); + i = 0; + while (i < size) + { + square[i] = malloc(sizeof(int) * size); + j = 0; + while (j < size) + square[i][j++] = 0; + i++; + } + return (square); +} + +/* +** Duplicate the square, create an empty clone +** and copy each element of `square` in it. +*/ + +int **dup_square(int **square) +{ + int **clone; + int i; + int j; + + clone = init_square(4); + i = 0; + while (i < 4) + { + j = 0; + while (j < 4) + { + clone[i][j] = square[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[i++]); + free(square); +} + +/* +** 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_board board) +{ + int i; + int j; + + i = 0; + while (i < 4) + { + j = 0; + while (j < 4) + { + ft_putnbr(board[i][j]); + j++; + if (j != 4) + ft_putchar(' '); + } + ft_putchar('\n'); + i++; + } +} 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++; + } +} |
