add day 5
This commit is contained in:
69
2025/5/Makefile
Normal file
69
2025/5/Makefile
Normal file
@ -0,0 +1,69 @@
|
||||
# **************************************************************************** #
|
||||
# #
|
||||
# ::: :::::::: #
|
||||
# Makefile :+: :+: :+: #
|
||||
# +:+ +:+ +:+ #
|
||||
# By: tomoron <tomoron@student.42.fr> +#+ +:+ +#+ #
|
||||
# +#+#+#+#+#+ +#+ #
|
||||
# Created: 2024/07/17 15:48:36 by tomoron #+# #+# #
|
||||
# Updated: 2025/12/02 14:14:04 by tomoron ### ########.fr #
|
||||
# #
|
||||
# **************************************************************************** #
|
||||
|
||||
SRCS = main.c\
|
||||
part1.c\
|
||||
part2.c
|
||||
|
||||
OBJS = $(SRCS:.c=.o)
|
||||
|
||||
FLAGS = -Wextra -Werror -Wall -g
|
||||
|
||||
LIB = libft/libft.a
|
||||
|
||||
all: $(LIB) a.out test
|
||||
cp test cur_input
|
||||
./a.out
|
||||
|
||||
f: $(LIB) a.out input
|
||||
cp input cur_input
|
||||
./a.out
|
||||
|
||||
test:
|
||||
touch test
|
||||
|
||||
input:
|
||||
touch input
|
||||
|
||||
cp:
|
||||
cp part1.c part2.c
|
||||
sed -i 's/resolve_part1/resolve_part2/g' part2.c
|
||||
|
||||
$(LIB): libft
|
||||
make -C libft
|
||||
|
||||
libft:
|
||||
cp -r ../../libft .
|
||||
|
||||
main.c:
|
||||
cp ../../main.c .
|
||||
|
||||
part1.c:
|
||||
cp ../../part1.c .
|
||||
|
||||
part2.c:
|
||||
cp ../../part2.c .
|
||||
|
||||
a.out: main.c $(OBJS) $(LIB)
|
||||
clang $(FLAGS) $(OBJS) $(LIB)
|
||||
|
||||
clean:
|
||||
rm -rf $(OBJS)
|
||||
rm -rf libft
|
||||
|
||||
fclean: clean
|
||||
rm -rf a.out
|
||||
|
||||
.c.o:
|
||||
clang $(FLAGS) -c $< -o $@
|
||||
|
||||
.PHONY: t all clean fclean cp
|
||||
67
2025/5/main.c
Normal file
67
2025/5/main.c
Normal file
@ -0,0 +1,67 @@
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include "libft/libft.h"
|
||||
|
||||
long int resolve_part1(char *input, char **split);
|
||||
long int resolve_part2(char *input, char **split);
|
||||
|
||||
char *get_input(char *file)
|
||||
{
|
||||
int fd;
|
||||
int rd_len;
|
||||
char buffer[1000];
|
||||
int len;
|
||||
char *res;
|
||||
|
||||
fd = open(file, O_RDONLY);
|
||||
len = 0;
|
||||
if(fd < 0)
|
||||
return(0);
|
||||
rd_len = -1;
|
||||
while(rd_len)
|
||||
{
|
||||
rd_len = read(fd, buffer, 1000);
|
||||
len+= rd_len;
|
||||
}
|
||||
close(fd);
|
||||
res = malloc(len + 1);
|
||||
if(!res)
|
||||
return(0);
|
||||
fd = open(file, O_RDONLY);
|
||||
if(fd < 0)
|
||||
return(0);
|
||||
rd_len = read(fd,res, len);
|
||||
res[len] = 0;
|
||||
close(fd);
|
||||
return(res);
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
char *input;
|
||||
char *input_cpy;
|
||||
char **split;
|
||||
|
||||
input = get_input("cur_input");
|
||||
input_cpy = ft_strdup(input);
|
||||
if(!input || !input_cpy)
|
||||
{
|
||||
fprintf(stderr, "file read error\n");
|
||||
return(1);
|
||||
}
|
||||
split = ft_split(input, '\n');
|
||||
if(!split)
|
||||
return(1);
|
||||
printf("result part 1: %ld\n", resolve_part1(input, split));
|
||||
ft_free_str_arr(split);
|
||||
split = ft_split(input_cpy, '\n');
|
||||
if(!split)
|
||||
return(1);
|
||||
printf("result part 2: %ld\n", resolve_part2(input, split));
|
||||
ft_free_str_arr(split);
|
||||
free(input);
|
||||
free(input_cpy);
|
||||
return(0);
|
||||
}
|
||||
78
2025/5/part1.c
Normal file
78
2025/5/part1.c
Normal file
@ -0,0 +1,78 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* part1.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: tomoron <tomoron@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/07/17 23:03:36 by tomoron #+# #+# */
|
||||
/* Updated: 2025/12/05 11:36:52 by tomoron ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "libft/libft.h"
|
||||
|
||||
typedef struct s_range_list
|
||||
{
|
||||
long int min;
|
||||
long int max;
|
||||
struct s_range_list *next;
|
||||
} t_range_list;
|
||||
|
||||
static t_range_list *parse_ranges(char **input)
|
||||
{
|
||||
t_range_list *res;
|
||||
t_range_list *new;
|
||||
|
||||
res = 0;
|
||||
while(**input != '\n')
|
||||
{
|
||||
new = malloc(sizeof(t_range_list));
|
||||
if(!new)
|
||||
return(0);
|
||||
new->min = atol(*input);
|
||||
while(ft_isdigit(**input))
|
||||
(*input)++;
|
||||
(*input)++;
|
||||
new->max = atol(*input);
|
||||
while(ft_isdigit(**input))
|
||||
(*input)++;
|
||||
(*input)++;
|
||||
new->next = res;
|
||||
res = new;
|
||||
}
|
||||
return(res);
|
||||
}
|
||||
|
||||
static int test_range(long int nb, t_range_list *lst)
|
||||
{
|
||||
while(lst)
|
||||
{
|
||||
if(nb >= lst->min && nb <= lst->max)
|
||||
return(1);
|
||||
lst = lst->next;
|
||||
}
|
||||
return(0);
|
||||
}
|
||||
|
||||
long int resolve_part1(char *input, char **split)
|
||||
{
|
||||
(void)split;
|
||||
t_range_list *lst;
|
||||
long int res;
|
||||
|
||||
res = 0;
|
||||
lst = parse_ranges(&input);
|
||||
input++;
|
||||
while(*input)
|
||||
{
|
||||
if(test_range(atol(input), lst))
|
||||
res++;
|
||||
while(ft_isdigit(*input))
|
||||
input++;
|
||||
input++;
|
||||
}
|
||||
return(res);
|
||||
}
|
||||
142
2025/5/part2.c
Normal file
142
2025/5/part2.c
Normal file
@ -0,0 +1,142 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* part2.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: tomoron <tomoron@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/07/17 23:03:36 by tomoron #+# #+# */
|
||||
/* Updated: 2025/12/05 14:49:11 by tomoron ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "libft/libft.h"
|
||||
|
||||
typedef struct s_range_list
|
||||
{
|
||||
long int min;
|
||||
long int max;
|
||||
struct s_range_list *next;
|
||||
} t_range_list;
|
||||
|
||||
static t_range_list *test_range(long int nb, t_range_list *lst)
|
||||
{
|
||||
while(lst)
|
||||
{
|
||||
if(nb >= lst->min && nb <= lst->max)
|
||||
return(lst);
|
||||
lst = lst->next;
|
||||
}
|
||||
return(0);
|
||||
}
|
||||
|
||||
static int check_dup(t_range_list *node, t_range_list *lst)
|
||||
{
|
||||
t_range_list *found_min;
|
||||
t_range_list *found_max;
|
||||
|
||||
found_min = test_range(node->min, lst);
|
||||
found_max = test_range(node->max, lst);
|
||||
if(!found_min && !found_max)
|
||||
return(0);
|
||||
if(found_min && found_min == found_max)
|
||||
return(1);
|
||||
|
||||
if(found_min)
|
||||
node->min = found_min->max + 1;
|
||||
if(node->min > node->max)
|
||||
return(1);
|
||||
|
||||
if(found_max)
|
||||
node->max = found_max->min - 1;
|
||||
if(node->min > node->max)
|
||||
return(1);
|
||||
|
||||
if(node->min == node->max && test_range(node->min, lst))
|
||||
return(1);
|
||||
|
||||
return(check_dup(node, lst));
|
||||
}
|
||||
|
||||
static int rm_duplicates(t_range_list **orig)
|
||||
{
|
||||
t_range_list *tmp;
|
||||
t_range_list *lst;
|
||||
t_range_list *prev;
|
||||
|
||||
prev = 0;
|
||||
lst = (*orig);
|
||||
while(lst)
|
||||
{
|
||||
tmp = *orig;
|
||||
while(tmp)
|
||||
{
|
||||
if(tmp != lst && lst->min >= tmp->min && lst->max <= tmp->max)
|
||||
{
|
||||
if(prev)
|
||||
prev->next = lst->next;
|
||||
else
|
||||
*orig = lst->next;
|
||||
tmp = lst;
|
||||
lst = lst->next;
|
||||
free(tmp);
|
||||
return(1);
|
||||
}
|
||||
tmp = tmp->next;
|
||||
}
|
||||
prev = lst;
|
||||
lst = lst->next;
|
||||
}
|
||||
return(0);
|
||||
}
|
||||
|
||||
static t_range_list *parse_ranges(char **input)
|
||||
{
|
||||
t_range_list *res;
|
||||
t_range_list *new;
|
||||
|
||||
res = 0;
|
||||
while(**input != '\n')
|
||||
{
|
||||
new = malloc(sizeof(t_range_list));
|
||||
if(!new)
|
||||
return(0);
|
||||
new->min = atol(*input);
|
||||
while(ft_isdigit(**input))
|
||||
(*input)++;
|
||||
(*input)++;
|
||||
new->max = atol(*input);
|
||||
while(ft_isdigit(**input))
|
||||
(*input)++;
|
||||
(*input)++;
|
||||
if(check_dup(new, res))
|
||||
free(new);
|
||||
else
|
||||
{
|
||||
new->next = res;
|
||||
res = new;
|
||||
}
|
||||
}
|
||||
|
||||
while(rm_duplicates(&res))
|
||||
;;
|
||||
return(res);
|
||||
}
|
||||
|
||||
long int resolve_part2(char *input, char **split)
|
||||
{
|
||||
(void)split;
|
||||
t_range_list *lst;
|
||||
long int res;
|
||||
|
||||
res = 0;
|
||||
lst = parse_ranges(&input);
|
||||
while(lst)
|
||||
{
|
||||
res += (lst->max - lst->min) + 1;
|
||||
lst = lst->next;
|
||||
}
|
||||
return(res);
|
||||
}
|
||||
14
2025/5/test
Normal file
14
2025/5/test
Normal file
@ -0,0 +1,14 @@
|
||||
3-5
|
||||
10-14
|
||||
16-20
|
||||
12-18
|
||||
4-17
|
||||
555838442293911-558418634540905
|
||||
554358003043155-560773839216269
|
||||
|
||||
1
|
||||
5
|
||||
8
|
||||
11
|
||||
17
|
||||
32
|
||||
Reference in New Issue
Block a user