From f94d1948d629c057d7a6057179d1d444b46e3d69 Mon Sep 17 00:00:00 2001 From: tomoron Date: Tue, 10 Dec 2024 12:36:52 +0100 Subject: [PATCH] add day 10 --- 2024/10/Makefile | 69 ++++++++++++++++++++++++++++++++ 2024/10/main.c | 67 +++++++++++++++++++++++++++++++ 2024/10/part1.c | 101 +++++++++++++++++++++++++++++++++++++++++++++++ 2024/10/part2.c | 58 +++++++++++++++++++++++++++ 2024/10/test | 8 ++++ 5 files changed, 303 insertions(+) create mode 100644 2024/10/Makefile create mode 100644 2024/10/main.c create mode 100644 2024/10/part1.c create mode 100644 2024/10/part2.c create mode 100644 2024/10/test diff --git a/2024/10/Makefile b/2024/10/Makefile new file mode 100644 index 0000000..808a501 --- /dev/null +++ b/2024/10/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/10/main.c b/2024/10/main.c new file mode 100644 index 0000000..4f9b609 --- /dev/null +++ b/2024/10/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/10/part1.c b/2024/10/part1.c new file mode 100644 index 0000000..35424ef --- /dev/null +++ b/2024/10/part1.c @@ -0,0 +1,101 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* part1.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tomoron +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/07/17 23:03:36 by tomoron #+# #+# */ +/* Updated: 2024/12/10 12:31:18 by tomoron ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include +#include "libft/libft.h" + +static long int find_trails(char **map, int pos[2], char **locations) +{ + char c; + long int res; + + c = map[pos[0]][pos[1]]; + res = 0; + if(c == '9' && !locations[pos[0]][pos[1]]) + { + locations[pos[0]][pos[1]] = 1; + return(1); + } + if(map[pos[0] + 1] && map[pos[0] + 1][pos[1]] == c + 1) + res += find_trails(map, (int [2]){pos[0] + 1, pos[1]}, locations); + if(pos[0] - 1 >= 0 && map[pos[0] - 1][pos[1]] == c + 1) + res += find_trails(map, (int [2]){pos[0] - 1, pos[1]}, locations); + if(map[pos[0]][pos[1] + 1] && map[pos[0]][pos[1] + 1] == c + 1) + res += find_trails(map, (int [2]){pos[0], pos[1] + 1}, locations); + if(pos[1] - 1 >= 0 && map[pos[0]][pos[1] - 1] == c + 1) + res += find_trails(map, (int [2]){pos[0], pos[1] - 1}, locations); + return(res); +} + +static void reset_locations(char **locations, int height, int width) +{ + int i; + + i = 0; + while(i < height) + { + ft_bzero(locations[i], width); + i++; + } +} + +static char **create_map(char **split, int *height, int *width) +{ + char **res; + + *height = 0; + *width = 0; + while(split[*height]) + (*height)++; + while(split[0][*width]) + (*width)++; + res = malloc(*height * sizeof(char *)); + *height = 0; + while(split[*height]) + { + res[*height] = malloc(*width); + (*height)++; + } + reset_locations(res, *height, *width); + return(res); +} + +long int resolve_part1(char *input, char **split) +{ + (void)input; + int i; + int j; + long int res; + char **locations; + int height; + int width; + + locations = create_map(split, &height, &width); + i = 0; + res = 0; + while(split[i]) + { + j = 0; + while(split[i][j]) + { + if(split[i][j] == '0') + { + reset_locations(locations, height, width); + res += find_trails(split, (int [2]){i, j}, locations); + } + j++; + } + i++; + } + return(res); +} diff --git a/2024/10/part2.c b/2024/10/part2.c new file mode 100644 index 0000000..5af3a65 --- /dev/null +++ b/2024/10/part2.c @@ -0,0 +1,58 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* part2.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tomoron +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/07/17 23:03:36 by tomoron #+# #+# */ +/* Updated: 2024/12/10 12:32:48 by tomoron ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include +#include "libft/libft.h" + +static long int find_trails(char **map, int pos[2]) +{ + char c; + long int res; + + c = map[pos[0]][pos[1]]; + res = 0; + if(c == '9') + return(1); + if(map[pos[0] + 1] && map[pos[0] + 1][pos[1]] == c + 1) + res += find_trails(map, (int [2]){pos[0] + 1, pos[1]}); + if(pos[0] - 1 >= 0 && map[pos[0] - 1][pos[1]] == c + 1) + res += find_trails(map, (int [2]){pos[0] - 1, pos[1]}); + if(map[pos[0]][pos[1] + 1] && map[pos[0]][pos[1] + 1] == c + 1) + res += find_trails(map, (int [2]){pos[0], pos[1] + 1}); + if(pos[1] - 1 >= 0 && map[pos[0]][pos[1] - 1] == c + 1) + res += find_trails(map, (int [2]){pos[0], pos[1] - 1}); + return(res); +} + +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(split[i][j] == '0') + res += find_trails(split, (int [2]){i, j}); + j++; + } + i++; + } + return(res); +} diff --git a/2024/10/test b/2024/10/test new file mode 100644 index 0000000..cada9b3 --- /dev/null +++ b/2024/10/test @@ -0,0 +1,8 @@ +89010123 +78121874 +87430965 +96549874 +45678903 +32019012 +01329801 +10456732