remove inputs

This commit is contained in:
2024-12-02 12:32:14 +01:00
commit 60acac0231
247 changed files with 9071 additions and 0 deletions

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

198
2022/7/part1.c Normal file
View File

@ -0,0 +1,198 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* part1.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tomoron <tomoron@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/17 23:03:36 by tomoron #+# #+# */
/* Updated: 2024/10/29 20:32:22 by tomoron ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdio.h>
#include <stdlib.h>
#include "libft/libft.h"
#include "structs.h"
static t_directory *create_directory(char *name, t_directory *parent, void *next)
{
t_directory *res;
res = malloc(sizeof(t_directory));
res->is_dir = 1;
res->name = name;
res->next = next;
res->elements = 0;
res->parent = parent;
return(res);
}
static char *get_word(char *input)
{
int len;
char *res;
len = 0;
while(input[len] != ' ' && input[len])
len++;
res = malloc(len + 1);
res[len] = 0;
ft_memcpy(res, input, len);
return(res);
}
static void change_directory(char *arg, t_directory *root, t_directory **current)
{
t_directory *tmp;
if(!ft_strcmp(arg, "/"))
*current = root;
else if (!ft_strcmp(arg, ".."))
{
if((*current)->parent)
*current = (*current)->parent;
}
else
{
tmp = (*current)->elements;
while(tmp)
{
if(tmp->is_dir && !ft_strcmp(tmp->name, arg))
{
*current = tmp;
break;
}
tmp = tmp->next;
}
}
}
static t_file *create_file(char *name, int size, t_file *next)
{
t_file *res;
res = malloc(sizeof(t_file));
res->is_dir = 0;
res->name = name;
res->size = size;
res->next = next;
return(res);
}
static char **list(char **input, t_directory *dir)
{
char *first_arg;
char *second_arg;
(void)dir;
while(*input && **input != '$')
{
first_arg = get_word(*input);
second_arg = *input + ft_strlen(first_arg) + 1;
if(!ft_strcmp(first_arg, "dir"))
dir->elements = create_directory(second_arg, dir, dir->elements);
else
dir->elements = create_file(second_arg, ft_atoi(first_arg), dir->elements);
free(first_arg);
input++;
}
return(input);
}
static char **exec_command(char **input, t_directory *root, t_directory **current)
{
char *cmd;
if(**input != '$')
return(input + 1);
cmd = get_word(*input + 2);
if(!ft_strcmp(cmd, "cd"))
{
change_directory(*input + 5 , root, current);
free(cmd);
return(input + 1);
}
else if (!ft_strcmp(cmd, "ls"))
{
free(cmd);
return(list(input + 1, *current));
}
free(cmd);
return(input + 1);
}
static t_directory *parse_input(char **input)
{
t_directory *root;
t_directory *current;
root = create_directory("/", 0, 0);
current = root;
while(*input)
{
input = exec_command(input, root, &current);
}
return(root);
}
static long int dir_size(t_directory *dir,int depth, long int *res)
{
long int tmp_dir_size;
long int size;
t_file *tmp;
tmp = dir->elements;
size = 0;
while(tmp)
{
if(tmp->is_dir)
{
tmp_dir_size = dir_size((t_directory *)tmp, depth + 1, res);
if(tmp_dir_size <= 100000)
{
*res += tmp_dir_size;
}
size += tmp_dir_size;
}
else
size += tmp->size;
tmp = tmp->next;
}
return(size);
}
static void free_dir(t_directory *dir)
{
void *tmp;
while(dir->elements)
{
if(((t_directory *)dir->elements)->is_dir)
{
tmp = ((t_directory*)dir->elements)->next;
free_dir(dir->elements);
dir->elements = tmp;
}
else
{
tmp = ((t_directory*)dir->elements)->next;
free(dir->elements);
dir->elements = tmp;
}
}
free(dir);
}
long int resolve_part1(char *input, char **split)
{
(void)input;
t_directory *dir;
long int res;
res = 0;
dir = parse_input(split);
dir_size(dir, 0, &res);
free_dir(dir);
return(res);
}

198
2022/7/part2.c Normal file
View File

@ -0,0 +1,198 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* part2.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tomoron <tomoron@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/17 23:03:36 by tomoron #+# #+# */
/* Updated: 2024/10/29 20:32:14 by tomoron ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdio.h>
#include <stdlib.h>
#include "libft/libft.h"
#include "structs.h"
static t_directory *create_directory(char *name, t_directory *parent, void *next)
{
t_directory *res;
res = malloc(sizeof(t_directory));
res->is_dir = 1;
res->name = name;
res->next = next;
res->elements = 0;
res->parent = parent;
return(res);
}
static char *get_word(char *input)
{
int len;
char *res;
len = 0;
while(input[len] != ' ' && input[len])
len++;
res = malloc(len + 1);
res[len] = 0;
ft_memcpy(res, input, len);
return(res);
}
static void change_directory(char *arg, t_directory *root, t_directory **current)
{
t_directory *tmp;
if(!ft_strcmp(arg, "/"))
*current = root;
else if (!ft_strcmp(arg, ".."))
{
if((*current)->parent)
*current = (*current)->parent;
}
else
{
tmp = (*current)->elements;
while(tmp)
{
if(tmp->is_dir && !ft_strcmp(tmp->name, arg))
{
*current = tmp;
break;
}
tmp = tmp->next;
}
}
}
static t_file *create_file(char *name, int size, t_file *next)
{
t_file *res;
res = malloc(sizeof(t_file));
res->is_dir = 0;
res->name = name;
res->size = size;
res->next = next;
return(res);
}
static char **list(char **input, t_directory *dir)
{
char *first_arg;
char *second_arg;
(void)dir;
while(*input && **input != '$')
{
first_arg = get_word(*input);
second_arg = *input + ft_strlen(first_arg) + 1;
if(!ft_strcmp(first_arg, "dir"))
dir->elements = create_directory(second_arg, dir, dir->elements);
else
dir->elements = create_file(second_arg, ft_atoi(first_arg), dir->elements);
free(first_arg);
input++;
}
return(input);
}
static char **exec_command(char **input, t_directory *root, t_directory **current)
{
char *cmd;
if(**input != '$')
return(input + 1);
cmd = get_word(*input + 2);
if(!ft_strcmp(cmd, "cd"))
{
change_directory(*input + 5 , root, current);
free(cmd);
return(input + 1);
}
else if (!ft_strcmp(cmd, "ls"))
{
free(cmd);
return(list(input + 1, *current));
}
free(cmd);
return(input + 1);
}
static t_directory *parse_input(char **input)
{
t_directory *root;
t_directory *current;
root = create_directory("/", 0, 0);
current = root;
while(*input)
{
input = exec_command(input, root, &current);
}
return(root);
}
static long int dir_size(t_directory *dir, long int *res , int space_needed)
{
long int tmp_dir_size;
long int size;
t_file *tmp;
tmp = dir->elements;
size = 0;
while(tmp)
{
if(tmp->is_dir)
{
tmp_dir_size = dir_size((t_directory *)tmp, res, space_needed);
if(res && tmp_dir_size >= space_needed && (tmp_dir_size < *res || *res == 0))
*res = tmp_dir_size;
size += tmp_dir_size;
}
else
size += tmp->size;
tmp = tmp->next;
}
return(size);
}
static void free_dir(t_directory *dir)
{
void *tmp;
while(dir->elements)
{
if(((t_directory *)dir->elements)->is_dir)
{
tmp = ((t_directory*)dir->elements)->next;
free_dir(dir->elements);
dir->elements = tmp;
}
else
{
tmp = ((t_directory*)dir->elements)->next;
free(dir->elements);
dir->elements = tmp;
}
}
free(dir);
}
long int resolve_part2(char *input, char **split)
{
(void)input;
t_directory *dir;
long int res;
int free_space;
res = 0;
dir = parse_input(split);
free_space = 70000000 - dir_size(dir, 0, 0);
dir_size(dir, &res, 30000000 - free_space);
free_dir(dir);
return(res);
}

33
2022/7/structs.h Normal file
View File

@ -0,0 +1,33 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* structs.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tomoron <tomoron@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/29 14:20:26 by tomoron #+# #+# */
/* Updated: 2024/10/29 18:10:36 by tomoron ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef STRUCTS_H
# define STRUCTS_H
typedef struct s_file
{
int is_dir;
void *next;
char *name;
int size;
} t_file;
typedef struct s_directory
{
int is_dir;
void *next;
char *name;
void *elements;
struct s_directory *parent;
} t_directory;
#endif

23
2022/7/test Normal file
View File

@ -0,0 +1,23 @@
$ cd /
$ ls
dir a
14848514 b.txt
8504156 c.dat
dir d
$ cd a
$ ls
dir e
29116 f
2557 g
62596 h.lst
$ cd e
$ ls
584 i
$ cd ..
$ cd ..
$ cd d
$ ls
4060174 j
8033020 d.log
5626152 d.ext
7214296 k