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