add day 8
This commit is contained in:
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);
|
||||
}
|
||||
Reference in New Issue
Block a user