FIRST COMMIT

This commit is contained in:
David Gailleton
2025-12-05 17:37:02 +01:00
commit 86c2e68987
16 changed files with 9580 additions and 0 deletions

55
.gitignore vendored Normal file
View File

@@ -0,0 +1,55 @@
# Prerequisites
*.d
# Object files
*.o
*.ko
*.obj
*.elf
# Linker output
*.ilk
*.map
*.exp
# Precompiled Headers
*.gch
*.pch
# Libraries
*.lib
*.a
*.la
*.lo
# Shared objects (inc. Windows DLLs)
*.dll
*.so
*.so.*
*.dylib
# Executables
*.exe
*.out
*.app
*.i*86
*.x86_64
*.hex
# Debug files
*.dSYM/
*.su
*.idb
*.pdb
# Kernel Module Compile Results
*.mod*
*.cmd
.tmp_versions/
modules.order
Module.symvers
Mkfile.old
dkms.conf
# debug information files
*.dwo

4352
01/input Normal file

File diff suppressed because it is too large Load Diff

69
01/main.c Normal file
View File

@@ -0,0 +1,69 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/12/05 12:00:08 by dgaillet #+# #+# */
/* Updated: 2025/12/05 12:13:27 by dgaillet ### ########lyon.fr */
/* */
/* ************************************************************************** */
#include "get_next_line.h"
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
int turning_left(int nb, int temp)
{
while (temp > 0)
{
if (nb == 0)
nb = 99;
else
nb--;
temp--;
}
return (nb);
}
int turning_right(int nb, int temp)
{
while (temp > 0)
{
if (nb == 99)
nb = 0;
else
nb++;
temp--;
}
return (nb);
}
int main(void)
{
char *str;
int fd;
int nb = 50;
int temp;
int nb_zero = 0;
fd = open("input", O_RDONLY);
str = get_next_line(fd);
while (str)
{
temp = atoi(&str[1]);
if (str[0] == 'L')
nb = turning_left(nb, temp);
if (str[0] == 'R')
nb = turning_right(nb, temp);
if (nb == 0)
nb_zero++;
free(str);
str = get_next_line(fd);
}
close(fd);
printf("%d", nb_zero);
}

111
01_b/get_next_line.c Normal file
View File

@@ -0,0 +1,111 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/11/21 17:20:41 by dgaillet #+# #+# */
/* Updated: 2025/11/28 13:47:15 by dgaillet ### ########lyon.fr */
/* */
/* ************************************************************************** */
#include "get_next_line.h"
#include <unistd.h>
static int del_before_nl(char buf[BUFFER_SIZE])
{
int nl_i;
int i;
char *temp;
nl_i = index_of_nl(buf, BUFFER_SIZE);
if (nl_i >= 0)
{
temp = ft_substr(buf, nl_i + 1, BUFFER_SIZE - nl_i);
if (!temp)
return (-1);
ft_bzero(buf, BUFFER_SIZE);
i = 0;
while (temp[i])
{
buf[i] = temp[i];
i++;
}
free(temp);
}
return (1);
}
static char *ft_read_one(char buf[BUFFER_SIZE], int fd, char *str)
{
int temp;
str = ft_strjoin_new(str, buf, BUFFER_SIZE);
if (!str)
return (NULL);
ft_bzero(buf, BUFFER_SIZE);
temp = read(fd, buf, BUFFER_SIZE);
if (temp < 0)
{
free(str);
return (NULL);
}
if ((!str || ft_strlen(str) == 0) && temp == 0)
{
free(str);
return (NULL);
}
if (temp == 0)
return (str);
return (str);
}
static char *extract_all_nl(char buf[BUFFER_SIZE], int fd, char *str, int nl_i)
{
int temp;
if (!str)
return (NULL);
while (nl_i < 0)
{
str = ft_read_one(buf, fd, str);
if (!str)
return (NULL);
nl_i = index_of_nl(buf, BUFFER_SIZE);
if (nl_i < 0 && !ft_strlen(buf))
return (str);
}
str = ft_strjoin_new(str, buf, nl_i);
if (!str)
return (NULL);
temp = del_before_nl(buf);
if (temp < 0)
{
free(str);
return (NULL);
}
return (str);
}
char *get_next_line(int fd)
{
static char buf[BUFFER_SIZE + 1];
int nl_i;
int temp;
char *str;
if (fd < 0 || BUFFER_SIZE <= 0)
return (NULL);
nl_i = index_of_nl(buf, BUFFER_SIZE);
if (nl_i >= 0)
{
str = ft_substr(buf, 0, nl_i + 1);
temp = del_before_nl(buf);
if (temp < 0)
return (free(str), NULL);
}
else
str = extract_all_nl(buf, fd, ft_substr("", 0, 1), nl_i);
return (str);
}

30
01_b/get_next_line.h Normal file
View File

@@ -0,0 +1,30 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/11/21 17:22:03 by dgaillet #+# #+# */
/* Updated: 2025/11/27 18:05:13 by dgaillet ### ########lyon.fr */
/* */
/* ************************************************************************** */
#ifndef GET_NEXT_LINE_H
# define GET_NEXT_LINE_H
# ifndef BUFFER_SIZE
# define BUFFER_SIZE 42
# endif
# include <stdlib.h>
char *get_next_line(int fd);
size_t ft_strlen(const char *s);
char *ft_strjoin_new(char const *s1, char const *s2, size_t limit);
void ft_bzero(void *s, size_t n);
int index_of_nl(char *str, int limit);
char *ft_substr(char const *s, unsigned int start, size_t len);
#endif

102
01_b/get_next_line_utils.c Normal file
View File

@@ -0,0 +1,102 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/11/21 17:21:38 by dgaillet #+# #+# */
/* Updated: 2025/11/27 18:06:51 by dgaillet ### ########lyon.fr */
/* */
/* ************************************************************************** */
#include "get_next_line.h"
#include <stdint.h>
int index_of_nl(char *str, int limit)
{
int i;
if (!str)
return (-1);
i = 0;
while (i < limit && str[i])
{
if (str[i] == '\n')
return (i);
i++;
}
return (-1);
}
size_t ft_strlen(const char *s)
{
size_t i;
i = 0;
while (s[i])
i++;
return (i);
}
char *ft_strjoin_new(char const *s1, char const *s2, size_t limit)
{
char *str;
size_t i;
size_t j;
str = malloc(sizeof(char) * (ft_strlen(s1) + ft_strlen(s2) + 1));
if (!str)
return (NULL);
i = 0;
j = 0;
while (s1[i])
{
str[i] = s1[i];
i++;
}
while (j <= limit && s2[j])
{
str[i + j] = s2[j];
j++;
}
str[i + j] = '\0';
free((void *) s1);
return (str);
}
char *ft_substr(char const *s, unsigned int start, size_t len)
{
size_t i;
char *sub_str;
size_t s_len;
if (!s)
return (NULL);
s_len = ft_strlen(s);
if (start > s_len)
len = 0;
else if (s_len < (start + len))
len = s_len - start;
sub_str = malloc(sizeof(char) * (len + 1));
if (!sub_str)
return (NULL);
i = 0;
while (i < len)
{
sub_str[i] = s[start + i];
i++;
}
sub_str[i] = '\0';
return (sub_str);
}
void ft_bzero(void *s, size_t n)
{
while (n > 0)
{
*((unsigned char *) s) = '\0';
s++;
n--;
}
}

4352
01_b/input Normal file

File diff suppressed because it is too large Load Diff

76
01_b/main.c Normal file
View File

@@ -0,0 +1,76 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/12/05 12:00:08 by dgaillet #+# #+# */
/* Updated: 2025/12/05 12:37:42 by dgaillet ### ########lyon.fr */
/* */
/* ************************************************************************** */
#include "get_next_line.h"
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
int turning_left(int nb, int temp, int *nb_zero)
{
int i = 0;
while (temp > 0)
{
if (nb == 0 && temp > 0 && i > 0)
(*nb_zero)++;
if (nb == 0)
nb = 99;
else
nb--;
temp--;
i++;
}
return (nb);
}
int turning_right(int nb, int temp, int *nb_zero)
{
while (temp > 0)
{
if (nb == 99 && temp > 1)
(*nb_zero)++;
if (nb == 99)
nb = 0;
else
nb++;
temp--;
}
return (nb);
}
int main(void)
{
char *str;
int fd;
int nb = 50;
int temp;
int nb_zero = 0;
fd = open("input", O_RDONLY);
str = get_next_line(fd);
while (str)
{
temp = atoi(&str[1]);
if (str[0] == 'L')
nb = turning_left(nb, temp, &nb_zero);
if (str[0] == 'R')
nb = turning_right(nb, temp, &nb_zero);
if (nb == 0)
nb_zero++;
free(str);
str = get_next_line(fd);
}
close(fd);
printf("%d\n", nb_zero);
}

29
02/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/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

122
02/main.c Normal file
View File

@@ -0,0 +1,122 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/12/05 12:39:53 by dgaillet #+# #+# */
/* Updated: 2025/12/05 16:51:41 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;
str = ft_lltoa(nb);
if (!str)
return (0);
len = strlen(str);
if (!(len % 2))
{
temp = ft_substr(str, 0, len / 2);
if (!ft_strncmp(temp, &str[len / 2], len / 2))
{
free(str);
return (free(temp), 1);
}
free(temp);
}
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);
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;
}
}
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)
{
get_invalids(temp);
temp = temp->next;
}
temp = lst;
while (temp)
{
i = 0;
while (i < temp->nb_invalid)
{
result += temp->invalids[i];
i++;
}
temp = temp->next;
}
printf("%llu", result);
clear_lst(lst);
}

85
02/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);
}

View File

@@ -0,0 +1,111 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_char.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/11/21 17:20:41 by dgaillet #+# #+# */
/* Updated: 2025/12/05 14:04:02 by dgaillet ### ########lyon.fr */
/* */
/* ************************************************************************** */
#include "get_next_char.h"
#include <unistd.h>
static int del_before_char(char buf[BUFFER_SIZE], char c)
{
int nl_i;
int i;
char *temp;
nl_i = index_of_char(buf, BUFFER_SIZE, c);
if (nl_i >= 0)
{
temp = ft_substr(buf, nl_i + 1, BUFFER_SIZE - nl_i);
if (!temp)
return (-1);
ft_bzero(buf, BUFFER_SIZE);
i = 0;
while (temp[i])
{
buf[i] = temp[i];
i++;
}
free(temp);
}
return (1);
}
static char *ft_read_one(char buf[BUFFER_SIZE], int fd, char *str)
{
int temp;
str = ft_strjoin_new(str, buf, BUFFER_SIZE);
if (!str)
return (NULL);
ft_bzero(buf, BUFFER_SIZE);
temp = read(fd, buf, BUFFER_SIZE);
if (temp < 0)
{
free(str);
return (NULL);
}
if ((!str || ft_strlen(str) == 0) && temp == 0)
{
free(str);
return (NULL);
}
if (temp == 0)
return (str);
return (str);
}
static char *extract_all_char(char buf[BUFFER_SIZE], int fd, char *str, int nl_i, char c)
{
int temp;
if (!str)
return (NULL);
while (nl_i < 0)
{
str = ft_read_one(buf, fd, str);
if (!str)
return (NULL);
nl_i = index_of_char(buf, BUFFER_SIZE, c);
if (nl_i < 0 && !ft_strlen(buf))
return (str);
}
str = ft_strjoin_new(str, buf, nl_i);
if (!str)
return (NULL);
temp = del_before_char(buf, c);
if (temp < 0)
{
free(str);
return (NULL);
}
return (str);
}
char *get_next_char(int fd, char c)
{
static char buf[BUFFER_SIZE + 1];
int nl_i;
int temp;
char *str;
if (fd < 0 || BUFFER_SIZE <= 0)
return (NULL);
nl_i = index_of_char(buf, BUFFER_SIZE, c);
if (nl_i >= 0)
{
str = ft_substr(buf, 0, nl_i + 1);
temp = del_before_char(buf, c);
if (temp < 0)
return (free(str), NULL);
}
else
str = extract_all_char(buf, fd, ft_substr("", 0, 1), nl_i, c);
return (str);
}

View File

@@ -0,0 +1,28 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_char.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/11/21 17:22:03 by dgaillet #+# #+# */
/* Updated: 2025/12/05 16:18:29 by dgaillet ### ########lyon.fr */
/* */
/* ************************************************************************** */
#ifndef GET_NEXT_CHAR_H
# define GET_NEXT_CHAR_H
# ifndef BUFFER_SIZE
# define BUFFER_SIZE 42
# endif
# include <stdlib.h>
# include "libft.h"
char *get_next_char(int fd, char c);
char *ft_strjoin_new(char const *s1, char const *s2, size_t limit);
int index_of_char(char *str, int limit, char c);
#endif

View File

@@ -0,0 +1,56 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_char_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/11/21 17:21:38 by dgaillet #+# #+# */
/* Updated: 2025/12/05 16:18:14 by dgaillet ### ########lyon.fr */
/* */
/* ************************************************************************** */
#include "get_next_char.h"
#include <stdint.h>
int index_of_char(char *str, int limit, char c)
{
int i;
if (!str)
return (-1);
i = 0;
while (i < limit && str[i])
{
if (str[i] == c)
return (i);
i++;
}
return (-1);
}
char *ft_strjoin_new(char const *s1, char const *s2, size_t limit)
{
char *str;
size_t i;
size_t j;
str = malloc(sizeof(char) * (ft_strlen(s1) + ft_strlen(s2) + 1));
if (!str)
return (NULL);
i = 0;
j = 0;
while (s1[i])
{
str[i] = s1[i];
i++;
}
while (j <= limit && s2[j])
{
str[i + j] = s2[j];
j++;
}
str[i + j] = '\0';
free((void *) s1);
return (str);
}

1
libft Submodule

Submodule libft added at 53b2050d11