add -g to libft makefile; add day 3

This commit is contained in:
2024-12-03 10:45:55 +01:00
parent 60acac0231
commit 0f2cc977b0
6 changed files with 290 additions and 4 deletions

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

72
2024/3/part1.c Normal file
View File

@ -0,0 +1,72 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* part1.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tomoron <tomoron@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/17 23:03:36 by tomoron #+# #+# */
/* Updated: 2024/12/03 10:22:09 by tomoron ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdio.h>
#include <stdlib.h>
#include "libft/libft.h"
static int get_instruction(char **line, int *n1, int *n2)
{
int i;
if(ft_strncmp(*line, "mul(", 4))
return(0);
*line += 4;
i = 0;
*n1 = ft_atoi(*line);
while(ft_isdigit((*line)[i]))
i++;
if(!i)
return(0);
*line += i;
if(**line != ',')
return(0);
(*line)++;
i = 0;
*n2 = ft_atoi(*line);
while(ft_isdigit((*line)[i]))
i++;
if(!i)
return(0);
*line += i;
return(**line == ')');
}
static long int res_line(char *line)
{
int res;
int n1;
int n2;
res = 0;
while(*line)
{
if(get_instruction(&line, &n1, &n2))
res += n1 * n2;
line++;
}
return(res);
}
long int resolve_part1(char *input, char **split)
{
(void)input;
long int res;
res = 0;
while(*split)
{
res += res_line(*split);
split++;
}
return(res);
}

79
2024/3/part2.c Normal file
View File

@ -0,0 +1,79 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* part1.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tomoron <tomoron@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/17 23:03:36 by tomoron #+# #+# */
/* Updated: 2024/12/03 10:22:09 by tomoron ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdio.h>
#include <stdlib.h>
#include "libft/libft.h"
static int get_instruction(char **line, int *n1, int *n2)
{
int i;
if(ft_strncmp(*line, "mul(", 4))
return(0);
*line += 4;
i = 0;
*n1 = ft_atoi(*line);
while(ft_isdigit((*line)[i]))
i++;
if(!i || i > 3)
return(0);
*line += i;
if(**line != ',')
return(0);
(*line)++;
i = 0;
*n2 = ft_atoi(*line);
while(ft_isdigit((*line)[i]))
i++;
if(!i || i > 3)
return(0);
*line += i;
return(**line == ')');
}
static long int res_line(char *line, int *enable)
{
int res;
int n1;
int n2;
res = 0;
while(*line)
{
if(!ft_strncmp(line, "do()", 4))
*enable = 1;
if(!ft_strncmp(line, "don't()", 7))
*enable = 0;
if(*enable && get_instruction(&line, &n1, &n2))
res += n1 * n2;
line++;
}
return(res);
}
long int resolve_part2(char *input, char **split)
{
(void)input;
long int res;
int enable;
res = 0;
enable = 1;
while(*split)
{
res += res_line(*split, &enable);
split++;
}
return(res);
}

1
2024/3/test Normal file
View File

@ -0,0 +1 @@
xmul(2,4)&mul[3,7]!^don't()_mul(5,5)+mul(32,64](mul(11,8)undo()?mul(8,5))

View File

@ -1,7 +1,5 @@
# **************************************************************************** # # **************************************************************************** #
# # # # ::: :::::::: # Makefile :+: :+: :+: #
# ::: :::::::: #
# Makefile :+: :+: :+: #
# +:+ +:+ +:+ # # +:+ +:+ +:+ #
# By: tomoron <marvin@42.fr> +#+ +:+ +#+ # # By: tomoron <marvin@42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ # # +#+#+#+#+#+ +#+ #
@ -65,7 +63,7 @@ SRCS_BONUS = ft_lstnew.c\
OBJS = $(SRCS:.c=.o) OBJS = $(SRCS:.c=.o)
OBJS_BONUS = $(SRCS_BONUS:.c=.o) OBJS_BONUS = $(SRCS_BONUS:.c=.o)
FLAGS = -Wall -Wextra -Werror FLAGS = -Wall -Wextra -Werror -g
all: $(NAME) all: $(NAME)