add day 8
This commit is contained in:
69
2025/8/Makefile
Normal file
69
2025/8/Makefile
Normal file
@ -0,0 +1,69 @@
|
|||||||
|
# **************************************************************************** #
|
||||||
|
# #
|
||||||
|
# ::: :::::::: #
|
||||||
|
# Makefile :+: :+: :+: #
|
||||||
|
# +:+ +:+ +:+ #
|
||||||
|
# By: tomoron <tomoron@student.42.fr> +#+ +:+ +#+ #
|
||||||
|
# +#+#+#+#+#+ +#+ #
|
||||||
|
# Created: 2024/07/17 15:48:36 by tomoron #+# #+# #
|
||||||
|
# Updated: 2025/12/09 16:21:44 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) -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
2025/8/main.c
Normal file
67
2025/8/main.c
Normal 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);
|
||||||
|
}
|
||||||
202
2025/8/part1.c
Normal file
202
2025/8/part1.c
Normal file
@ -0,0 +1,202 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* part1.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: tomoron <tomoron@student.42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/07/17 23:03:36 by tomoron #+# #+# */
|
||||||
|
/* Updated: 2025/12/09 19:49:03 by tomoron ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <math.h>
|
||||||
|
#include "libft/libft.h"
|
||||||
|
|
||||||
|
|
||||||
|
typedef struct s_conn
|
||||||
|
{
|
||||||
|
struct s_box *box;
|
||||||
|
struct s_conn *next;
|
||||||
|
} t_conn;
|
||||||
|
|
||||||
|
typedef struct s_box
|
||||||
|
{
|
||||||
|
int x;
|
||||||
|
int y;
|
||||||
|
int z;
|
||||||
|
t_conn *conn;
|
||||||
|
int exp;
|
||||||
|
} t_box;
|
||||||
|
|
||||||
|
|
||||||
|
static void parse_boxes(t_box *boxes, char **split)
|
||||||
|
{
|
||||||
|
long int i;
|
||||||
|
char *line;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
while(split[i])
|
||||||
|
{
|
||||||
|
line = split[i];
|
||||||
|
boxes[i].x = ft_atoi(line);
|
||||||
|
while(ft_isdigit(*line))
|
||||||
|
line++;
|
||||||
|
line++;
|
||||||
|
boxes[i].y = ft_atoi(line);
|
||||||
|
while(ft_isdigit(*line))
|
||||||
|
line++;
|
||||||
|
line++;
|
||||||
|
boxes[i].z = ft_atoi(line);
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static double box_dist(t_box *a, t_box *b)
|
||||||
|
{
|
||||||
|
double r;
|
||||||
|
|
||||||
|
r = pow(b->x - a->x , 2) + pow(b->y - a->y, 2) + pow(b->z - a->z, 2);
|
||||||
|
return(sqrt(r));
|
||||||
|
}
|
||||||
|
|
||||||
|
static double is_connected(t_box *a, t_box *b)
|
||||||
|
{
|
||||||
|
t_conn *tmp;
|
||||||
|
|
||||||
|
tmp = a->conn;
|
||||||
|
while(tmp)
|
||||||
|
{
|
||||||
|
if(tmp->box == b)
|
||||||
|
return(1);
|
||||||
|
tmp = tmp->next;
|
||||||
|
}
|
||||||
|
return(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void connect_boxes(t_box *a, t_box *b)
|
||||||
|
{
|
||||||
|
t_conn *conn;
|
||||||
|
|
||||||
|
conn = malloc(sizeof(t_conn) * 2);
|
||||||
|
if(!conn)
|
||||||
|
return;
|
||||||
|
conn->box = b;
|
||||||
|
conn->next = a->conn;
|
||||||
|
a->conn = conn;
|
||||||
|
|
||||||
|
(conn + 1)->box = a;
|
||||||
|
(conn + 1)->next = b->conn;
|
||||||
|
b->conn = (conn + 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void connect_closest(t_box *boxes, int len)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
int j;
|
||||||
|
t_box *closest;
|
||||||
|
t_box *closest2;
|
||||||
|
double closest_dist;
|
||||||
|
i = 0;
|
||||||
|
closest = 0;
|
||||||
|
closest2 = 0;
|
||||||
|
closest_dist = 999999999999;
|
||||||
|
while(i <len)
|
||||||
|
{
|
||||||
|
j = -1;
|
||||||
|
while(++j < len)
|
||||||
|
{
|
||||||
|
if(i == j)
|
||||||
|
continue ;
|
||||||
|
if(is_connected(boxes + i, boxes + j))
|
||||||
|
continue ;
|
||||||
|
if(box_dist(boxes + i, boxes + j) < closest_dist)
|
||||||
|
{
|
||||||
|
closest = boxes + i;
|
||||||
|
closest2 = boxes + j;
|
||||||
|
closest_dist = box_dist(boxes + i, boxes + j);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
if(!closest)
|
||||||
|
return ;
|
||||||
|
connect_boxes(closest, closest2);
|
||||||
|
}
|
||||||
|
|
||||||
|
static long int get_net_size(t_box *box)
|
||||||
|
{
|
||||||
|
long int res;
|
||||||
|
t_conn *tmp;
|
||||||
|
|
||||||
|
if(box->exp)
|
||||||
|
return(0);
|
||||||
|
|
||||||
|
box->exp = 1;
|
||||||
|
res = 1;
|
||||||
|
tmp = box->conn;
|
||||||
|
while(tmp)
|
||||||
|
{
|
||||||
|
res += get_net_size(tmp->box);
|
||||||
|
tmp = tmp->next;
|
||||||
|
}
|
||||||
|
return(res);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void add_to_top(long int nb, long int *top)
|
||||||
|
{
|
||||||
|
if(nb > top[0])
|
||||||
|
{
|
||||||
|
top[2] = top[1];
|
||||||
|
top[1] = top[0];
|
||||||
|
top[0] = nb;
|
||||||
|
}
|
||||||
|
else if(nb > top[1])
|
||||||
|
{
|
||||||
|
top[1] = top[0];
|
||||||
|
top[1] = nb;
|
||||||
|
}
|
||||||
|
else if(nb > top[2])
|
||||||
|
top[2] = nb;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
long int resolve_part1(char *input, char **split)
|
||||||
|
{
|
||||||
|
(void)input;
|
||||||
|
long int len;
|
||||||
|
t_box *boxes;
|
||||||
|
long int i;
|
||||||
|
long int tmp;
|
||||||
|
long int largest[3];
|
||||||
|
|
||||||
|
len = 0;
|
||||||
|
while(split[len])
|
||||||
|
len++;
|
||||||
|
|
||||||
|
boxes = calloc(len, sizeof(t_box));
|
||||||
|
if(!boxes)
|
||||||
|
return(0);
|
||||||
|
parse_boxes(boxes, split);
|
||||||
|
i = 0;
|
||||||
|
while(i < 1000)
|
||||||
|
{
|
||||||
|
printf("\r%ld/%d ", i + 1, 1000);
|
||||||
|
fflush(stdout);
|
||||||
|
connect_closest(boxes, len);
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
i = -1;
|
||||||
|
ft_bzero(largest, 3 * sizeof(long int));
|
||||||
|
while(++i < len)
|
||||||
|
{
|
||||||
|
if(boxes[i].exp)
|
||||||
|
continue;
|
||||||
|
tmp = get_net_size(boxes + i);
|
||||||
|
add_to_top(tmp, largest);
|
||||||
|
}
|
||||||
|
printf("\n");
|
||||||
|
return(largest[0] * largest[1] * largest[2]);
|
||||||
|
}
|
||||||
215
2025/8/part2.c
Normal file
215
2025/8/part2.c
Normal file
@ -0,0 +1,215 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* part2.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: tomoron <tomoron@student.42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/07/17 23:03:36 by tomoron #+# #+# */
|
||||||
|
/* Updated: 2025/12/09 19:47:43 by tomoron ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <math.h>
|
||||||
|
#include "libft/libft.h"
|
||||||
|
|
||||||
|
|
||||||
|
typedef struct s_conn
|
||||||
|
{
|
||||||
|
struct s_box *box;
|
||||||
|
struct s_conn *next;
|
||||||
|
} t_conn;
|
||||||
|
|
||||||
|
typedef struct s_box
|
||||||
|
{
|
||||||
|
long int x;
|
||||||
|
long int y;
|
||||||
|
long int z;
|
||||||
|
t_conn *conn;
|
||||||
|
int exp;
|
||||||
|
} t_box;
|
||||||
|
|
||||||
|
long int get_net_size(t_box *box)
|
||||||
|
{
|
||||||
|
long int res;
|
||||||
|
t_conn *tmp;
|
||||||
|
|
||||||
|
if(box->exp)
|
||||||
|
return(0);
|
||||||
|
|
||||||
|
box->exp = 1;
|
||||||
|
res = 1;
|
||||||
|
tmp = box->conn;
|
||||||
|
while(tmp)
|
||||||
|
{
|
||||||
|
res += get_net_size(tmp->box);
|
||||||
|
tmp = tmp->next;
|
||||||
|
}
|
||||||
|
return(res);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static void parse_boxes(t_box *boxes, char **split)
|
||||||
|
{
|
||||||
|
long int i;
|
||||||
|
char *line;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
while(split[i])
|
||||||
|
{
|
||||||
|
line = split[i];
|
||||||
|
boxes[i].x = ft_atoi(line);
|
||||||
|
while(ft_isdigit(*line))
|
||||||
|
line++;
|
||||||
|
line++;
|
||||||
|
boxes[i].y = ft_atoi(line);
|
||||||
|
while(ft_isdigit(*line))
|
||||||
|
line++;
|
||||||
|
line++;
|
||||||
|
boxes[i].z = ft_atoi(line);
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static double box_dist(t_box *a, t_box *b)
|
||||||
|
{
|
||||||
|
double r;
|
||||||
|
|
||||||
|
r = pow(b->x - a->x , 2) + pow(b->y - a->y, 2) + pow(b->z - a->z, 2);
|
||||||
|
return(sqrt(r));
|
||||||
|
}
|
||||||
|
|
||||||
|
static double is_connected(t_box *a, t_box *b)
|
||||||
|
{
|
||||||
|
t_conn *tmp;
|
||||||
|
|
||||||
|
tmp = a->conn;
|
||||||
|
while(tmp)
|
||||||
|
{
|
||||||
|
if(tmp->box == b)
|
||||||
|
return(1);
|
||||||
|
tmp = tmp->next;
|
||||||
|
}
|
||||||
|
return(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void connect_boxes(t_box *a, t_box *b)
|
||||||
|
{
|
||||||
|
t_conn *conn;
|
||||||
|
|
||||||
|
conn = malloc(sizeof(t_conn) * 2);
|
||||||
|
if(!conn)
|
||||||
|
return;
|
||||||
|
conn->box = b;
|
||||||
|
conn->next = a->conn;
|
||||||
|
a->conn = conn;
|
||||||
|
|
||||||
|
(conn + 1)->box = a;
|
||||||
|
(conn + 1)->next = b->conn;
|
||||||
|
b->conn = (conn + 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
static long int one_network(t_box *boxes, int len)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
int found;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
found = 0;
|
||||||
|
while(i < len)
|
||||||
|
{
|
||||||
|
boxes[i].exp = 0;
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
i = 0;
|
||||||
|
while(i < len)
|
||||||
|
{
|
||||||
|
if(boxes[i].exp)
|
||||||
|
{
|
||||||
|
i++;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if(found)
|
||||||
|
return(0);
|
||||||
|
get_net_size(boxes + i);
|
||||||
|
found = 1;
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
return(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
static long int connect_closest(t_box *boxes, int len)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
int j;
|
||||||
|
t_box *closest;
|
||||||
|
t_box *closest2;
|
||||||
|
double closest_dist;
|
||||||
|
|
||||||
|
|
||||||
|
while(!one_network(boxes, len))
|
||||||
|
{
|
||||||
|
i = 0;
|
||||||
|
closest_dist = 99999999999999;
|
||||||
|
while(i < len)
|
||||||
|
{
|
||||||
|
j = -1;
|
||||||
|
while(++j < len)
|
||||||
|
{
|
||||||
|
if(i == j)
|
||||||
|
continue ;
|
||||||
|
if(is_connected(boxes + i, boxes + j))
|
||||||
|
continue ;
|
||||||
|
if(box_dist(boxes + i, boxes + j) < closest_dist)
|
||||||
|
{
|
||||||
|
closest = boxes + i;
|
||||||
|
closest2 = boxes + j;
|
||||||
|
closest_dist = box_dist(boxes + i, boxes + j);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
connect_boxes(closest, closest2);
|
||||||
|
}
|
||||||
|
return(closest->x * closest2->x);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//static void add_to_top(long int nb, long int *top)
|
||||||
|
//{
|
||||||
|
// if(nb > top[0])
|
||||||
|
// {
|
||||||
|
// top[2] = top[1];
|
||||||
|
// top[1] = top[0];
|
||||||
|
// top[0] = nb;
|
||||||
|
// }
|
||||||
|
// else if(nb > top[1])
|
||||||
|
// {
|
||||||
|
// top[1] = top[0];
|
||||||
|
// top[1] = nb;
|
||||||
|
// }
|
||||||
|
// else if(nb > top[2])
|
||||||
|
// top[2] = nb;
|
||||||
|
//
|
||||||
|
//}
|
||||||
|
|
||||||
|
long int resolve_part2(char *input, char **split)
|
||||||
|
{
|
||||||
|
(void)input;
|
||||||
|
long int len;
|
||||||
|
t_box *boxes;
|
||||||
|
long int res;
|
||||||
|
|
||||||
|
len = 0;
|
||||||
|
while(split[len])
|
||||||
|
len++;
|
||||||
|
|
||||||
|
boxes = calloc(len, sizeof(t_box));
|
||||||
|
if(!boxes)
|
||||||
|
return(0);
|
||||||
|
parse_boxes(boxes, split);
|
||||||
|
res = connect_closest(boxes, len);
|
||||||
|
return(res);
|
||||||
|
}
|
||||||
20
2025/8/test
Normal file
20
2025/8/test
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
162,817,812
|
||||||
|
57,618,57
|
||||||
|
906,360,560
|
||||||
|
592,479,940
|
||||||
|
352,342,300
|
||||||
|
466,668,158
|
||||||
|
542,29,236
|
||||||
|
431,825,988
|
||||||
|
739,650,466
|
||||||
|
52,470,668
|
||||||
|
216,146,977
|
||||||
|
819,987,18
|
||||||
|
117,168,530
|
||||||
|
805,96,715
|
||||||
|
346,949,466
|
||||||
|
970,615,88
|
||||||
|
941,993,340
|
||||||
|
862,61,35
|
||||||
|
984,92,344
|
||||||
|
425,690,689
|
||||||
Reference in New Issue
Block a user