remove inputs
This commit is contained in:
69
2024/2/Makefile
Normal file
69
2024/2/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/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/2/main.c
Normal file
67
2024/2/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);
|
||||
}
|
65
2024/2/part1.c
Normal file
65
2024/2/part1.c
Normal file
@ -0,0 +1,65 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* part1.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: tomoron <tomoron@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/07/17 23:03:36 by tomoron #+# #+# */
|
||||
/* Updated: 2024/12/02 11:22:15 by tomoron ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "libft/libft.h"
|
||||
|
||||
static void skip_nbr(char **line)
|
||||
{
|
||||
while(ft_isdigit(**line))
|
||||
(*line)++;
|
||||
while(**line == ' ')
|
||||
(*line)++;
|
||||
}
|
||||
|
||||
static long int check_line(char *line)
|
||||
{
|
||||
int left;
|
||||
int right;
|
||||
int asc;
|
||||
|
||||
left = ft_atoi(line);
|
||||
skip_nbr(&line);
|
||||
asc = -1;
|
||||
while(*line)
|
||||
{
|
||||
right = ft_atoi(line);
|
||||
skip_nbr(&line);
|
||||
if(right == left || (left > right && asc == 1) || (left < right && asc == 0))
|
||||
return(0);
|
||||
if(left > right)
|
||||
asc = 0;
|
||||
if(left < right)
|
||||
asc = 1;
|
||||
if(left - right > 3 || left - right < -3)
|
||||
return(0);
|
||||
left = right;
|
||||
}
|
||||
return(1);
|
||||
}
|
||||
|
||||
long int resolve_part1(char *input, char **split)
|
||||
{
|
||||
(void)input;
|
||||
long int res;
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
res = 0;
|
||||
while(split[i])
|
||||
{
|
||||
res += check_line(split[i]);
|
||||
i++;
|
||||
}
|
||||
return(res);
|
||||
}
|
105
2024/2/part2.c
Normal file
105
2024/2/part2.c
Normal file
@ -0,0 +1,105 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* part2.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: tomoron <tomoron@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/07/17 23:03:36 by tomoron #+# #+# */
|
||||
/* Updated: 2024/12/02 12:20:40 by tomoron ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "libft/libft.h"
|
||||
|
||||
static void skip_nbr(char **line)
|
||||
{
|
||||
while(ft_isdigit(**line))
|
||||
(*line)++;
|
||||
while(**line == ' ')
|
||||
(*line)++;
|
||||
}
|
||||
|
||||
static long int check_line(char *line, int ignore)
|
||||
{
|
||||
int left;
|
||||
int right;
|
||||
int asc;
|
||||
int i;
|
||||
|
||||
if(ignore == 0)
|
||||
skip_nbr(&line);
|
||||
left = ft_atoi(line);
|
||||
skip_nbr(&line);
|
||||
asc = -1;
|
||||
i = 1;
|
||||
while(*line)
|
||||
{
|
||||
if(i == ignore)
|
||||
skip_nbr(&line);
|
||||
if(!*line)
|
||||
break;
|
||||
right = ft_atoi(line);
|
||||
skip_nbr(&line);
|
||||
if(right == left || (left > right && asc == 1) || (left < right && asc == 0))
|
||||
return(0);
|
||||
if(left > right)
|
||||
asc = 0;
|
||||
if(left < right)
|
||||
asc = 1;
|
||||
if(left - right > 3 || left - right < -3)
|
||||
return(0);
|
||||
left = right;
|
||||
i++;
|
||||
}
|
||||
return(1);
|
||||
}
|
||||
|
||||
static int count_char(char *str, char c)
|
||||
{
|
||||
int res;
|
||||
|
||||
res = 0;
|
||||
while(*str)
|
||||
{
|
||||
if(*str == c)
|
||||
res++;
|
||||
str++;
|
||||
}
|
||||
return(res);
|
||||
}
|
||||
|
||||
long int resolve_part2(char *input, char **split)
|
||||
{
|
||||
(void)input;
|
||||
long int res;
|
||||
int i;
|
||||
int j;
|
||||
int len;
|
||||
|
||||
i = 0;
|
||||
res = 0;
|
||||
while(split[i])
|
||||
{
|
||||
if(check_line(split[i], -1))
|
||||
res++;
|
||||
else
|
||||
{
|
||||
len = count_char(split[i], ' ') + 1;
|
||||
j = 0;
|
||||
while(j < len)
|
||||
{
|
||||
if(check_line(split[i], j))
|
||||
{
|
||||
res++;
|
||||
break;
|
||||
}
|
||||
j++;
|
||||
}
|
||||
}
|
||||
i++;
|
||||
}
|
||||
return(res);
|
||||
}
|
7
2024/2/test
Normal file
7
2024/2/test
Normal file
@ -0,0 +1,7 @@
|
||||
7 6 4 2 1
|
||||
1 2 7 8 9
|
||||
9 7 6 2 1
|
||||
1 3 2 4 5
|
||||
8 6 4 4 1
|
||||
1 3 6 7 9
|
||||
1 1 5 6 7 9
|
Reference in New Issue
Block a user