From 7288a95bb9bf0fa7bf35777635546b7b46ab90e5 Mon Sep 17 00:00:00 2001 From: tomoron Date: Sat, 6 Dec 2025 16:22:04 +0100 Subject: [PATCH] add day 6 --- 2025/6/Makefile | 69 +++++++++++++++++++++++++++++++++++++ 2025/6/main.c | 67 +++++++++++++++++++++++++++++++++++ 2025/6/part1.c | 92 +++++++++++++++++++++++++++++++++++++++++++++++++ 2025/6/part2.c | 79 ++++++++++++++++++++++++++++++++++++++++++ 2025/6/test | 4 +++ 5 files changed, 311 insertions(+) create mode 100644 2025/6/Makefile create mode 100644 2025/6/main.c create mode 100644 2025/6/part1.c create mode 100644 2025/6/part2.c create mode 100644 2025/6/test diff --git a/2025/6/Makefile b/2025/6/Makefile new file mode 100644 index 0000000..5fad37f --- /dev/null +++ b/2025/6/Makefile @@ -0,0 +1,69 @@ +# **************************************************************************** # +# # +# ::: :::::::: # +# Makefile :+: :+: :+: # +# +:+ +:+ +:+ # +# By: tomoron +#+ +:+ +#+ # +# +#+#+#+#+#+ +#+ # +# Created: 2024/07/17 15:48:36 by tomoron #+# #+# # +# Updated: 2025/12/02 14:14:04 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 ../../libft . + +main.c: + cp ../../main.c . + +part1.c: + cp ../../part1.c . + +part2.c: + cp ../../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/2025/6/main.c b/2025/6/main.c new file mode 100644 index 0000000..4f9b609 --- /dev/null +++ b/2025/6/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/2025/6/part1.c b/2025/6/part1.c new file mode 100644 index 0000000..61f962b --- /dev/null +++ b/2025/6/part1.c @@ -0,0 +1,92 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* part1.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tomoron +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/07/17 23:03:36 by tomoron #+# #+# */ +/* Updated: 2025/12/06 15:54:24 by tomoron ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include +#include "libft/libft.h" + +long int get_nb(char *line, int index) +{ + int i = 0; + + while(i < index && *line) + { + while(*line == ' ') + line++; + while(ft_isdigit(*line) || *line == '+' || *line == '*') + line++; + i++; + } + if(!*line) + return(0); + while(*line == ' ') + line++; + if(*line == '*') + return(-1); + if(*line == '+') + return(-2); + return(ft_atoi(line)); + +} + +static char *get_ll(char **split) +{ + char *tmp; + + tmp = 0; + while(*(split + 1)) + split++; + + tmp = *split; + *split = 0; + return(tmp); +} + +long int resolve_part1(char *input, char **split) +{ + (void)input; + char **tmp; + char *last_line; + long int ret; + long int res; + long int cur; + long int op; + int i = 0; + + res = 0; + last_line = get_ll(split); + while(1) + { + tmp = split; + op = get_nb(last_line, i); + cur = 0; + while(*tmp) + { + ret = get_nb(*tmp, i); + if(ret == 0) + return(res); + if(cur) + { + if(op == -1) + cur *= ret; + else + cur += ret; + } + else + cur = ret; + tmp++; + } + res += cur; + i++; + } + return(0); +} diff --git a/2025/6/part2.c b/2025/6/part2.c new file mode 100644 index 0000000..682b329 --- /dev/null +++ b/2025/6/part2.c @@ -0,0 +1,79 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* part2.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tomoron +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/07/17 23:03:36 by tomoron #+# #+# */ +/* Updated: 2025/12/06 16:16:55 by tomoron ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include +#include "libft/libft.h" + +static char *get_ll(char **split) +{ + char *tmp; + + tmp = 0; + while(*(split + 1)) + split++; + + tmp = *split; + *split = 0; + return(tmp); +} + +long int resolve_part2(char *input, char **split) +{ + (void)input; + char **tmp; + char *last_line; + long int nb; + long int res; + long int cur; + long int op; + int i = 0; + + res = 0; + last_line = get_ll(split); + cur = 0; + while(split[0][i]) + { + if(last_line[i] == '+') + op = 1; + if(last_line[i] == '*') + op = 0; + tmp = split; + nb = 0; + while(*tmp) + { + if(ft_isdigit((*tmp)[i])) + { + nb *= 10; + nb += (*tmp)[i] - '0'; + } + tmp++; + } + if(nb == 0) + { + res += cur; + cur = 0; + } + if(!cur) + cur = nb; + else + { + if(op) + cur += nb; + else + cur *= nb; + } + i++; + } + res+=cur; + return(res); +} diff --git a/2025/6/test b/2025/6/test new file mode 100644 index 0000000..337b837 --- /dev/null +++ b/2025/6/test @@ -0,0 +1,4 @@ +123 328 51 64 + 45 64 387 23 + 6 98 215 314 +* + * +