add day 7

This commit is contained in:
2024-12-07 13:17:57 +01:00
parent 777f37ca17
commit c2acedfa11
5 changed files with 398 additions and 0 deletions

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

116
2024/7/part1.c Normal file
View File

@ -0,0 +1,116 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* part1.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* 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 */
/* */
/* ************************************************************************** */
#include <stdio.h>
#include <stdlib.h>
#include "libft/libft.h"
static int *get_numbers(char *line, int *len)
{
int i;
int *res;
*len = 0;
i = 0;
while(line[i])
{
if(line[i] == ' ')
(*len)++;
i++;
}
*len += 1;
res = malloc((*len) * sizeof(int));
i = 0;
while(*line)
{
res[i] = ft_atoi(line);
i++;
while(ft_isdigit(*line))
line++;
while(*line && !ft_isdigit(*line))
line++;
}
return(res);
}
static int next_op(char *op, int i)
{
while(i >= 0 && op[i] == '+')
{
op[i] = '*';
i--;
}
if(i < 0)
return(0);
if(i >= 0)
op[i] = '+';
return(1);
}
static long int test_nbrs(long int test_val, int *nbrs, char *op, int len)
{
int i;
long int tmp;
tmp = 0;
while(tmp != test_val)
{
tmp = *nbrs;
i = 1;
while(i < len)
{
if(op[i - 1] == '+')
tmp += nbrs[i];
else if(op[i - 1] == '*')
tmp *= nbrs[i];
if(tmp > test_val)
break;
i++;
}
if(!next_op(op, i - 1) && tmp != test_val)
return(0);
}
return(test_val);
}
static long int check_line(char *line)
{
long int test_val;
int *nbrs;
int len;
char *op;
test_val = atol(line);
while(ft_isdigit(*line))
line++;
while(*line && !ft_isdigit(*line))
line++;
nbrs = get_numbers(line, &len);
op = malloc(len);
ft_memset(op, '*', len);
return(test_nbrs(test_val, nbrs, op, len));
}
long int resolve_part1(char *input, char **split)
{
(void)input;
long int res;
res = 0;
while(*split)
{
res+= check_line(*split);
split++;
}
return(res);
}

137
2024/7/part2.c Normal file
View File

@ -0,0 +1,137 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* part2.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* 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 */
/* */
/* ************************************************************************** */
#include <stdio.h>
#include <stdlib.h>
#include "libft/libft.h"
static int *get_numbers(char *line, int *len)
{
int i;
int *res;
*len = 0;
i = 0;
while(line[i])
{
if(line[i] == ' ')
(*len)++;
i++;
}
*len += 1;
res = malloc((*len) * sizeof(int));
i = 0;
while(*line)
{
res[i] = ft_atoi(line);
i++;
while(ft_isdigit(*line))
line++;
while(*line && !ft_isdigit(*line))
line++;
}
return(res);
}
static int next_op(char *op, int i)
{
while(i >= 0 && op[i] == '|')
{
op[i] = '*';
i--;
}
if(i < 0)
return(0);
if(op[i] == '*')
op[i] = '+';
else if(op[i] == '+')
op[i] = '|';
return(1);
}
static long int mult_nb(long int tmp, int nb)
{
while(nb)
{
nb /= 10;
tmp *= 10;
}
return(tmp);
}
static long int test_nbrs(long int test_val, int *nbrs, char *op, int len)
{
int i;
long int tmp;
tmp = 0;
while(tmp != test_val)
{
tmp = *nbrs;
i = 1;
while(i < len)
{
if(op[i - 1] == '+')
tmp += nbrs[i];
else if(op[i - 1] == '*')
tmp *= nbrs[i];
else if(op[i - 1] == '|')
{
tmp = mult_nb(tmp, nbrs[i]);
tmp += nbrs[i];
}
if(tmp > test_val)
break;
i++;
}
if(i == len)
i--;
if(!next_op(op, i - 1) && tmp != test_val)
return(0);
}
return(test_val);
}
static long int check_line(char *line)
{
long int test_val;
int *nbrs;
int len;
char *op;
test_val = atol(line);
while(ft_isdigit(*line))
line++;
while(*line && !ft_isdigit(*line))
line++;
nbrs = get_numbers(line, &len);
op = malloc(len);
ft_memset(op, '*', len);
if(test_nbrs(test_val, nbrs, op, len))
return(test_val);
return(0);
}
long int resolve_part2(char *input, char **split)
{
(void)input;
long int res;
res = 0;
while(*split)
{
res+= check_line(*split);
split++;
}
return(res);
}

9
2024/7/test Normal file
View File

@ -0,0 +1,9 @@
190: 10 19
3267: 81 40 27
83: 17 5
156: 15 6
7290: 6 8 6 15
161011: 16 10 13
192: 17 8 14
21037: 9 7 18 13
292: 11 6 16 20