add day 13

This commit is contained in:
2024-12-13 23:48:16 +01:00
parent 53b1c38358
commit 4342ae314e
5 changed files with 295 additions and 0 deletions

69
2024/13/Makefile Normal file
View File

@ -0,0 +1,69 @@
# **************************************************************************** #
# #
# ::: :::::::: #
# Makefile :+: :+: :+: #
# +:+ +:+ +:+ #
# By: tomoron <tomoron@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# 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

67
2024/13/main.c Normal file
View File

@ -0,0 +1,67 @@
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#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);
}

71
2024/13/part1.c Normal file
View File

@ -0,0 +1,71 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* part1.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tomoron <tomoron@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/17 23:03:36 by tomoron #+# #+# */
/* Updated: 2024/12/13 23:41:42 by tomoron ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#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);
}

73
2024/13/part2.c Normal file
View File

@ -0,0 +1,73 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* part2.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tomoron <tomoron@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/17 23:03:36 by tomoron #+# #+# */
/* Updated: 2024/12/13 23:41:49 by tomoron ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#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);
}

15
2024/13/test Normal file
View File

@ -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