add day 6

This commit is contained in:
2024-12-06 13:06:14 +01:00
parent e3ece8ca06
commit 2261742442
5 changed files with 438 additions and 0 deletions

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

129
2024/6/part1.c Normal file
View File

@ -0,0 +1,129 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* part1.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tomoron <tomoron@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/17 23:03:36 by tomoron #+# #+# */
/* Updated: 2024/12/06 12:39:11 by tomoron ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdio.h>
#include <stdlib.h>
#include "libft/libft.h"
typedef struct s_guard
{
int pos[2];
int dir[2];
} t_guard;
static void find_guard(char **split, int *x, int *y)
{
char c;
*y = 0;
while(split[*y])
{
*x = 0;
while(split[*y][*x])
{
c = split[*y][*x];
if(c == '^' || c == '>' || c == 'v' || c == '<')
return ;
(*x)++;
}
(*y)++;
}
}
static int is_on_map(char **map, int x, int y)
{
return(y >= 0 && x >= 0 && map[y] && map[y][x]);
}
static void next_dir(int *dir)
{
if(dir[0] == -1 && dir[1] == 0)
{
dir[0] = 0;
dir[1] = 1;
}
else if(dir[0] == 0 && dir[1] == 1)
{
dir[0] = 1;
dir[1] = 0;
}
else if(dir[0] == 1 && dir[1] == 0)
{
dir[0] = 0;
dir[1] = -1;
}
else if(dir[0] == 0 && dir[1] == -1)
{
dir[0] = -1;
dir[1] = 0;
}
}
static void move(char **map, t_guard *guard)
{
int nx;
int ny;
ny = guard->pos[0] + guard->dir[0];
nx = guard->pos[1] + guard->dir[1];
if(is_on_map(map, nx, ny) && map[ny][nx] == '#')
{
next_dir(guard->dir);
move(map, guard);
return ;
}
map[guard->pos[0]][guard->pos[1]] = 'X';
guard->pos[0] = ny;
guard->pos[1] = nx;
}
static long int count_char(char **map, char c)
{
int x;
int y;
long int res;
res = 0;
y = 0;
while(map[y])
{
x = 0;
while(map[y][x])
{
if(map[y][x] == c)
res++;
x++;
}
y++;
}
return(res);
}
long int resolve_part1(char *input, char **split)
{
(void)input;
int x;
int y;
t_guard guard;
long int res;
find_guard(split, &x, &y);
guard.pos[0] = y;
guard.pos[1] = x;
guard.dir[0] = (1 * (split[y][x] == 'v')) + (-1 * (split[y][x] == '^'));
guard.dir[1] = (1 * (split[y][x] == '>')) + (-1 * (split[y][x] == '<'));
while(is_on_map(split, guard.pos[1], guard.pos[0]))
move(split, &guard);
res = count_char(split, 'X');
return(res);
}

163
2024/6/part2.c Normal file
View File

@ -0,0 +1,163 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* part2.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tomoron <tomoron@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/17 23:03:36 by tomoron #+# #+# */
/* Updated: 2024/12/06 13:04:52 by tomoron ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "libft/libft.h"
typedef struct s_guard
{
int pos[2];
int dir[2];
} t_guard;
static void find_guard(char **split, t_guard *guard)
{
int x;
int y;
char c;
y = 0;
while(split[y])
{
x = 0;
while(split[y][x])
{
c = split[y][x];
if(c == '^' || c == '>' || c == 'v' || c == '<')
{
guard->pos[0] = y;
guard->pos[1] = x;
guard->dir[0] = (1 * (split[y][x] == 'v')) + (-1 * (split[y][x] == '^'));
guard->dir[1] = (1 * (split[y][x] == '>')) + (-1 * (split[y][x] == '<'));
return ;
}
x++;
}
y++;
}
}
static int is_on_map(char **map, int x, int y)
{
return(y >= 0 && x >= 0 && map[y] && map[y][x]);
}
static void next_dir(int *dir)
{
if(dir[0] == -1 && dir[1] == 0)
{
dir[0] = 0;
dir[1] = 1;
}
else if(dir[0] == 0 && dir[1] == 1)
{
dir[0] = 1;
dir[1] = 0;
}
else if(dir[0] == 1 && dir[1] == 0)
{
dir[0] = 0;
dir[1] = -1;
}
else if(dir[0] == 0 && dir[1] == -1)
{
dir[0] = -1;
dir[1] = 0;
}
}
static void move(char **map, t_guard *guard)
{
int nx;
int ny;
ny = guard->pos[0] + guard->dir[0];
nx = guard->pos[1] + guard->dir[1];
if(is_on_map(map, nx, ny) && map[ny][nx] == '#')
{
next_dir(guard->dir);
move(map, guard);
return ;
}
guard->pos[0] = ny;
guard->pos[1] = nx;
}
/*static long int count_char(char **map, char c)
{
int x;
int y;
long int res;
res = 0;
y = 0;
while(map[y])
{
x = 0;
while(map[y][x])
{
if(map[y][x] == c)
res++;
x++;
}
y++;
}
return(res);
}*/
int is_loop(char **map, t_guard start, int x, int y)
{
t_guard guard;
int i;
guard = start;
i = 0;
map[x][y] = '#';
while(is_on_map(map, guard.pos[1], guard.pos[0]))
{
move(map, &guard);
if(!memcmp(&guard, &start, sizeof(t_guard)) || i++ > 50000)
{
map[x][y] = '.';
return(1);
}
}
map[x][y] = '.';
return(0);
}
long int resolve_part2(char *input, char **split)
{
(void)input;
int x;
int y;
t_guard guard;
long int res;
find_guard(split, &guard);
y = 0;
res = 0;
while(split[y])
{
x = 0;
while(split[y][x])
{
if(split[x][y] == '.' && is_loop(split, guard, x, y))
res++;
x++;
}
y++;
}
return(res);
}

10
2024/6/test Normal file
View File

@ -0,0 +1,10 @@
....#.....
.........#
..........
..#.......
.......#..
..........
.#..^.....
........#.
#.........
......#...