add day 23 part 2 (leaks a lot)

This commit is contained in:
2024-12-24 00:27:26 +01:00
parent fae7f6ddb7
commit f76dbf2355
5 changed files with 403 additions and 4 deletions

69
2024/23/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/23/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);
}

View File

@ -6,7 +6,7 @@
/* By: tomoron <tomoron@student.42.fr> +#+ +:+ +#+ */ /* By: tomoron <tomoron@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/17 23:03:36 by tomoron #+# #+# */ /* Created: 2024/07/17 23:03:36 by tomoron #+# #+# */
/* Updated: 2024/12/23 22:26:48 by tomoron ### ########.fr */ /* Updated: 2024/12/23 23:20:04 by tomoron ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -111,7 +111,7 @@ static int is_connected(t_computer *c1, t_computer *c2)
return(0); return(0);
} }
int is_done(t_done *done, t_computer *c1, t_computer *c2, t_computer *c3) static int is_done(t_done *done, t_computer *c1, t_computer *c2, t_computer *c3)
{ {
while(done) while(done)
{ {
@ -122,7 +122,7 @@ int is_done(t_done *done, t_computer *c1, t_computer *c2, t_computer *c3)
return(0); return(0);
} }
void add_done(t_done **done, t_computer *c1, t_computer *c2, t_computer *c3) static void add_done(t_done **done, t_computer *c1, t_computer *c2, t_computer *c3)
{ {
t_done *new; t_done *new;
@ -134,7 +134,7 @@ void add_done(t_done **done, t_computer *c1, t_computer *c2, t_computer *c3)
*done = new; *done = new;
} }
int get_res(t_computer *input) static int get_res(t_computer *input)
{ {
t_connected *tmp1; t_connected *tmp1;
t_connected *tmp2; t_connected *tmp2;

257
2024/23/part2.c Normal file
View File

@ -0,0 +1,257 @@
/* ************************************************ ************************** */
/* */
/* ::: :::::::: */
/* part2.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tomoron <tomoron@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/17 23:03:36 by tomoron #+# #+# */
/* Updated: 2024/12/24 00:26:43 by tomoron ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "libft/libft.h"
typedef struct s_connected t_connected;
typedef struct s_computer
{
t_connected *connected;
char name[3];
struct s_computer *next;
} t_computer;
typedef struct s_done
{
t_computer *lst[3];
struct s_done *next;
} t_done;
typedef struct s_connected
{
t_computer *comp;
struct s_connected *next;
} t_connected;
typedef struct s_found
{
t_computer *comp;
struct s_found *next;
} t_found;
static t_computer *get_computer(t_computer *list, char *name)
{
while(list)
{
if(!ft_strcmp(list->name, name))
return(list);
list = list->next;
}
return(0);
}
static void add_to_computer(t_computer **computers, char *computer, char *other)
{
t_computer *tmp;
t_connected *new;
tmp = *computers;
while(tmp)
{
if(!ft_strcmp(tmp->name, computer))
break;
tmp = tmp->next;
}
if(!tmp)
{
tmp = malloc(sizeof(t_computer));
memcpy(tmp->name, computer, 3);
tmp->next = *computers;
tmp->connected = 0;
*computers = tmp;
}
new = malloc(sizeof(t_connected));
new->comp = get_computer(*computers, other);
new->next = tmp->connected;
tmp->connected = new;
}
static void parse_line(t_computer **list, char *line)
{
char c1[3];
char c2[3];
c1[0] = line[0];
c1[1] = line[1];
c1[2] = 0;
c2[0] = line[3];
c2[1] = line[4];
c2[2] = 0;
add_to_computer(list, c1, c2);
add_to_computer(list, c2, c1);
}
static int is_in_found(t_found *found, t_computer *c)
{
while(found)
{
if(found->comp == c)
return(1);
found = found->next;
}
return(0);
}
static void add_found(t_found **found, t_computer *c)
{
t_found *new;
new = malloc(sizeof(t_found));
new->comp = c;
new->next = *found;
*found = new;
}
static int is_connected(t_computer *c1, t_computer *c2)
{
t_connected *tmp;
if(!c1 || !c2)
return(0);
tmp = c1->connected;
while(tmp)
{
if(tmp->comp == c2)
return(1);
tmp = tmp->next;
}
tmp = c2->connected;
while(tmp)
{
if(tmp->comp == c1)
return(1);
tmp = tmp->next;
}
return(0);
}
static void find_connected(t_found **found,t_found **res, t_computer *comp)
{
t_connected *lst;
t_found *tmp;
if(!comp || is_in_found(*found, comp))
return;
add_found(found, comp);
if(!*res)
add_found(res, comp);
tmp = *res;
while(tmp)
{
if(!is_connected(tmp->comp, comp))
break;
tmp = tmp->next;
}
if(!tmp)
add_found(res, comp);
lst = comp->connected;
while(lst)
{
find_connected(found,res, lst->comp);
lst = lst->next;
}
}
static void sort_list(t_found *found)
{
t_found *cur;
t_found *start;
char tmp;
start = found;
cur = found;
while(cur)
{
found = start;
while(found->next)
{
if(ft_strcmp(found->comp->name, found->next->comp->name) > 0)
{
tmp = found->comp->name[0];
found->comp->name[0] = found->next->comp->name[0];
found->next->comp->name[0] = tmp;
tmp = found->comp->name[1];
found->comp->name[1] = found->next->comp->name[1];
found->next->comp->name[1] = tmp;
tmp = found->comp->name[2];
found->comp->name[2] = found->next->comp->name[2];
found->next->comp->name[2] = tmp;
}
found = found->next;
}
cur = cur->next;
}
}
static int get_res(t_computer *input)
{
t_found *found;
t_found *res;
t_found *tmp;
int i;
int max_len;
res = 0;
max_len = 0;
while(input)
{
found = 0;
tmp = 0;
find_connected(&tmp,&found, input);
i = 0;
tmp = found;
while(tmp)
{
tmp = tmp->next;
i++;
}
if(i > max_len)
{
max_len = i;
res = found;
}
input = input->next;
}
i = 0;
while(res)
{
sort_list(res);
printf("%s", res->comp->name);
if(res->next)
printf(",");
res = res->next;
i++;
}
printf("\n");
return(0);
}
long int resolve_part2(char *line, char **split)
{
(void)line;
t_computer *input;
int res;
res = 0;
input = 0;
while(*split)
{
parse_line(&input, *split);
split++;
}
res = get_res(input);
return(res);
}

6
2024/23/test Normal file
View File

@ -0,0 +1,6 @@
ka-co
ta-co
de-co
ta-ka
de-ta
ka-de