mirror of
https://github.com/DavidGailleton/AdventOfCode-2025.git
synced 2026-01-26 23:11:58 +00:00
05
This commit is contained in:
26
05/aoc05.h
Normal file
26
05/aoc05.h
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* aoc05.h :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2025/12/16 10:28:12 by dgaillet #+# #+# */
|
||||||
|
/* Updated: 2025/12/16 10:55:28 by dgaillet ### ########lyon.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#ifndef AOC05_H
|
||||||
|
# define AOC05_H
|
||||||
|
|
||||||
|
typedef struct s_range
|
||||||
|
{
|
||||||
|
long long start;
|
||||||
|
long long end;
|
||||||
|
void *next;
|
||||||
|
} t_range;
|
||||||
|
|
||||||
|
t_range *parsing(int fd);
|
||||||
|
void lst_clear(t_range *lst);
|
||||||
|
|
||||||
|
#endif
|
||||||
48
05/main.c
Normal file
48
05/main.c
Normal file
@@ -0,0 +1,48 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* main.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2025/12/16 10:44:11 by dgaillet #+# #+# */
|
||||||
|
/* Updated: 2025/12/16 10:55:36 by dgaillet ### ########lyon.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "aoc05.h"
|
||||||
|
#include "get_next_char.h"
|
||||||
|
#include <fcntl.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
static int is_fresh(t_range *lst, long long ingredient)
|
||||||
|
{
|
||||||
|
while (lst)
|
||||||
|
{
|
||||||
|
if (lst->start <= ingredient && lst->end >= ingredient)
|
||||||
|
return (1);
|
||||||
|
lst = lst->next;
|
||||||
|
}
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
int fd = open("input", O_RDONLY);
|
||||||
|
t_range *lst = parsing(fd);
|
||||||
|
int nb_fresh = 0;
|
||||||
|
char *temp;
|
||||||
|
|
||||||
|
temp = get_next_char(fd, '\n');
|
||||||
|
while (temp)
|
||||||
|
{
|
||||||
|
nb_fresh += is_fresh(lst, atoll(temp));
|
||||||
|
free(temp);
|
||||||
|
temp = get_next_char(fd, '\n');
|
||||||
|
}
|
||||||
|
close(fd);
|
||||||
|
lst_clear(lst);
|
||||||
|
printf("%d\n", nb_fresh);
|
||||||
|
}
|
||||||
77
05/parsing.c
Normal file
77
05/parsing.c
Normal file
@@ -0,0 +1,77 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* parsing.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2025/12/16 10:30:27 by dgaillet #+# #+# */
|
||||||
|
/* Updated: 2025/12/16 10:55:01 by dgaillet ### ########lyon.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "aoc05.h"
|
||||||
|
#include "get_next_char.h"
|
||||||
|
#include <string.h>
|
||||||
|
#include <fcntl.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
static t_range *new_range(char *str)
|
||||||
|
{
|
||||||
|
t_range *range;
|
||||||
|
|
||||||
|
range = malloc(sizeof(t_range));
|
||||||
|
if (!range)
|
||||||
|
return (NULL);
|
||||||
|
range->start = atoll(str);
|
||||||
|
range->end = atoll(strchr(str, '-') + 1);
|
||||||
|
range->next = NULL;
|
||||||
|
return (range);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void range_add_back(t_range **lst, t_range *new)
|
||||||
|
{
|
||||||
|
t_range *temp;
|
||||||
|
|
||||||
|
if (!lst)
|
||||||
|
return ;
|
||||||
|
if (!(*lst))
|
||||||
|
*lst = new;
|
||||||
|
else
|
||||||
|
{
|
||||||
|
temp = *lst;
|
||||||
|
while (temp->next)
|
||||||
|
temp = temp->next;
|
||||||
|
temp->next = new;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void lst_clear(t_range *lst)
|
||||||
|
{
|
||||||
|
t_range *temp;
|
||||||
|
|
||||||
|
while (lst)
|
||||||
|
{
|
||||||
|
temp = lst->next;
|
||||||
|
free(lst);
|
||||||
|
lst = temp;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
t_range *parsing(int fd)
|
||||||
|
{
|
||||||
|
t_range *lst = NULL;
|
||||||
|
char *temp;
|
||||||
|
|
||||||
|
temp = get_next_char(fd, '\n');
|
||||||
|
while (temp && strlen(temp) > 1)
|
||||||
|
{
|
||||||
|
range_add_back(&lst, new_range(temp));
|
||||||
|
free(temp);
|
||||||
|
temp = get_next_char(fd, '\n');
|
||||||
|
}
|
||||||
|
if (temp)
|
||||||
|
free(temp);
|
||||||
|
return (lst);
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user