mirror of
https://github.com/DavidGailleton/AdventOfCode-2025.git
synced 2026-01-27 07:21:59 +00:00
05 bonus
This commit is contained in:
27
05_b/aoc05.h
Normal file
27
05_b/aoc05.h
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* aoc05.h :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2025/12/16 10:28:12 by dgaillet #+# #+# */
|
||||||
|
/* Updated: 2025/12/16 11:39:24 by dgaillet ### ########lyon.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#ifndef AOC05_H
|
||||||
|
# define AOC05_H
|
||||||
|
|
||||||
|
typedef struct s_range
|
||||||
|
{
|
||||||
|
long long start;
|
||||||
|
long long end;
|
||||||
|
void *next;
|
||||||
|
void *pre;
|
||||||
|
} t_range;
|
||||||
|
|
||||||
|
t_range *parsing(int fd);
|
||||||
|
void lst_clear(t_range *lst);
|
||||||
|
|
||||||
|
#endif
|
||||||
6
05_b/example
Normal file
6
05_b/example
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
3-5
|
||||||
|
10-14
|
||||||
|
16-20
|
||||||
|
12-18
|
||||||
|
10-20
|
||||||
|
1-1000
|
||||||
1183
05_b/input
Normal file
1183
05_b/input
Normal file
File diff suppressed because it is too large
Load Diff
72
05_b/main.c
Normal file
72
05_b/main.c
Normal file
@@ -0,0 +1,72 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* main.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2025/12/16 10:44:11 by dgaillet #+# #+# */
|
||||||
|
/* Updated: 2025/12/16 15:25:14 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 void update_range(t_range *lst)
|
||||||
|
{
|
||||||
|
t_range *node = lst;
|
||||||
|
|
||||||
|
while (lst->pre)
|
||||||
|
lst = lst->pre;
|
||||||
|
while (lst != node)
|
||||||
|
{
|
||||||
|
if (node->start >= lst->start && node->start <= lst->end)
|
||||||
|
node->start = lst->end + 1;
|
||||||
|
if (node->end >= lst->start && node->end <= lst->end)
|
||||||
|
node->end = lst->start - 1;
|
||||||
|
if ((lst->start >= node->start && lst->start <= node->end)
|
||||||
|
&& (lst->end >= node->start && lst->end <= node->end))
|
||||||
|
{
|
||||||
|
lst->start = 0;
|
||||||
|
lst->end = -1;
|
||||||
|
}
|
||||||
|
lst = lst->next;
|
||||||
|
}
|
||||||
|
if (node->start > node->end)
|
||||||
|
{
|
||||||
|
node->start = 0;
|
||||||
|
node->end = -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
int fd = open("input", O_RDONLY);
|
||||||
|
t_range *lst = parsing(fd);
|
||||||
|
unsigned long long nb_fresh = 0;
|
||||||
|
|
||||||
|
while (lst->next)
|
||||||
|
{
|
||||||
|
update_range(lst);
|
||||||
|
lst = lst->next;
|
||||||
|
}
|
||||||
|
update_range(lst);
|
||||||
|
while (lst->pre)
|
||||||
|
lst = lst->pre;
|
||||||
|
while (lst->next)
|
||||||
|
{
|
||||||
|
nb_fresh += 1 + lst->end - lst->start;
|
||||||
|
lst = lst->next;
|
||||||
|
}
|
||||||
|
nb_fresh += 1 + lst->end - lst->start;
|
||||||
|
while (lst->pre)
|
||||||
|
lst = lst->pre;
|
||||||
|
close(fd);
|
||||||
|
lst_clear(lst);
|
||||||
|
printf("\n%lld\n", nb_fresh);
|
||||||
|
}
|
||||||
79
05_b/parsing.c
Normal file
79
05_b/parsing.c
Normal file
@@ -0,0 +1,79 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* parsing.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2025/12/16 10:30:27 by dgaillet #+# #+# */
|
||||||
|
/* Updated: 2025/12/16 11:51:22 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;
|
||||||
|
range->pre = 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;
|
||||||
|
new->pre = temp;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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