diff --git a/2024/4/Makefile b/2024/4/Makefile new file mode 100644 index 0000000..808a501 --- /dev/null +++ b/2024/4/Makefile @@ -0,0 +1,69 @@ +# **************************************************************************** # +# # +# ::: :::::::: # +# Makefile :+: :+: :+: # +# +:+ +:+ +:+ # +# By: tomoron +#+ +:+ +#+ # +# +#+#+#+#+#+ +#+ # +# Created: 2024/07/17 15:48:36 by tomoron #+# #+# # +# Updated: 2024/10/29 20:43:12 by tomoron ### ########.fr # +# # +# **************************************************************************** # + +SRCS = main.c\ + part1.c\ + part2.c + +OBJS = $(SRCS:.c=.o) + +FLAGS = -Wextra -Werror -Wall -g + +LIB = libft/libft.a + +all: $(LIB) a.out test + cp test cur_input + ./a.out + +f: $(LIB) a.out input + cp input cur_input + ./a.out + +test: + touch test + +input: + touch input + +cp: + cp part1.c part2.c + sed -i 's/resolve_part1/resolve_part2/g' part2.c + +$(LIB): libft + make -C libft + +libft: + cp -r ~/Desktop/aoc/libft . + +main.c: + cp ~/Desktop/aoc/main.c . + +part1.c: + cp ~/Desktop/aoc/part1.c . + +part2.c: + cp ~/Desktop/aoc/part2.c . + +a.out: main.c $(OBJS) $(LIB) + clang $(FLAGS) $(OBJS) $(LIB) + +clean: + rm -rf $(OBJS) + rm -rf libft + +fclean: clean + rm -rf a.out + +.c.o: + clang $(FLAGS) -c $< -o $@ + +.PHONY: t all clean fclean cp diff --git a/2024/4/main.c b/2024/4/main.c new file mode 100644 index 0000000..4f9b609 --- /dev/null +++ b/2024/4/main.c @@ -0,0 +1,67 @@ +#include +#include +#include +#include +#include "libft/libft.h" + +long int resolve_part1(char *input, char **split); +long int resolve_part2(char *input, char **split); + +char *get_input(char *file) +{ + int fd; + int rd_len; + char buffer[1000]; + int len; + char *res; + + fd = open(file, O_RDONLY); + len = 0; + if(fd < 0) + return(0); + rd_len = -1; + while(rd_len) + { + rd_len = read(fd, buffer, 1000); + len+= rd_len; + } + close(fd); + res = malloc(len + 1); + if(!res) + return(0); + fd = open(file, O_RDONLY); + if(fd < 0) + return(0); + rd_len = read(fd,res, len); + res[len] = 0; + close(fd); + return(res); +} + +int main(void) +{ + char *input; + char *input_cpy; + char **split; + + input = get_input("cur_input"); + input_cpy = ft_strdup(input); + if(!input || !input_cpy) + { + fprintf(stderr, "file read error\n"); + return(1); + } + split = ft_split(input, '\n'); + if(!split) + return(1); + printf("result part 1: %ld\n", resolve_part1(input, split)); + ft_free_str_arr(split); + split = ft_split(input_cpy, '\n'); + if(!split) + return(1); + printf("result part 2: %ld\n", resolve_part2(input, split)); + ft_free_str_arr(split); + free(input); + free(input_cpy); + return(0); +} diff --git a/2024/4/part1.c b/2024/4/part1.c new file mode 100644 index 0000000..fb879f3 --- /dev/null +++ b/2024/4/part1.c @@ -0,0 +1,78 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* part1.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tomoron +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/07/17 23:03:36 by tomoron #+# #+# */ +/* Updated: 2024/12/04 10:41:12 by tomoron ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include +#include "libft/libft.h" + +static int is_word(char **split, int y, int x, int depth, int dir) +{ + char *word = "XMAS"; + int nb; + + if(depth == 4) + return(1); + if( y < 0 || x < 0 || !split[y] || !split[y][x]) + return(0); + if(split[y][x] != word[depth]) + return(0); + nb = 0; + if((dir == 0 || dir == 1) && + is_word(split, y - 1, x - 1, depth + 1, 1)) + nb++; + if((dir == 0 || dir == 2) && + is_word(split, y - 1, x, depth + 1, 2)) + nb++; + if((dir == 0 || dir == 3) && + is_word(split, y - 1, x + 1, depth + 1, 3)) + nb++; + if((dir == 0 || dir == 4) && + is_word(split, y, x + 1, depth + 1, 4)) + nb++; + if((dir == 0 || dir == 5) && + is_word(split, y + 1, x + 1, depth + 1, 5)) + nb++; + if((dir == 0 || dir == 6) && + is_word(split, y + 1, x, depth + 1, 6)) + nb++; + if((dir == 0 || dir == 7) && + is_word(split, y + 1, x - 1, depth + 1, 7)) + nb++; + if((dir == 0 || dir == 8) && + is_word(split, y, x - 1, depth + 1, 8)) + nb++; + return(nb); + +} + +long int resolve_part1(char *input, char **split) +{ + (void)input; + int i; + int j; + long int res; + + i = 0; + res = 0; + while(split[i]) + { + j = 0; + while(split[i][j]) + { + res += is_word(split, i, j, 0, 0); + j++; + } + i++; + } + i = 0; + return(res); +} diff --git a/2024/4/part2.c b/2024/4/part2.c new file mode 100644 index 0000000..acbb662 --- /dev/null +++ b/2024/4/part2.c @@ -0,0 +1,60 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* part2.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tomoron +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/07/17 23:03:36 by tomoron #+# #+# */ +/* Updated: 2024/12/04 10:48:38 by tomoron ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include +#include "libft/libft.h" + +static int is_word(char **split, int y, int x) +{ + if(split[y][x] != 'A') + return(0); + if(x == 0 || y == 0 || !split[y + 1]) + return(0); + if(((split[y - 1][x - 1] == 'M' && split[y + 1][x + 1] == 'S') || + (split[y - 1][x - 1] == 'S' && split[y + 1][x + 1] == 'M')) && + ((split[y - 1][x + 1] == 'S' && split[y + 1][x - 1] == 'M') || + (split[y - 1][x + 1] == 'M' && split[y + 1][x - 1] == 'S'))) + return(1); + return(0); + +} + +long int resolve_part2(char *input, char **split) +{ + (void)input; + int i; + int j; + long int res; + + i = 0; + res = 0; + while(split[i]) + { + j = 0; + while(split[i][j]) + { + if(is_word(split, i, j)) + { + printf("%c", split[i][j]); + res++; + } + else + printf("."); + j++; + } + printf("\n"); + i++; + } + i = 0; + return(res); +} diff --git a/2024/4/test b/2024/4/test new file mode 100644 index 0000000..1f4eda2 --- /dev/null +++ b/2024/4/test @@ -0,0 +1,10 @@ +MMMSXXMASM +MSAMXMSMSA +AMXSXMAAMM +MSAMASMSMX +XMASAMXAMM +XXAMMXXAMA +SMSMSASXSS +SAXAMASAAA +MAMMMXMMMM +MXMXAXMASX diff --git a/2024/5/Makefile b/2024/5/Makefile new file mode 100644 index 0000000..808a501 --- /dev/null +++ b/2024/5/Makefile @@ -0,0 +1,69 @@ +# **************************************************************************** # +# # +# ::: :::::::: # +# Makefile :+: :+: :+: # +# +:+ +:+ +:+ # +# By: tomoron +#+ +:+ +#+ # +# +#+#+#+#+#+ +#+ # +# Created: 2024/07/17 15:48:36 by tomoron #+# #+# # +# Updated: 2024/10/29 20:43:12 by tomoron ### ########.fr # +# # +# **************************************************************************** # + +SRCS = main.c\ + part1.c\ + part2.c + +OBJS = $(SRCS:.c=.o) + +FLAGS = -Wextra -Werror -Wall -g + +LIB = libft/libft.a + +all: $(LIB) a.out test + cp test cur_input + ./a.out + +f: $(LIB) a.out input + cp input cur_input + ./a.out + +test: + touch test + +input: + touch input + +cp: + cp part1.c part2.c + sed -i 's/resolve_part1/resolve_part2/g' part2.c + +$(LIB): libft + make -C libft + +libft: + cp -r ~/Desktop/aoc/libft . + +main.c: + cp ~/Desktop/aoc/main.c . + +part1.c: + cp ~/Desktop/aoc/part1.c . + +part2.c: + cp ~/Desktop/aoc/part2.c . + +a.out: main.c $(OBJS) $(LIB) + clang $(FLAGS) $(OBJS) $(LIB) + +clean: + rm -rf $(OBJS) + rm -rf libft + +fclean: clean + rm -rf a.out + +.c.o: + clang $(FLAGS) -c $< -o $@ + +.PHONY: t all clean fclean cp diff --git a/2024/5/main.c b/2024/5/main.c new file mode 100644 index 0000000..4f9b609 --- /dev/null +++ b/2024/5/main.c @@ -0,0 +1,67 @@ +#include +#include +#include +#include +#include "libft/libft.h" + +long int resolve_part1(char *input, char **split); +long int resolve_part2(char *input, char **split); + +char *get_input(char *file) +{ + int fd; + int rd_len; + char buffer[1000]; + int len; + char *res; + + fd = open(file, O_RDONLY); + len = 0; + if(fd < 0) + return(0); + rd_len = -1; + while(rd_len) + { + rd_len = read(fd, buffer, 1000); + len+= rd_len; + } + close(fd); + res = malloc(len + 1); + if(!res) + return(0); + fd = open(file, O_RDONLY); + if(fd < 0) + return(0); + rd_len = read(fd,res, len); + res[len] = 0; + close(fd); + return(res); +} + +int main(void) +{ + char *input; + char *input_cpy; + char **split; + + input = get_input("cur_input"); + input_cpy = ft_strdup(input); + if(!input || !input_cpy) + { + fprintf(stderr, "file read error\n"); + return(1); + } + split = ft_split(input, '\n'); + if(!split) + return(1); + printf("result part 1: %ld\n", resolve_part1(input, split)); + ft_free_str_arr(split); + split = ft_split(input_cpy, '\n'); + if(!split) + return(1); + printf("result part 2: %ld\n", resolve_part2(input, split)); + ft_free_str_arr(split); + free(input); + free(input_cpy); + return(0); +} diff --git a/2024/5/part1.c b/2024/5/part1.c new file mode 100644 index 0000000..73fbfff --- /dev/null +++ b/2024/5/part1.c @@ -0,0 +1,148 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* part1.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tomoron +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/07/17 23:03:36 by tomoron #+# #+# */ +/* Updated: 2024/12/05 08:50:09 by tomoron ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include +#include "libft/libft.h" + +typedef struct s_orders +{ + int left; + int right; + struct s_orders *next; +} t_orders; + +static void add_order(char *line, t_orders **orders) +{ + t_orders *new; + + new = malloc(sizeof(t_orders)); + new->left = ft_atoi(line); + while(ft_isdigit(*line)) + line++; + line++; + new->right = ft_atoi(line); + new->next = *orders; + *orders = new; +} + +static int count_char(char *str, char c) +{ + int res; + + res = 0; + while(*str) + { + if(*str == c) + res++; + str++; + } + return(res); +} + +static int *get_numbers(char *line, int *len) +{ + int *res; + int i; + + *len = count_char(line, ',') + 1; + res = malloc(*len * sizeof(int)); + i = 0; + while(*line) + { + res[i] = ft_atoi(line); + while(ft_isdigit(*line)) + line++; + if(*line == ',') + line++; + i++; + } + return(res); + +} + +static int respect_order(int *nbrs, t_orders *orders, int len) +{ + int i; + int j; +// int tmp; + + i = 0; + while(i < len) + { + j = 0; + while(j < len) + { + if(i != j) + { + if(nbrs[i] == orders->left && nbrs[j] == orders->right && i > j) + { + // tmp = nbrs[i]; + // nbrs[i] = nbrs[j]; + // nbrs[j] = tmp; + return(0); + } + } + j++; + } + i++; + } + return(1); +} + +static int is_ordered(int *nbrs, t_orders *orders, int len) +{ + + while(orders) + { + if(!respect_order(nbrs, orders, len)) + return(0); + orders = orders->next; + } + return(1); +} + +static long int check_line(char *line, t_orders *orders) +{ + int *nbrs; + int len; + int res; + + nbrs = get_numbers(line, &len); + if(!is_ordered(nbrs, orders, len)) + return(0); + + res = nbrs[(len / 2)]; + free(nbrs); + return(res); +} + +long int resolve_part1(char *input, char **split) +{ + (void)input; + t_orders *orders; + long int res; + + orders = 0; + while((*split)[2] == '|') + { + add_order(*split, &orders); + split++; + } + res = 0; + while(*split) + { + res += check_line(*split, orders); + split++; + } + return(res); +} diff --git a/2024/5/part2.c b/2024/5/part2.c new file mode 100644 index 0000000..a1be9ce --- /dev/null +++ b/2024/5/part2.c @@ -0,0 +1,153 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* part2.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tomoron +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/07/17 23:03:36 by tomoron #+# #+# */ +/* Updated: 2024/12/05 08:49:16 by tomoron ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include +#include "libft/libft.h" + +typedef struct s_orders +{ + int left; + int right; + struct s_orders *next; +} t_orders; + +static void add_order(char *line, t_orders **orders) +{ + t_orders *new; + + new = malloc(sizeof(t_orders)); + new->left = ft_atoi(line); + while(ft_isdigit(*line)) + line++; + line++; + new->right = ft_atoi(line); + new->next = *orders; + *orders = new; +} + +static int count_char(char *str, char c) +{ + int res; + + res = 0; + while(*str) + { + if(*str == c) + res++; + str++; + } + return(res); +} + +static int *get_numbers(char *line, int *len) +{ + int *res; + int i; + + *len = count_char(line, ',') + 1; + res = malloc(*len * sizeof(int)); + i = 0; + while(*line) + { + res[i] = ft_atoi(line); + while(ft_isdigit(*line)) + line++; + if(*line == ',') + line++; + i++; + } + return(res); + +} + +static int respect_order(int *nbrs, t_orders *orders, int len) +{ + int i; + int j; + int tmp; + + i = 0; + while(i < len) + { + j = 0; + while(j < len) + { + if(i != j) + { + if(nbrs[i] == orders->left && nbrs[j] == orders->right && i > j) + { + tmp = nbrs[i]; + nbrs[i] = nbrs[j]; + nbrs[j] = tmp; + return(0); + } + } + j++; + } + i++; + } + return(1); +} + +static int is_ordered(int *nbrs, t_orders *orders, int len) +{ + + while(orders) + { + if(!respect_order(nbrs, orders, len)) + return(0); + orders = orders->next; + } + return(1); +} + +static long int check_line(char *line, t_orders *orders) +{ + int *nbrs; + int len; + int res; + int err; + + nbrs = get_numbers(line, &len); + err = 0; + while(!is_ordered(nbrs, orders, len)) + err = 1; + if(err) + res = nbrs[(len / 2)]; + else + res = 0; + + free(nbrs); + return(res); +} + +long int resolve_part2(char *input, char **split) +{ + (void)input; + t_orders *orders; + long int res; + + orders = 0; + while((*split)[2] == '|') + { + add_order(*split, &orders); + split++; + } + res = 0; + while(*split) + { + res += check_line(*split, orders); + split++; + } + return(res); +} diff --git a/2024/5/test b/2024/5/test new file mode 100644 index 0000000..9d146d6 --- /dev/null +++ b/2024/5/test @@ -0,0 +1,28 @@ +47|53 +97|13 +97|61 +97|47 +75|29 +61|13 +75|53 +29|13 +97|29 +53|29 +61|53 +97|53 +61|29 +47|13 +75|47 +97|75 +47|61 +75|61 +47|29 +75|13 +53|13 + +75,47,61,53,29 +97,61,53,29,13 +75,29,13 +75,97,47,61,53 +61,13,29 +97,13,75,29,47