From 4342ae314ead5a31d3cc576cff5da37ca4a501a2 Mon Sep 17 00:00:00 2001 From: tomoron Date: Fri, 13 Dec 2024 23:48:16 +0100 Subject: [PATCH] add day 13 --- 2024/13/Makefile | 69 +++++++++++++++++++++++++++++++++++++++++++++ 2024/13/main.c | 67 ++++++++++++++++++++++++++++++++++++++++++++ 2024/13/part1.c | 71 ++++++++++++++++++++++++++++++++++++++++++++++ 2024/13/part2.c | 73 ++++++++++++++++++++++++++++++++++++++++++++++++ 2024/13/test | 15 ++++++++++ 5 files changed, 295 insertions(+) create mode 100644 2024/13/Makefile create mode 100644 2024/13/main.c create mode 100644 2024/13/part1.c create mode 100644 2024/13/part2.c create mode 100644 2024/13/test diff --git a/2024/13/Makefile b/2024/13/Makefile new file mode 100644 index 0000000..d37dd83 --- /dev/null +++ b/2024/13/Makefile @@ -0,0 +1,69 @@ +# **************************************************************************** # +# # +# ::: :::::::: # +# Makefile :+: :+: :+: # +# +:+ +:+ +:+ # +# By: tomoron +#+ +:+ +#+ # +# +#+#+#+#+#+ +#+ # +# Created: 2024/07/17 15:48:36 by tomoron #+# #+# # +# Updated: 2024/12/13 20:53:42 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) -lm + +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/13/main.c b/2024/13/main.c new file mode 100644 index 0000000..4f9b609 --- /dev/null +++ b/2024/13/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/13/part1.c b/2024/13/part1.c new file mode 100644 index 0000000..7f1290d --- /dev/null +++ b/2024/13/part1.c @@ -0,0 +1,71 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* part1.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tomoron +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/07/17 23:03:36 by tomoron #+# #+# */ +/* Updated: 2024/12/13 23:41:42 by tomoron ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include +#include +#include "libft/libft.h" + +static void get_pos(char *line, long int *res) +{ + while(*line && *line != 'X') + line++; + if(*line) + { + line += 2; + res[0] = atoi(line); + } + while(*line && *line != 'Y') + line++; + if(*line) + { + line += 2; + res[1] = atoi(line); + } +} + +static long int get_score(long int btn_a[2],long int btn_b[2],long int prize[2]) +{ + double a; + double b; + long int res[2]; + + a = ((prize[1] * btn_b[0]) - (prize[0] * btn_b[1])) / ((btn_a[1] * btn_b[0]) - (btn_a[0] * btn_b[1])); + b = (((-btn_a[0] * a) + prize[0]) / btn_b[0]); + res[0] = a; + res[1] = b; + if((btn_a[0] * res[0]) + (btn_b[0] * res[1]) != prize[0] || (btn_a[1] * res[0]) + (btn_b[1] * res[1]) != prize[1]) + return(0); + return((a * 3) + b); +} + +long int resolve_part1(char *input, char **split) +{ + (void)input; + long int btn_a[2]; + long int btn_b[2]; + long int prize[2]; + long int res; + + res = 0; + while(*split) + { + get_pos(*split, btn_a); + split++; + get_pos(*split, btn_b); + split++; + get_pos(*split, prize); + split++; + res += get_score(btn_a, btn_b, prize); + } + return(res); +} diff --git a/2024/13/part2.c b/2024/13/part2.c new file mode 100644 index 0000000..bbeb7c2 --- /dev/null +++ b/2024/13/part2.c @@ -0,0 +1,73 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* part2.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tomoron +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/07/17 23:03:36 by tomoron #+# #+# */ +/* Updated: 2024/12/13 23:41:49 by tomoron ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include +#include +#include "libft/libft.h" + +static void get_pos(char *line, long int *res) +{ + while(*line && *line != 'X') + line++; + if(*line) + { + line += 2; + res[0] = atoi(line); + } + while(*line && *line != 'Y') + line++; + if(*line) + { + line += 2; + res[1] = atoi(line); + } +} + +static long int get_score(long int btn_a[2],long int btn_b[2],long int prize[2]) +{ + double a; + double b; + long int res[2]; + + a = ((prize[1] * btn_b[0]) - (prize[0] * btn_b[1])) / ((btn_a[1] * btn_b[0]) - (btn_a[0] * btn_b[1])); + b = (((-btn_a[0] * a) + prize[0]) / btn_b[0]); + res[0] = a; + res[1] = b; + if((btn_a[0] * res[0]) + (btn_b[0] * res[1]) != prize[0] || (btn_a[1] * res[0]) + (btn_b[1] * res[1]) != prize[1]) + return(0); + return((a * 3) + b); +} + +long int resolve_part2(char *input, char **split) +{ + (void)input; + long int btn_a[2]; + long int btn_b[2]; + long int prize[2]; + long int res; + + res = 0; + while(*split) + { + get_pos(*split, btn_a); + split++; + get_pos(*split, btn_b); + split++; + get_pos(*split, prize); + split++; + prize[0] += 10000000000000; + prize[1] += 10000000000000; + res += get_score(btn_a, btn_b, prize); + } + return(res); +} diff --git a/2024/13/test b/2024/13/test new file mode 100644 index 0000000..912f482 --- /dev/null +++ b/2024/13/test @@ -0,0 +1,15 @@ +Button A: X+94, Y+34 +Button B: X+22, Y+67 +Prize: X=8400, Y=5400 + +Button A: X+26, Y+66 +Button B: X+67, Y+21 +Prize: X=12748, Y=12176 + +Button A: X+17, Y+86 +Button B: X+84, Y+37 +Prize: X=7870, Y=6450 + +Button A: X+69, Y+23 +Button B: X+27, Y+71 +Prize: X=18641, Y=10279