add day 9
This commit is contained in:
69
2024/9/Makefile
Normal file
69
2024/9/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/9/main.c
Normal file
67
2024/9/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);
|
||||||
|
}
|
98
2024/9/part1.c
Normal file
98
2024/9/part1.c
Normal file
@ -0,0 +1,98 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* part1.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: tomoron <tomoron@student.42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/07/17 23:03:36 by tomoron #+# #+# */
|
||||||
|
/* Updated: 2024/12/09 14:23:36 by tomoron ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include "libft/libft.h"
|
||||||
|
|
||||||
|
static void add_nbr(int **res, int nb, int index)
|
||||||
|
{
|
||||||
|
int free_space;
|
||||||
|
|
||||||
|
free_space = index % 2;
|
||||||
|
if(index)
|
||||||
|
index /= 2;
|
||||||
|
while(nb > 0)
|
||||||
|
{
|
||||||
|
if(free_space)
|
||||||
|
**res = -1;
|
||||||
|
else
|
||||||
|
**res = index;
|
||||||
|
(*res)++;
|
||||||
|
nb--;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static int *get_nbrs(char *input, size_t *len)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
int *nbrs;
|
||||||
|
int *res;
|
||||||
|
|
||||||
|
*len = 0;
|
||||||
|
i = 0;
|
||||||
|
while(input[i] && ft_isdigit(input[i]))
|
||||||
|
{
|
||||||
|
*len += input[i] - '0';
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
res = malloc(*len * sizeof(int));
|
||||||
|
nbrs = res;
|
||||||
|
i = 0;
|
||||||
|
while(input[i])
|
||||||
|
{
|
||||||
|
add_nbr(&nbrs, input[i] - '0', i);
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
return(res);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void frag_disk(int *disk, int len)
|
||||||
|
{
|
||||||
|
int *fs_ptr;
|
||||||
|
int *end_ptr;
|
||||||
|
|
||||||
|
fs_ptr = disk;
|
||||||
|
end_ptr = disk + (len - 1);
|
||||||
|
while(end_ptr != fs_ptr)
|
||||||
|
{
|
||||||
|
while(*fs_ptr != -1 && fs_ptr < end_ptr)
|
||||||
|
fs_ptr++;
|
||||||
|
while(*end_ptr == -1 && end_ptr > fs_ptr)
|
||||||
|
end_ptr--;
|
||||||
|
*fs_ptr = *end_ptr;
|
||||||
|
*end_ptr = -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
long int resolve_part1(char *input, char **split)
|
||||||
|
{
|
||||||
|
(void)split;
|
||||||
|
int *disk;
|
||||||
|
size_t len;
|
||||||
|
size_t i;
|
||||||
|
long int res;
|
||||||
|
disk = get_nbrs(input, &len);
|
||||||
|
frag_disk(disk, len);
|
||||||
|
res = 0;
|
||||||
|
i = 0;
|
||||||
|
while(i < len)
|
||||||
|
{
|
||||||
|
if(disk[i] != -1)
|
||||||
|
res += disk[i] * i;
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
free(disk);
|
||||||
|
return(res);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
128
2024/9/part2.c
Normal file
128
2024/9/part2.c
Normal file
@ -0,0 +1,128 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* part2.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: tomoron <tomoron@student.42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/07/17 23:03:36 by tomoron #+# #+# */
|
||||||
|
/* Updated: 2024/12/09 14:24:07 by tomoron ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include "libft/libft.h"
|
||||||
|
|
||||||
|
static void add_nbr(int **res, int nb, int index)
|
||||||
|
{
|
||||||
|
int free_space;
|
||||||
|
|
||||||
|
free_space = index % 2;
|
||||||
|
if(index)
|
||||||
|
index /= 2;
|
||||||
|
while(nb > 0)
|
||||||
|
{
|
||||||
|
if(free_space)
|
||||||
|
**res = -1;
|
||||||
|
else
|
||||||
|
**res = index;
|
||||||
|
(*res)++;
|
||||||
|
nb--;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static int *get_nbrs(char *input, size_t *len)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
int *nbrs;
|
||||||
|
int *res;
|
||||||
|
|
||||||
|
*len = 0;
|
||||||
|
i = 0;
|
||||||
|
while(input[i] && ft_isdigit(input[i]))
|
||||||
|
{
|
||||||
|
*len += input[i] - '0';
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
res = malloc(*len * sizeof(int));
|
||||||
|
nbrs = res;
|
||||||
|
i = 0;
|
||||||
|
while(input[i])
|
||||||
|
{
|
||||||
|
add_nbr(&nbrs, input[i] - '0', i);
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
return(res);
|
||||||
|
}
|
||||||
|
|
||||||
|
static int *find_place(int *disk, int bloc_len, int *end)
|
||||||
|
{
|
||||||
|
int free_len;
|
||||||
|
|
||||||
|
while(disk < end)
|
||||||
|
{
|
||||||
|
free_len = 0;
|
||||||
|
while(*disk != -1 && disk < end)
|
||||||
|
disk++;
|
||||||
|
while(disk[free_len] == -1 && disk < end)
|
||||||
|
free_len++;
|
||||||
|
if(free_len >= bloc_len)
|
||||||
|
return(disk);
|
||||||
|
disk += free_len;
|
||||||
|
}
|
||||||
|
return(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void frag_disk(int *disk, int len)
|
||||||
|
{
|
||||||
|
int *fs_ptr;
|
||||||
|
int *end_ptr;
|
||||||
|
int bloc_len;
|
||||||
|
int tmp;
|
||||||
|
|
||||||
|
fs_ptr = disk;
|
||||||
|
end_ptr = disk + (len - 1);
|
||||||
|
while(end_ptr > disk)
|
||||||
|
{
|
||||||
|
bloc_len = 0;
|
||||||
|
fs_ptr = disk;
|
||||||
|
while(*end_ptr == -1 && end_ptr > disk)
|
||||||
|
end_ptr--;
|
||||||
|
tmp = *end_ptr;
|
||||||
|
while(end_ptr[-bloc_len] == tmp && end_ptr > disk)
|
||||||
|
bloc_len++;
|
||||||
|
fs_ptr = find_place(disk, bloc_len, end_ptr);
|
||||||
|
while(fs_ptr && bloc_len > 0)
|
||||||
|
{
|
||||||
|
*fs_ptr = *end_ptr;
|
||||||
|
*end_ptr = -1;
|
||||||
|
fs_ptr++;
|
||||||
|
end_ptr--;
|
||||||
|
bloc_len--;
|
||||||
|
}
|
||||||
|
if(!fs_ptr)
|
||||||
|
end_ptr -= bloc_len;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
long int resolve_part2(char *input, char **split)
|
||||||
|
{
|
||||||
|
(void)split;
|
||||||
|
int *disk;
|
||||||
|
size_t len;
|
||||||
|
size_t i;
|
||||||
|
long int res;
|
||||||
|
disk = get_nbrs(input, &len);
|
||||||
|
frag_disk(disk, len);
|
||||||
|
i = 0;
|
||||||
|
res = 0;
|
||||||
|
while(i < len)
|
||||||
|
{
|
||||||
|
if(disk[i] != -1)
|
||||||
|
res += disk[i] * i;
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
free(disk);
|
||||||
|
return(res);
|
||||||
|
}
|
1
2024/9/test
Normal file
1
2024/9/test
Normal file
@ -0,0 +1 @@
|
|||||||
|
2333133121414131402
|
Reference in New Issue
Block a user