add day 03
This commit is contained in:
69
2025/3/Makefile
Normal file
69
2025/3/Makefile
Normal file
@ -0,0 +1,69 @@
|
|||||||
|
# **************************************************************************** #
|
||||||
|
# #
|
||||||
|
# ::: :::::::: #
|
||||||
|
# Makefile :+: :+: :+: #
|
||||||
|
# +:+ +:+ +:+ #
|
||||||
|
# By: tomoron <tomoron@student.42.fr> +#+ +:+ +#+ #
|
||||||
|
# +#+#+#+#+#+ +#+ #
|
||||||
|
# Created: 2024/07/17 15:48:36 by tomoron #+# #+# #
|
||||||
|
# Updated: 2025/12/02 14:14:04 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 ../../libft .
|
||||||
|
|
||||||
|
main.c:
|
||||||
|
cp ../../main.c .
|
||||||
|
|
||||||
|
part1.c:
|
||||||
|
cp ../../part1.c .
|
||||||
|
|
||||||
|
part2.c:
|
||||||
|
cp ../../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
2025/3/main.c
Normal file
67
2025/3/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);
|
||||||
|
}
|
||||||
53
2025/3/part1.c
Normal file
53
2025/3/part1.c
Normal file
@ -0,0 +1,53 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* part1.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: tomoron <tomoron@student.42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/07/17 23:03:36 by tomoron #+# #+# */
|
||||||
|
/* Updated: 2025/12/03 16:40:31 by tomoron ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include "libft/libft.h"
|
||||||
|
|
||||||
|
static long int get_max_jolt(char *line)
|
||||||
|
{
|
||||||
|
char *first;
|
||||||
|
char *second;
|
||||||
|
long int max;
|
||||||
|
long int tmp;
|
||||||
|
|
||||||
|
first = line;
|
||||||
|
max = 0;
|
||||||
|
while(*first)
|
||||||
|
{
|
||||||
|
second = first + 1;
|
||||||
|
while(*second)
|
||||||
|
{
|
||||||
|
tmp = ((*first - '0') * 10) + (*second - '0');
|
||||||
|
if(tmp > max)
|
||||||
|
max = tmp;
|
||||||
|
second++;
|
||||||
|
}
|
||||||
|
first++;
|
||||||
|
}
|
||||||
|
return(max);
|
||||||
|
}
|
||||||
|
|
||||||
|
long int resolve_part1(char *input, char **split)
|
||||||
|
{
|
||||||
|
(void)input;
|
||||||
|
long int res;
|
||||||
|
|
||||||
|
res = 0;
|
||||||
|
while(*split)
|
||||||
|
{
|
||||||
|
res += get_max_jolt(*split);
|
||||||
|
split++;
|
||||||
|
}
|
||||||
|
return(res);
|
||||||
|
}
|
||||||
69
2025/3/part2.c
Normal file
69
2025/3/part2.c
Normal file
@ -0,0 +1,69 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* part2.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: tomoron <tomoron@student.42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/07/17 23:03:36 by tomoron #+# #+# */
|
||||||
|
/* Updated: 2025/12/03 20:03:20 by tomoron ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include "libft/libft.h"
|
||||||
|
|
||||||
|
long int get_max_pos(char *line, int n)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
int found;
|
||||||
|
int found_i;
|
||||||
|
|
||||||
|
found = '0';
|
||||||
|
i = 0;
|
||||||
|
found_i = 0;
|
||||||
|
while(line[i] && i < n)
|
||||||
|
{
|
||||||
|
if(line[i] > found)
|
||||||
|
{
|
||||||
|
found = line[i];
|
||||||
|
found_i = i;
|
||||||
|
}
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
return(found_i);
|
||||||
|
}
|
||||||
|
|
||||||
|
static long int get_max_jolt(char *line, long int cur_nb, int rec_i)
|
||||||
|
{
|
||||||
|
int end;
|
||||||
|
int len;
|
||||||
|
int ind;
|
||||||
|
|
||||||
|
if(rec_i == 0)
|
||||||
|
return(cur_nb);
|
||||||
|
|
||||||
|
len = ft_strlen(line);
|
||||||
|
end = len - (rec_i -1);
|
||||||
|
ind = get_max_pos(line, end);
|
||||||
|
|
||||||
|
cur_nb = cur_nb * 10 + (line[ind] - '0');
|
||||||
|
return(get_max_jolt(line + ind + 1, cur_nb, rec_i - 1));
|
||||||
|
}
|
||||||
|
|
||||||
|
long int resolve_part2(char *input, char **split)
|
||||||
|
{
|
||||||
|
(void)input;
|
||||||
|
long int res;
|
||||||
|
long int tmp;
|
||||||
|
|
||||||
|
res = 0;
|
||||||
|
while(*split)
|
||||||
|
{
|
||||||
|
tmp = get_max_jolt(*split, 0, 12);
|
||||||
|
res+=tmp;
|
||||||
|
split++;
|
||||||
|
}
|
||||||
|
return(res);
|
||||||
|
}
|
||||||
4
2025/3/test
Normal file
4
2025/3/test
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
987654321111111
|
||||||
|
811111111111119
|
||||||
|
234234234234278
|
||||||
|
818181911112111
|
||||||
Reference in New Issue
Block a user