From 6d6bad0cbe783ddf8368a531df05b81c1c67e030 Mon Sep 17 00:00:00 2001 From: tomoron Date: Sun, 8 Dec 2024 13:49:16 +0100 Subject: [PATCH] add day 8 and add free on day 7 --- 2024/7/part1.c | 8 +++- 2024/7/part2.c | 8 +++- 2024/8/Makefile | 69 ++++++++++++++++++++++++++++ 2024/8/main.c | 67 +++++++++++++++++++++++++++ 2024/8/part1.c | 110 ++++++++++++++++++++++++++++++++++++++++++++ 2024/8/part2.c | 119 ++++++++++++++++++++++++++++++++++++++++++++++++ 2024/8/test | 10 ++++ 7 files changed, 389 insertions(+), 2 deletions(-) create mode 100644 2024/8/Makefile create mode 100644 2024/8/main.c create mode 100644 2024/8/part1.c create mode 100644 2024/8/part2.c create mode 100644 2024/8/test diff --git a/2024/7/part1.c b/2024/7/part1.c index a6d1769..dad9c63 100644 --- a/2024/7/part1.c +++ b/2024/7/part1.c @@ -6,7 +6,7 @@ /* By: tomoron +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/07/17 23:03:36 by tomoron #+# #+# */ -/* Updated: 2024/12/07 13:14:36 by tomoron ### ########.fr */ +/* Updated: 2024/12/08 13:38:45 by tomoron ### ########.fr */ /* */ /* ************************************************************************** */ @@ -77,9 +77,15 @@ static long int test_nbrs(long int test_val, int *nbrs, char *op, int len) i++; } if(!next_op(op, i - 1) && tmp != test_val) + { + free(nbrs); + free(op); return(0); + } } + free(nbrs); + free(op); return(test_val); } diff --git a/2024/7/part2.c b/2024/7/part2.c index 82a90fa..8c1ba4a 100644 --- a/2024/7/part2.c +++ b/2024/7/part2.c @@ -6,7 +6,7 @@ /* By: tomoron +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/07/17 23:03:36 by tomoron #+# #+# */ -/* Updated: 2024/12/07 13:17:09 by tomoron ### ########.fr */ +/* Updated: 2024/12/08 13:47:46 by tomoron ### ########.fr */ /* */ /* ************************************************************************** */ @@ -96,9 +96,15 @@ static long int test_nbrs(long int test_val, int *nbrs, char *op, int len) if(i == len) i--; if(!next_op(op, i - 1) && tmp != test_val) + { + free(op); + free(nbrs); return(0); + } } + free(op); + free(nbrs); return(test_val); } diff --git a/2024/8/Makefile b/2024/8/Makefile new file mode 100644 index 0000000..808a501 --- /dev/null +++ b/2024/8/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/8/main.c b/2024/8/main.c new file mode 100644 index 0000000..4f9b609 --- /dev/null +++ b/2024/8/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/8/part1.c b/2024/8/part1.c new file mode 100644 index 0000000..d228fff --- /dev/null +++ b/2024/8/part1.c @@ -0,0 +1,110 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* part1.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tomoron +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/07/17 23:03:36 by tomoron #+# #+# */ +/* Updated: 2024/12/08 13:37:33 by tomoron ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include +#include "libft/libft.h" + +static long int get_nb_antinode_char(char **map, char **locations, int limits[2], int ref_pos[2]) +{ + char c; + long int res; + int pos[2]; + int np[2]; + + res = 0; + c = map[ref_pos[0]][ref_pos[1]]; + pos[0] = 0; + while(map[pos[0]]) + { + pos[1] = 0; + while(map[pos[0]][pos[1]]) + { + if(ref_pos[0] != pos[0] && ref_pos[1] != pos[1] && map[pos[0]][pos[1]] == c) + { + np[0] = ref_pos[0] - (pos[0] - ref_pos[0]); + np[1] = ref_pos[1] - (pos[1] - ref_pos[1]); + if(np[0] >= 0 && np[0] < limits[0] && np[1] >= 0 && np[1] < limits[1] && locations[np[0]][np[1]] != '#') + { + res++; + locations[np[0]][np[1]] = '#'; + } + np[0] = pos[0] + (pos[0] - ref_pos[0]); + np[1] = pos[1] + (pos[1] - ref_pos[1]); + if(np[0] >= 0 && np[0] < limits[0] && np[1] >= 0 && np[1] < limits[1] && locations[np[0]][np[1]] != '#') + { + res++; + locations[np[0]][np[1]] = '#'; + } + } + pos[1]++; + } + pos[0]++; + } + map[ref_pos[0]][ref_pos[1]] = '.'; + return(res); +} + +static long int find_antinode(char **map, int height, int width) +{ + int x; + int y; + long int res; + char **locations; + + y = 0; + res = 0; + locations = malloc(height * sizeof(char *)); + while(y < height) + { + locations[y] = malloc(width); + ft_memset(locations[y], '.', width); + y++; + } + y = 0; + while(map[y]) + { + x = 0; + while(map[y][x]) + { + if(map[y][x] != '.') + res += get_nb_antinode_char(map, locations, (int [2]){height, width}, (int [2]){y, x}); + x++; + } + y++; + } + y = 0; + while(y < height) + { + free(locations[y]); + y++; + } + free(locations); + return(res); +} + +long int resolve_part1(char *input, char **split) +{ + (void)input; + int height; + int width; + long int res; + + height = 0; + while(split[height]) + height++; + width = 0; + while(split[0][width]) + width++; + res = find_antinode(split, height, width); + return(res); +} diff --git a/2024/8/part2.c b/2024/8/part2.c new file mode 100644 index 0000000..ea41575 --- /dev/null +++ b/2024/8/part2.c @@ -0,0 +1,119 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* part2.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tomoron +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/07/17 23:03:36 by tomoron #+# #+# */ +/* Updated: 2024/12/08 13:37:39 by tomoron ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include +#include "libft/libft.h" + +static long int get_nb_antinode_char(char **map, char **locations, int limits[2], int ref_pos[2]) +{ + char c; + long int res; + int pos[2]; + int np[2]; + int diff[2]; + + res = 0; + c = map[ref_pos[0]][ref_pos[1]]; + pos[0] = 0; + while(map[pos[0]]) + { + pos[1] = 0; + while(map[pos[0]][pos[1]]) + { + if(ref_pos[0] != pos[0] && ref_pos[1] != pos[1] && map[pos[0]][pos[1]] == c) + { + diff[0] = pos[0] - ref_pos[0]; + diff[1] = pos[1] - ref_pos[1]; + np[0] = ref_pos[0]; + np[1] = ref_pos[1]; + while(np[0] >= 0 && np[0] < limits[0] && np[1] >= 0 && np[1] < limits[1]) + { + if(locations[np[0]][np[1]] != '#') + res++; + locations[np[0]][np[1]] = '#'; + np[0] -= diff[0]; + np[1] -= diff[1]; + } + np[0] = pos[0]; + np[1] = pos[1]; + while(np[0] >= 0 && np[0] < limits[0] && np[1] >= 0 && np[1] < limits[1]) + { + if(locations[np[0]][np[1]] != '#') + res++; + locations[np[0]][np[1]] = '#'; + np[0] += diff[0]; + np[1] += diff[1]; + } + } + pos[1]++; + } + pos[0]++; + } + map[ref_pos[0]][ref_pos[1]] = '.'; + return(res); +} + +static long int find_antinode(char **map, int height, int width) +{ + int x; + int y; + long int res; + char **locations; + + y = 0; + res = 0; + locations = malloc(height * sizeof(char *)); + while(y < height) + { + locations[y] = malloc(width); + ft_memset(locations[y], '.', width); + y++; + } + y = 0; + while(map[y]) + { + x = 0; + while(map[y][x]) + { + if(map[y][x] != '.') + res += get_nb_antinode_char(map, locations, (int [2]){height, width}, (int [2]){y, x}); + x++; + } + y++; + } + y = 0; + while(y < height) + { + free(locations[y]); + y++; + } + free(locations); + return(res); +} + +long int resolve_part2(char *input, char **split) +{ + (void)input; + int height; + int width; + long int res; + + height = 0; + while(split[height]) + height++; + width = 0; + while(split[0][width]) + width++; + res = find_antinode(split, height, width); + return(res); +} diff --git a/2024/8/test b/2024/8/test new file mode 100644 index 0000000..b8439c3 --- /dev/null +++ b/2024/8/test @@ -0,0 +1,10 @@ +T......... +...T...... +.T........ +.......... +.......... +.......... +.......... +.......... +.......... +..........