add day 25

This commit is contained in:
2024-12-25 12:10:02 +01:00
parent 7fd80bb5d4
commit 27a62a50ad
5 changed files with 329 additions and 0 deletions

69
2024/25/Makefile Normal file
View File

@ -0,0 +1,69 @@
# **************************************************************************** #
# #
# ::: :::::::: #
# Makefile :+: :+: :+: #
# +:+ +:+ +:+ #
# By: tomoron <tomoron@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2024/07/17 15:48:36 by tomoron #+# #+# #
# Updated: 2024/10/29 20:43:12 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 ~/Desktop/aoc/libft .
main.c:
cp ~/Desktop/aoc/main.c .
part1.c:
cp ~/Desktop/aoc/part1.c .
part2.c:
cp ~/Desktop/aoc/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
2024/25/main.c Normal file
View 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);
}

132
2024/25/part1.c Normal file
View File

@ -0,0 +1,132 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* part1.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tomoron <tomoron@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/17 23:03:36 by tomoron #+# #+# */
/* Updated: 2024/12/25 12:08:33 by tomoron ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdio.h>
#include <stdlib.h>
#include "libft/libft.h"
typedef struct s_elem
{
int nbrs[5];
int lock;
struct s_elem *next;
} t_elem;
void set_nbrs(t_elem *res, char **split)
{
int y;
int x;
if(**split == '#')
res->lock = 1;
x = 0;
y = 0;
while(split[0][x])
{
y = 0;
while((res->lock && split[y][x] == '#') || (!res->lock && split[y][x] == '.'))
y++;
res->nbrs[x] = y;
x++;
}
}
void add_elem(t_elem **res, char **split)
{
t_elem *new;
new = malloc(sizeof(t_elem));
new->lock = 0;
new->next = *res;
set_nbrs(new, split);
*res = new;
}
t_elem *get_all(char **all)
{
char **split;
t_elem *res;
res = 0;
while(*all)
{
split = ft_split(*all, '\n');
add_elem(&res, split);
ft_free_str_arr(split);
all++;
}
return(res);
}
int try_keys(t_elem *all, t_elem *lock)
{
int i;
int res;
res = 0;
while(all)
{
if(all->lock == 0)
{
i = 0;
while(i < 5)
{
if(lock->nbrs[i] > all->nbrs[i])
break;
i++;
}
if(i == 5)
res++;
}
all = all->next;
}
return(res);
}
int get_res(t_elem *lst)
{
t_elem *start;
int res;
res = 0;
start = lst;
while(lst)
{
if(lst->lock == 1)
res += try_keys(start, lst);
lst = lst->next;
}
return(res);
}
long int resolve_part1(char *input, char **split)
{
int i;
t_elem *lst;
int res;
i = 1;
while(input[i])
{
if(input[i - 1] == '\n' && input[i] == '\n')
{
input[i - 1] = 'x';
input[i] = 'x';
}
i++;
}
split = ft_split(input, 'x');
lst = get_all(split);
ft_free_str_arr(split);
res = get_res(lst);
return(res);
}

22
2024/25/part2.c Normal file
View File

@ -0,0 +1,22 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* part2.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tomoron <tomoron@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/17 23:03:36 by tomoron #+# #+# */
/* Updated: 2024/07/17 23:18:02 by tomoron ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdio.h>
#include <stdlib.h>
#include "libft/libft.h"
long int resolve_part2(char *input, char **split)
{
(void)input;
(void)split;
return(0);
}

39
2024/25/test Normal file
View File

@ -0,0 +1,39 @@
#####
.####
.####
.####
.#.#.
.#...
.....
#####
##.##
.#.##
...##
...#.
...#.
.....
.....
#....
#....
#...#
#.#.#
#.###
#####
.....
.....
#.#..
###..
###.#
###.#
#####
.....
.....
.....
#....
#.#..
#.#.#
#####