add day 22

This commit is contained in:
2024-12-23 11:56:29 +01:00
parent 0599507330
commit 83abe1d14d
5 changed files with 322 additions and 0 deletions

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

44
2024/22/part1.c Normal file
View File

@ -0,0 +1,44 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* part1.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tomoron <tomoron@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/17 23:03:36 by tomoron #+# #+# */
/* Updated: 2024/12/22 23:07:18 by tomoron ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdio.h>
#include <stdlib.h>
#include "libft/libft.h"
long int sim_secret(long int nb)
{
int i;
i = 0;
while(i < 2000)
{
nb = ((nb * 64) ^ nb) % 16777216;
nb = ((nb / 32) ^ nb) % 16777216;
nb = ((nb * 2048) ^ nb) % 16777216;
i++;
}
return(nb);
}
long int resolve_part1(char *input, char **split)
{
(void)input;
long int res;
res = 0;
while(*split)
{
res += sim_secret(atol(*split));
split++;
}
return(res);
}

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

@ -0,0 +1,138 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* part2.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tomoron <tomoron@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/17 23:03:36 by tomoron #+# #+# */
/* Updated: 2024/12/23 00:38:13 by tomoron ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include "libft/libft.h"
typedef struct s_seq
{
int seq[4];
int score;
int checked;
struct s_seq *next;
} t_seq;
static void add_seq(t_seq **list, int *seq, int val, int cur)
{
t_seq *tmp;
t_seq *new;
tmp = *list;
while(tmp)
{
if(!memcmp(tmp->seq, seq, 4 * sizeof(int)))
{
if(tmp->checked < cur)
tmp->score += val;
tmp->checked = cur;
return;
}
tmp = tmp->next;
}
new = malloc(sizeof(t_seq));
new->seq[0] = seq[0];
new->seq[1] = seq[1];
new->seq[2] = seq[2];
new->seq[3] = seq[3];
new->checked = cur;
new->score = val;
new->next = *list;
*list = new;
}
static long int sim_secret(long int nb, t_seq **seq_list, int cur)
{
int i;
int prev;
int seq[4];
i = 0;
while(i < 4)
{
prev = nb % 10;
nb = ((nb * 64) ^ nb) % 16777216;
nb = ((nb / 32) ^ nb) % 16777216;
nb = ((nb * 2048) ^ nb) % 16777216;
seq[i] = (nb % 10) - prev;
i++;
}
add_seq(seq_list, seq, nb % 10, cur);
while(i < 2000)
{
prev = nb % 10;
nb = ((nb * 64) ^ nb) % 16777216;
nb = ((nb / 32) ^ nb) % 16777216;
nb = ((nb * 2048) ^ nb) % 16777216;
seq[0] = seq[1];
seq[1] = seq[2];
seq[2] = seq[3];
seq[3] = (nb % 10) - prev;
add_seq(seq_list, seq, nb % 10, cur);
prev = nb % 10;
i++;
}
return(nb);
}
int get_eta(struct timeval start, int cur, int len)
{
struct timeval end;
double sec_time;
double secpit;
gettimeofday(&end, 0);
sec_time = ((double)(end.tv_usec - start.tv_usec) / 1000000) + (end.tv_sec - start.tv_sec);
secpit = sec_time / cur;
return((int)((len - cur) * secpit));
}
long int resolve_part2(char *input, char **split)
{
(void)input;
t_seq *sequences;
t_seq *tmp;
int best;
int i;
int len;
struct timeval start;
sequences = 0;
best = 0;
i = 0;
len = 0;
gettimeofday(&start, 0);
while(split[len])
len++;
while(split[i])
{
if(i < 300)
printf("%d/%d \r", i + 1, len);
else
printf("%d/%d, ETA : %ds \r", i + 1, len, get_eta(start, i + 1, len));
fflush(stdout);
sim_secret(atol(split[i]), &sequences, i);
i++;
}
printf("\n");
while(sequences)
{
if(sequences->score > best)
best = sequences->score;
tmp = sequences->next;
free(sequences);
sequences = tmp;
}
return(best);
}

4
2024/22/test Normal file
View File

@ -0,0 +1,4 @@
1
2
3
2024