remove inputs

This commit is contained in:
2024-12-02 12:32:14 +01:00
commit 60acac0231
247 changed files with 9071 additions and 0 deletions

69
2024/1/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/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

67
2024/1/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);
}

98
2024/1/part1.c Normal file
View File

@ -0,0 +1,98 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* part1.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tomoron <tomoron@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/17 23:03:36 by tomoron #+# #+# */
/* Updated: 2024/12/01 12:36:03 by tomoron ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdio.h>
#include <stdlib.h>
#include "libft/libft.h"
static void ft_sort(int *list, int len)
{
int i;
int j;
int tmp;
i = 0;
while(i < len - 1)
{
j = 0;
while(j < len - 1)
{
if(list[j] > list[j + 1])
{
tmp = list[j];
list[j] = list[j + 1];
list[j + 1] = tmp;
}
j++;
}
i++;
}
}
static void get_numbers(int *left, int *right, char **split)
{
int i;
int j;
i = 0;
while(*split)
{
j = 0;
*(left + i) = ft_atoi(*split);
while((*split)[j] >= '0' && (*split)[j] <= '9')
j++;
while((*split)[j] == ' ')
j++;
*(right + i) = ft_atoi(*split + j);
i++;
split++;
}
}
long int resolve_part1(char *input, char **split)
{
int len;
int *left;
int *right;
long int res;
int i;
int tmp;
len = 0;
i = 0;
while(split[len])
len++;
left = malloc(sizeof(int) * len);
right = malloc(sizeof(int) * len);
if(!left || !right)
{
free(left);
free(right);
return(-1);
}
get_numbers(left, right, split);
ft_sort(left, len);
ft_sort(right, len);
res = 0;
while(i < len)
{
tmp = left[i] - right[i];
if(tmp < 0)
tmp *= -1;
res += tmp;
i++;
}
free(left);
free(right);
(void)input;
return(res);
}

86
2024/1/part2.c Normal file
View File

@ -0,0 +1,86 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* part2.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tomoron <tomoron@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/17 23:03:36 by tomoron #+# #+# */
/* Updated: 2024/12/01 12:36:05 by tomoron ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdio.h>
#include <stdlib.h>
#include "libft/libft.h"
static void get_numbers(int *left, int *right, char **split)
{
int i;
int j;
i = 0;
while(*split)
{
j = 0;
*(left + i) = ft_atoi(*split);
while((*split)[j] >= '0' && (*split)[j] <= '9')
j++;
while((*split)[j] == ' ')
j++;
*(right + i) = ft_atoi(*split + j);
i++;
split++;
}
}
int count_nb(int nb, int *list, int len)
{
int i;
int res;
i = 0;
res = 0;
while(i < len)
{
if(list[i] == nb)
res++;
i++;
}
return(res);
}
long int resolve_part2(char *input, char **split)
{
int len;
int *left;
int *right;
long int res;
int i;
int tmp;
len = 0;
i = 0;
while(split[len])
len++;
left = malloc(sizeof(int) * len);
right = malloc(sizeof(int) * len);
if(!left || !right)
{
free(left);
free(right);
return(-1);
}
get_numbers(left, right, split);
res = 0;
while(i < len)
{
tmp = count_nb(left[i], right, len);
res += left[i] * tmp;
i++;
}
free(left);
free(right);
(void)input;
return(res);
}

6
2024/1/test Normal file
View File

@ -0,0 +1,6 @@
3 4
4 3
2 5
1 3
3 9
3 3