02 bonus WIP

This commit is contained in:
David Gailleton
2025-12-06 20:42:48 +01:00
parent 613e2dd0a2
commit 68f4286af2
4 changed files with 266 additions and 0 deletions

29
02_b/aoc02.h Normal file
View File

@@ -0,0 +1,29 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* aoc02.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/12/05 13:22:54 by dgaillet #+# #+# */
/* Updated: 2025/12/05 16:14:53 by dgaillet ### ########lyon.fr */
/* */
/* ************************************************************************** */
#ifndef AOC02_H
# define AOC02_H
# include <stdlib.h>
typedef struct s_comb
{
long long first;
long long second;
long long *invalids;
int nb_invalid;
struct s_comb *next;
} t_comb;
t_comb *ft_parsing(int fd);
#endif

1
02_b/input Normal file
View File

@@ -0,0 +1 @@
194-253,81430782-81451118,7709443-7841298,28377-38007,6841236050-6841305978,2222204551-2222236166,2623-4197,318169-385942,9827-16119,580816-616131,646982-683917,147-181,90-120,3545483464-3545590623,4304-5747,246071-314284,8484833630-8484865127,743942-795868,42-53,1435-2086,50480-60875,16232012-16441905,94275676-94433683,61509567-61686956,3872051-4002614,6918792899-6918944930,77312-106847,282-387,829099-1016957,288251787-288311732,6271381-6313272,9877430-10095488,59-87,161112-224439,851833788-851871307,6638265-6688423,434-624,1-20,26-40,6700-9791,990-1307,73673424-73819233

151
02_b/main.c Normal file
View File

@@ -0,0 +1,151 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/12/05 12:39:53 by dgaillet #+# #+# */
/* Updated: 2025/12/06 20:42:04 by dgaillet ### ########lyon.fr */
/* */
/* ************************************************************************** */
#include "aoc02.h"
#include "libft.h"
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
#include <stdio.h>
static int check_if_invalid(long long nb)
{
char *str;
char *temp;
size_t len;
size_t i = 1;
size_t j;
str = ft_lltoa(nb);
//printf("%s\n", str);
if (!str)
return (0);
len = strlen(str);
while (i <= (len / 2))
{
if (!(len % i))
{
j = 1;
temp = ft_substr(str, 0, i);
while (len > i * j)
{
if (ft_strncmp(temp, &str[i * j], i))
{
if (i * j >= len)
{
printf("%lld\n", nb);
return (1);
}
else
break ;
}
j++;
}
free(temp);
}
i++;
}
free(str);
return (0);
}
static long long *add_invalid(t_comb *comb, long long invalid)
{
long long *temp;
int i = 0;
temp = malloc(sizeof(long long) * (comb->nb_invalid + 1));
if (!temp)
return (NULL);
while (i < comb->nb_invalid)
{
temp[i] = comb->invalids[i];
i++;
}
temp[i] = invalid;
free(comb->invalids);
comb->nb_invalid++;
return (temp);
}
static void get_invalids(t_comb *comb)
{
long long actual;
actual = comb->first;
while (actual <= comb->second)
{
if (check_if_invalid(actual))
{
comb->invalids = add_invalid(comb, actual);
if (!comb->invalids)
return ;
}
actual++;
}
}
static void clear_lst(t_comb *lst)
{
t_comb *temp;
while (lst)
{
temp = lst->next;
if (lst->invalids)
free(lst->invalids);
free(lst);
lst = temp->next;
}
}
//8175253565 -> too low
//24157613387
int main(void)
{
t_comb *lst;
t_comb *temp;
int fd;
int i;
unsigned long long result = 0;
fd = open("input", O_RDONLY);
lst = ft_parsing(fd);
temp = lst;
while (temp)
{
//printf("%lld - %lld\n", temp->first, temp->second);
get_invalids(temp);
temp = temp->next;
}
temp = lst;
while (temp)
{
i = 0;
//printf("result = %lld", result);
while (i < temp->nb_invalid)
{
result += temp->invalids[i];
i++;
}
temp = temp->next;
}
printf("%llu", result);
clear_lst(lst);
close(fd);
}
//iterer sur chaque node
//applique une fonction
//incrmeenter le retour de fonction dans res
//
//return res

85
02_b/parsing.c Normal file
View File

@@ -0,0 +1,85 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* parsing.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/12/05 13:52:40 by dgaillet #+# #+# */
/* Updated: 2025/12/05 16:42:18 by dgaillet ### ########lyon.fr */
/* */
/* ************************************************************************** */
#include "aoc02.h"
#include "get_next_char.h"
static t_comb *create_comb(void)
{
t_comb *comb;
comb = malloc(sizeof(t_comb));
if (!comb)
return (NULL);
comb->first = 0;
comb->second = 0;
comb->invalids = NULL;
comb->nb_invalid = 0;
comb->next = NULL;
return (comb);
}
static t_comb *comb_add_back(t_comb **lst, t_comb *comb)
{
t_comb *temp;
if (!lst)
return (NULL);
temp = *lst;
if (!*lst)
*lst = comb;
else
{
while (temp->next)
temp = temp->next;
temp->next = comb;
}
return (*lst);
}
static t_comb *get_next_comb(int fd)
{
char *str;
t_comb *comb;
int i = 0;
str = get_next_char(fd, ',');
if (!str)
return (NULL);
comb = create_comb();
if (!comb)
return (NULL);
comb->first = atoll(str);
while (str[i] && str[i] != '-')
i++;
if (str[i])
comb->second = atoll(&str[i + 1]);
else
return (free(comb), NULL);
return (comb);
}
t_comb *ft_parsing(int fd)
{
t_comb *lst = NULL;
t_comb *temp;
temp = get_next_comb(fd);
while (temp)
{
temp = comb_add_back(&lst, temp);
if (!temp)
return (NULL);
temp = get_next_comb(fd);
}
return (lst);
}