add day 8 and add free on day 7

This commit is contained in:
2024-12-08 13:49:16 +01:00
parent c2acedfa11
commit 6d6bad0cbe
7 changed files with 389 additions and 2 deletions

View File

@ -6,7 +6,7 @@
/* By: tomoron <tomoron@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/17 23:03:36 by tomoron #+# #+# */
/* Updated: 2024/12/07 13:14:36 by tomoron ### ########.fr */
/* Updated: 2024/12/08 13:38:45 by tomoron ### ########.fr */
/* */
/* ************************************************************************** */
@ -77,9 +77,15 @@ static long int test_nbrs(long int test_val, int *nbrs, char *op, int len)
i++;
}
if(!next_op(op, i - 1) && tmp != test_val)
{
free(nbrs);
free(op);
return(0);
}
}
free(nbrs);
free(op);
return(test_val);
}

View File

@ -6,7 +6,7 @@
/* By: tomoron <tomoron@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/17 23:03:36 by tomoron #+# #+# */
/* Updated: 2024/12/07 13:17:09 by tomoron ### ########.fr */
/* Updated: 2024/12/08 13:47:46 by tomoron ### ########.fr */
/* */
/* ************************************************************************** */
@ -96,9 +96,15 @@ static long int test_nbrs(long int test_val, int *nbrs, char *op, int len)
if(i == len)
i--;
if(!next_op(op, i - 1) && tmp != test_val)
{
free(op);
free(nbrs);
return(0);
}
}
free(op);
free(nbrs);
return(test_val);
}

69
2024/8/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/8/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);
}

110
2024/8/part1.c Normal file
View File

@ -0,0 +1,110 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* part1.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tomoron <tomoron@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/17 23:03:36 by tomoron #+# #+# */
/* Updated: 2024/12/08 13:37:33 by tomoron ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdio.h>
#include <stdlib.h>
#include "libft/libft.h"
static long int get_nb_antinode_char(char **map, char **locations, int limits[2], int ref_pos[2])
{
char c;
long int res;
int pos[2];
int np[2];
res = 0;
c = map[ref_pos[0]][ref_pos[1]];
pos[0] = 0;
while(map[pos[0]])
{
pos[1] = 0;
while(map[pos[0]][pos[1]])
{
if(ref_pos[0] != pos[0] && ref_pos[1] != pos[1] && map[pos[0]][pos[1]] == c)
{
np[0] = ref_pos[0] - (pos[0] - ref_pos[0]);
np[1] = ref_pos[1] - (pos[1] - ref_pos[1]);
if(np[0] >= 0 && np[0] < limits[0] && np[1] >= 0 && np[1] < limits[1] && locations[np[0]][np[1]] != '#')
{
res++;
locations[np[0]][np[1]] = '#';
}
np[0] = pos[0] + (pos[0] - ref_pos[0]);
np[1] = pos[1] + (pos[1] - ref_pos[1]);
if(np[0] >= 0 && np[0] < limits[0] && np[1] >= 0 && np[1] < limits[1] && locations[np[0]][np[1]] != '#')
{
res++;
locations[np[0]][np[1]] = '#';
}
}
pos[1]++;
}
pos[0]++;
}
map[ref_pos[0]][ref_pos[1]] = '.';
return(res);
}
static long int find_antinode(char **map, int height, int width)
{
int x;
int y;
long int res;
char **locations;
y = 0;
res = 0;
locations = malloc(height * sizeof(char *));
while(y < height)
{
locations[y] = malloc(width);
ft_memset(locations[y], '.', width);
y++;
}
y = 0;
while(map[y])
{
x = 0;
while(map[y][x])
{
if(map[y][x] != '.')
res += get_nb_antinode_char(map, locations, (int [2]){height, width}, (int [2]){y, x});
x++;
}
y++;
}
y = 0;
while(y < height)
{
free(locations[y]);
y++;
}
free(locations);
return(res);
}
long int resolve_part1(char *input, char **split)
{
(void)input;
int height;
int width;
long int res;
height = 0;
while(split[height])
height++;
width = 0;
while(split[0][width])
width++;
res = find_antinode(split, height, width);
return(res);
}

119
2024/8/part2.c Normal file
View File

@ -0,0 +1,119 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* part2.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tomoron <tomoron@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/17 23:03:36 by tomoron #+# #+# */
/* Updated: 2024/12/08 13:37:39 by tomoron ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdio.h>
#include <stdlib.h>
#include "libft/libft.h"
static long int get_nb_antinode_char(char **map, char **locations, int limits[2], int ref_pos[2])
{
char c;
long int res;
int pos[2];
int np[2];
int diff[2];
res = 0;
c = map[ref_pos[0]][ref_pos[1]];
pos[0] = 0;
while(map[pos[0]])
{
pos[1] = 0;
while(map[pos[0]][pos[1]])
{
if(ref_pos[0] != pos[0] && ref_pos[1] != pos[1] && map[pos[0]][pos[1]] == c)
{
diff[0] = pos[0] - ref_pos[0];
diff[1] = pos[1] - ref_pos[1];
np[0] = ref_pos[0];
np[1] = ref_pos[1];
while(np[0] >= 0 && np[0] < limits[0] && np[1] >= 0 && np[1] < limits[1])
{
if(locations[np[0]][np[1]] != '#')
res++;
locations[np[0]][np[1]] = '#';
np[0] -= diff[0];
np[1] -= diff[1];
}
np[0] = pos[0];
np[1] = pos[1];
while(np[0] >= 0 && np[0] < limits[0] && np[1] >= 0 && np[1] < limits[1])
{
if(locations[np[0]][np[1]] != '#')
res++;
locations[np[0]][np[1]] = '#';
np[0] += diff[0];
np[1] += diff[1];
}
}
pos[1]++;
}
pos[0]++;
}
map[ref_pos[0]][ref_pos[1]] = '.';
return(res);
}
static long int find_antinode(char **map, int height, int width)
{
int x;
int y;
long int res;
char **locations;
y = 0;
res = 0;
locations = malloc(height * sizeof(char *));
while(y < height)
{
locations[y] = malloc(width);
ft_memset(locations[y], '.', width);
y++;
}
y = 0;
while(map[y])
{
x = 0;
while(map[y][x])
{
if(map[y][x] != '.')
res += get_nb_antinode_char(map, locations, (int [2]){height, width}, (int [2]){y, x});
x++;
}
y++;
}
y = 0;
while(y < height)
{
free(locations[y]);
y++;
}
free(locations);
return(res);
}
long int resolve_part2(char *input, char **split)
{
(void)input;
int height;
int width;
long int res;
height = 0;
while(split[height])
height++;
width = 0;
while(split[0][width])
width++;
res = find_antinode(split, height, width);
return(res);
}

10
2024/8/test Normal file
View File

@ -0,0 +1,10 @@
T.........
...T......
.T........
..........
..........
..........
..........
..........
..........
..........