add day 21
This commit is contained in:
69
2024/21/Makefile
Normal file
69
2024/21/Makefile
Normal 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/21/main.c
Normal file
67
2024/21/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);
|
||||
}
|
205
2024/21/part1.c
Normal file
205
2024/21/part1.c
Normal file
@ -0,0 +1,205 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* part1.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: tomoron <tomoron@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/07/17 23:03:36 by tomoron #+# #+# */
|
||||
/* Updated: 2024/12/22 20:19:21 by tomoron ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include "libft/libft.h"
|
||||
|
||||
typedef struct s_cache
|
||||
{
|
||||
char *str;
|
||||
int pos[2];
|
||||
int depth;
|
||||
uint64_t res;
|
||||
struct s_cache *next;
|
||||
} t_cache;
|
||||
|
||||
static void get_char_pos(char **map, char c, int *res)
|
||||
{
|
||||
res[0] = 0;
|
||||
while(map[res[0]])
|
||||
{
|
||||
res[1] = 0;
|
||||
while(map[res[0]][res[1]])
|
||||
{
|
||||
if(map[res[0]][res[1]] == c)
|
||||
return ;
|
||||
res[1]++;
|
||||
}
|
||||
res[0]++;
|
||||
}
|
||||
}
|
||||
|
||||
static void add_char(int nb, char c, char *res, int *i)
|
||||
{
|
||||
if(nb < 0)
|
||||
nb *= -1;
|
||||
while(nb > 0)
|
||||
{
|
||||
res[(*i)++] = c;
|
||||
nb--;
|
||||
}
|
||||
res[*i] = 0;
|
||||
}
|
||||
|
||||
int move_y(char **map, int diff[2], int from[2])
|
||||
{
|
||||
int diff_spc[2];
|
||||
int spc[2];
|
||||
|
||||
get_char_pos(map, ' ', spc);
|
||||
diff_spc[0] = spc[0] - from[0];
|
||||
diff_spc[1] = spc[1] - from[1];
|
||||
return((diff[1] > 0 || (diff_spc[1] == diff[1] && diff_spc[0] == 0)) && (diff_spc[0] != diff[0] || diff_spc[1] != 0));
|
||||
}
|
||||
|
||||
char *plan_route(char **map, int from[2], int to[2])
|
||||
{
|
||||
int diff[2];
|
||||
char chrs[2];
|
||||
char *res;
|
||||
int i;
|
||||
|
||||
res = malloc(128);
|
||||
i = 0;
|
||||
chrs[0] = 'v';
|
||||
chrs[1] = '>';
|
||||
diff[0] = to[0] - from[0];
|
||||
diff[1] = to[1] - from[1];
|
||||
if(diff[0] < 0)
|
||||
chrs[0] = '^';
|
||||
if(diff[1] < 0)
|
||||
chrs[1] = '<';
|
||||
if(move_y(map, diff, from))
|
||||
{
|
||||
add_char(diff[0], chrs[0], res, &i);
|
||||
add_char(diff[1], chrs[1], res, &i);
|
||||
}
|
||||
else
|
||||
{
|
||||
add_char(diff[1], chrs[1], res, &i);
|
||||
add_char(diff[0], chrs[0], res, &i);
|
||||
}
|
||||
res[i] = 'A';
|
||||
res[i + 1] = 0;
|
||||
return(res);
|
||||
}
|
||||
|
||||
uint64_t get_cache(t_cache *cache, char *str, int depth, int *pos)
|
||||
{
|
||||
int i;
|
||||
i = 0;
|
||||
while(cache)
|
||||
{
|
||||
if(!ft_strcmp(cache->str, str) && cache->depth == depth && cache->pos[0] == pos[0] && cache->pos[1] == pos[1])
|
||||
{
|
||||
printf("cache used\n");
|
||||
return(cache->res);
|
||||
}
|
||||
cache = cache->next;
|
||||
i++;
|
||||
}
|
||||
printf("cache len : %d\n", i);
|
||||
return(0);
|
||||
}
|
||||
|
||||
void add_cache(t_cache **cache, char *str, int depth, int *pos, uint64_t res)
|
||||
{
|
||||
t_cache *new;
|
||||
|
||||
new = malloc(sizeof(t_cache));
|
||||
new->str = ft_strdup(str);
|
||||
new->depth = depth;
|
||||
new->pos[0] = pos[0];
|
||||
new->pos[1] = pos[1];
|
||||
new->res = res;
|
||||
new->next = *cache;
|
||||
*cache = new;
|
||||
}
|
||||
|
||||
uint64_t get_path_len(char **keypad, char **robot_pad, int **pos, char *objectives, int depth)
|
||||
{
|
||||
static t_cache *cache;
|
||||
int cur_objective[2];
|
||||
char *path;
|
||||
uint64_t res;
|
||||
int i;
|
||||
|
||||
res = get_cache(cache, objectives, depth, pos[depth]);
|
||||
i = 0;
|
||||
if(res)
|
||||
return(res);
|
||||
while(objectives[i])
|
||||
{
|
||||
if(depth != 0)
|
||||
keypad = robot_pad;
|
||||
get_char_pos(keypad, objectives[i], cur_objective);
|
||||
path = plan_route(keypad, pos[depth], cur_objective);
|
||||
if(depth < 25)
|
||||
res += get_path_len(keypad, robot_pad, pos, path, depth + 1);
|
||||
else
|
||||
res += ft_strlen(path);
|
||||
free(path);
|
||||
pos[depth][0] = cur_objective[0];
|
||||
pos[depth][1] = cur_objective[1];
|
||||
i++;
|
||||
}
|
||||
add_cache(&cache, objectives, depth, pos[depth], res);
|
||||
return(res);
|
||||
}
|
||||
|
||||
int **init_positions(int nb)
|
||||
{
|
||||
int **res;
|
||||
int i;
|
||||
|
||||
res = malloc((nb + 1) * sizeof(int *));
|
||||
res[nb] = 0;
|
||||
i = 0;
|
||||
while(i < nb)
|
||||
{
|
||||
res[i] = malloc(2 * sizeof(int));
|
||||
res[i][0] = 0;
|
||||
res[i][1] = 2;
|
||||
if(i == 0)
|
||||
res[i][0] = 3;
|
||||
i++;
|
||||
}
|
||||
return(res);
|
||||
}
|
||||
|
||||
uint64_t resolve_part1(char *input, char **split)
|
||||
{
|
||||
(void)input;
|
||||
(void)split;
|
||||
int **pos;
|
||||
uint64_t res;
|
||||
char **keypad;
|
||||
char **robot_pad;
|
||||
|
||||
keypad = ft_split("789\n456\n123\n 0A", '\n');
|
||||
robot_pad = ft_split(" ^A\n<v>", '\n');
|
||||
pos = init_positions(30);
|
||||
res = 0;
|
||||
while(*split)
|
||||
{
|
||||
printf("%s\n", *split);
|
||||
// printf("%zu\n", get_path_len(keypad, robot_pad, pos, *split, 0));
|
||||
res += get_path_len(keypad, robot_pad, pos, *split, 0) * ft_atoi(*split);
|
||||
split++;
|
||||
}
|
||||
ft_free_str_arr((void *)pos);
|
||||
ft_free_str_arr(keypad);
|
||||
ft_free_str_arr(robot_pad);
|
||||
return(res);
|
||||
}
|
22
2024/21/part2.c
Normal file
22
2024/21/part2.c
Normal 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);
|
||||
}
|
5
2024/21/test
Normal file
5
2024/21/test
Normal file
@ -0,0 +1,5 @@
|
||||
029A
|
||||
980A
|
||||
179A
|
||||
456A
|
||||
379A
|
Reference in New Issue
Block a user