add day 11
This commit is contained in:
69
2024/11/Makefile
Normal file
69
2024/11/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/12/11 18:59:26 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/11/main.c
Normal file
67
2024/11/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);
|
||||
}
|
@ -6,7 +6,7 @@
|
||||
/* By: tomoron <tomoron@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/07/17 23:03:36 by tomoron #+# #+# */
|
||||
/* Updated: 2024/12/11 17:17:34 by tomoron ### ########.fr */
|
||||
/* Updated: 2024/12/11 22:19:45 by tomoron ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -30,6 +30,7 @@ static t_pebble *get_input(char *input)
|
||||
while(*input && (ft_isdigit(*input) || *input == ' '))
|
||||
{
|
||||
cur = malloc(sizeof(t_pebble));
|
||||
cur->next = 0;
|
||||
cur->value = atol(input);
|
||||
if(prev)
|
||||
prev->next = cur;
|
||||
@ -44,7 +45,7 @@ static t_pebble *get_input(char *input)
|
||||
return(res);
|
||||
}
|
||||
|
||||
int count_digits(long int nb)
|
||||
static int count_digits(long int nb)
|
||||
{
|
||||
int res;
|
||||
|
||||
@ -57,71 +58,57 @@ int count_digits(long int nb)
|
||||
return(res);
|
||||
}
|
||||
|
||||
void split_pebble(t_pebble *pebble)
|
||||
static void split_pebble(long int *l, long int *r)
|
||||
{
|
||||
t_pebble *new;
|
||||
t_pebble *tmp;
|
||||
int len;
|
||||
long exp;
|
||||
int i;
|
||||
|
||||
new = malloc(sizeof(t_pebble));
|
||||
len = count_digits(pebble->value) / 2;
|
||||
new->value = 0;
|
||||
len = count_digits(*l) / 2;
|
||||
*r = 0;
|
||||
i = 0;
|
||||
exp = 1;
|
||||
while(i < len)
|
||||
{
|
||||
new->value += (pebble->value % 10) * exp;
|
||||
pebble->value /= 10;
|
||||
*r += (*l % 10) * exp;
|
||||
*l /= 10;
|
||||
exp *= 10;
|
||||
i++;
|
||||
}
|
||||
tmp = pebble->next;
|
||||
pebble->next = new;
|
||||
new->next = tmp;
|
||||
}
|
||||
|
||||
long int update(t_pebble *pebble)
|
||||
static long int get_nb(long int nb, int depth)
|
||||
{
|
||||
long int res;
|
||||
long int tmp;
|
||||
|
||||
res = 0;
|
||||
while(pebble)
|
||||
if(depth == 25)
|
||||
return(1);
|
||||
if(nb == 0)
|
||||
return(get_nb(1, depth + 1));
|
||||
else if(count_digits(nb) % 2 == 0)
|
||||
{
|
||||
if(!pebble->value)
|
||||
pebble->value = 1;
|
||||
else if(count_digits(pebble->value) % 2 == 0)
|
||||
{
|
||||
split_pebble(pebble);
|
||||
pebble = pebble->next;
|
||||
}
|
||||
else
|
||||
pebble->value *= 2024;
|
||||
pebble = pebble->next;
|
||||
res++;
|
||||
split_pebble(&nb, &tmp);
|
||||
res = get_nb(nb, depth + 1);
|
||||
return(res + get_nb(tmp, depth + 1));
|
||||
}
|
||||
return(res);
|
||||
else
|
||||
return(get_nb(nb * 2024, depth + 1));
|
||||
}
|
||||
|
||||
long int resolve_part1(char *input, char **split)
|
||||
{
|
||||
(void)split;
|
||||
(void)input;
|
||||
t_pebble *pebble;
|
||||
pebble = get_input(input);
|
||||
int i;
|
||||
long int res;
|
||||
(void)get_input;
|
||||
|
||||
i = 0;
|
||||
res = 0;
|
||||
while(i < 25)
|
||||
{
|
||||
printf("step : %d, len :%ld\n",i, update(pebble));
|
||||
i++;
|
||||
}
|
||||
while(pebble)
|
||||
{
|
||||
res++;
|
||||
res += get_nb(pebble->value, 0);
|
||||
pebble = pebble->next;
|
||||
}
|
||||
return(res);
|
||||
|
132
2024/11/part2.c
Normal file
132
2024/11/part2.c
Normal file
@ -0,0 +1,132 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* part2.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: tomoron <tomoron@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/07/17 23:03:36 by tomoron #+# #+# */
|
||||
/* Updated: 2024/12/11 22:52:44 by tomoron ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "libft/libft.h"
|
||||
|
||||
#define MAX_NB_MAP 1000
|
||||
|
||||
typedef struct s_pebble
|
||||
{
|
||||
long int value;
|
||||
struct s_pebble *next;
|
||||
} t_pebble;
|
||||
|
||||
static t_pebble *get_input(char *input)
|
||||
{
|
||||
t_pebble *res;
|
||||
t_pebble *prev;
|
||||
t_pebble *cur;
|
||||
|
||||
prev = 0;
|
||||
while(*input && (ft_isdigit(*input) || *input == ' '))
|
||||
{
|
||||
cur = malloc(sizeof(t_pebble));
|
||||
cur->next = 0;
|
||||
cur->value = atol(input);
|
||||
if(prev)
|
||||
prev->next = cur;
|
||||
else
|
||||
res = cur;
|
||||
prev = cur;
|
||||
while(ft_isdigit(*input))
|
||||
input++;
|
||||
while(*input == ' ')
|
||||
input++;
|
||||
}
|
||||
return(res);
|
||||
}
|
||||
|
||||
static int count_digits(long int nb)
|
||||
{
|
||||
int res;
|
||||
|
||||
res = 1;
|
||||
while(nb > 9)
|
||||
{
|
||||
nb /= 10;
|
||||
res++;
|
||||
}
|
||||
return(res);
|
||||
}
|
||||
|
||||
static void split_pebble(long int *l, long int *r)
|
||||
{
|
||||
int len;
|
||||
long exp;
|
||||
int i;
|
||||
|
||||
len = count_digits(*l) / 2;
|
||||
*r = 0;
|
||||
i = 0;
|
||||
exp = 1;
|
||||
while(i < len)
|
||||
{
|
||||
*r += (*l % 10) * exp;
|
||||
*l /= 10;
|
||||
exp *= 10;
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
static long int get_nb(long int nb, long int depth, long int *map)
|
||||
{
|
||||
long int res;
|
||||
long int tmp;
|
||||
long int fuck;
|
||||
|
||||
fuck = nb;
|
||||
if(depth == 75)
|
||||
return(1);
|
||||
if(map && nb >= 0 && nb < MAX_NB_MAP && map[(depth * MAX_NB_MAP) + nb])
|
||||
return(map[(depth * MAX_NB_MAP) + nb]);
|
||||
res = 0;
|
||||
if(nb == 0)
|
||||
res = get_nb(1, depth + 1, map);
|
||||
else if(count_digits(nb) % 2 == 0)
|
||||
{
|
||||
split_pebble(&nb, &tmp);
|
||||
res = get_nb(nb, depth + 1, map);
|
||||
res += get_nb(tmp, depth + 1, map);
|
||||
}
|
||||
else
|
||||
res = get_nb(nb * 2024, depth + 1, map);
|
||||
if(map && fuck >= 0 && fuck < MAX_NB_MAP)
|
||||
map[(depth * MAX_NB_MAP) + fuck] = res;
|
||||
return(res);
|
||||
}
|
||||
|
||||
long int resolve_part2(char *input, char **split)
|
||||
{
|
||||
(void)split;
|
||||
(void)input;
|
||||
t_pebble *pebble;
|
||||
long int res;
|
||||
long int *map;
|
||||
size_t map_len;
|
||||
(void)get_input;
|
||||
|
||||
res = 0;
|
||||
pebble = get_input(input);
|
||||
map_len = MAX_NB_MAP * 75 * sizeof(long int);
|
||||
map = malloc(map_len);
|
||||
bzero(map, map_len);
|
||||
while(pebble)
|
||||
{
|
||||
res += get_nb(pebble->value, 0, map);
|
||||
pebble = pebble->next;
|
||||
}
|
||||
free(map);
|
||||
return(res);
|
||||
}
|
1
2024/11/test
Normal file
1
2024/11/test
Normal file
@ -0,0 +1 @@
|
||||
125 17
|
Reference in New Issue
Block a user