remove inputs
This commit is contained in:
69
2022/5/Makefile
Normal file
69
2022/5/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
2022/5/main.c
Normal file
67
2022/5/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);
|
||||
}
|
210
2022/5/part1.c
Normal file
210
2022/5/part1.c
Normal file
@ -0,0 +1,210 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* part1.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: tomoron <tomoron@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/07/17 23:03:36 by tomoron #+# #+# */
|
||||
/* Updated: 2024/10/29 02:51:51 by tomoron ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include "libft/libft.h"
|
||||
#include "structs.h"
|
||||
|
||||
static t_stack_list *get_next_stack_node(t_stack_list **stacks, t_stack_list *cur)
|
||||
{
|
||||
if(!*stacks)
|
||||
*stacks = ft_calloc(1, sizeof(t_stack_list));
|
||||
if(!cur)
|
||||
return(*stacks);
|
||||
if(!cur->next)
|
||||
cur->next = ft_calloc(1, sizeof(t_stack_list));
|
||||
return(cur->next);
|
||||
}
|
||||
|
||||
static void add_crate(t_stack_list *node, char letter)
|
||||
{
|
||||
t_crate *bottom;
|
||||
t_crate *tmp;
|
||||
|
||||
bottom = node->crate;
|
||||
while(bottom && bottom->prev)
|
||||
bottom = bottom->prev;
|
||||
|
||||
tmp = ft_calloc(1, sizeof(t_crate));
|
||||
tmp->letter = letter;
|
||||
tmp->prev = 0;
|
||||
|
||||
if(bottom)
|
||||
bottom->prev = tmp;
|
||||
else
|
||||
node->crate = tmp;
|
||||
}
|
||||
|
||||
static void get_line_crates(char *line, t_stack_list **stacks)
|
||||
{
|
||||
int i;
|
||||
int len;
|
||||
t_stack_list *cur;
|
||||
|
||||
i = 1;
|
||||
len = ft_strlen(line);
|
||||
cur = get_next_stack_node(stacks, 0);
|
||||
while(i < len)
|
||||
{
|
||||
if(line[i] >= 'A' && line[i] <= 'Z')
|
||||
add_crate(cur, line[i]);
|
||||
i += 4;
|
||||
cur = get_next_stack_node(stacks, cur);
|
||||
}
|
||||
}
|
||||
|
||||
static t_stack_list *parse_input_stacks(char **split)
|
||||
{
|
||||
t_stack_list *stacks;
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
stacks = 0;
|
||||
while(split[i] && ft_strlen(split[i]) > 2 && !(split[i][1] >= '0' && split[i][1] <= '9'))
|
||||
{
|
||||
get_line_crates(split[i], &stacks);
|
||||
i++;
|
||||
}
|
||||
return(stacks);
|
||||
}
|
||||
|
||||
static t_instruction parse_instruction(char *line)
|
||||
{
|
||||
t_instruction res;
|
||||
|
||||
res.nb = 1;
|
||||
res.from = 1;
|
||||
res.to = 1;
|
||||
if(line && ft_strlen(line) >= 18)
|
||||
{
|
||||
line += 5;
|
||||
res.nb = ft_atoi(line);
|
||||
while(*line >= '0' && *line <='9')
|
||||
line++;
|
||||
line += 6;
|
||||
res.from = ft_atoi(line);
|
||||
while(*line >= '0' && *line <='9')
|
||||
line++;
|
||||
line += 4;
|
||||
res.to = ft_atoi(line);
|
||||
}
|
||||
return(res);
|
||||
}
|
||||
|
||||
static t_stack_list *stack_from_index(t_stack_list *stacks, int index)
|
||||
{
|
||||
int i;
|
||||
|
||||
i = 1;
|
||||
while(stacks->next && i < index)
|
||||
{
|
||||
stacks = stacks->next;
|
||||
i++;
|
||||
}
|
||||
return(stacks);
|
||||
}
|
||||
|
||||
static void print_stack(t_stack_list *stack)
|
||||
{
|
||||
if(!stack->crate)
|
||||
return;
|
||||
printf("%c", stack->crate->letter);
|
||||
return;
|
||||
t_crate *tmp;
|
||||
|
||||
tmp = stack->crate;
|
||||
while(tmp)
|
||||
{
|
||||
printf("%c",tmp->letter);
|
||||
tmp = tmp->prev;
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
static void print_result(t_stack_list *stacks)
|
||||
{
|
||||
while(stacks)
|
||||
{
|
||||
print_stack(stacks);
|
||||
stacks = stacks->next;
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
static void exec_instruction(int rep, t_stack_list *from, t_stack_list *to)
|
||||
{
|
||||
int i;
|
||||
t_crate *tmp;
|
||||
|
||||
i = 0;
|
||||
while(i < rep)
|
||||
{
|
||||
i++;
|
||||
if(!from->crate)
|
||||
break;
|
||||
tmp = from->crate;
|
||||
from->crate = from->crate->prev;
|
||||
tmp->prev = to->crate;
|
||||
to->crate = tmp;
|
||||
}
|
||||
}
|
||||
|
||||
static void do_all_instructions(char **split,t_stack_list *stacks)
|
||||
{
|
||||
int i;
|
||||
t_instruction instr;
|
||||
|
||||
(void)stacks;
|
||||
i = 0;
|
||||
while(split[i] && (split[i][0] == ' ' || split[i][0] == '['))
|
||||
i++;
|
||||
while(split[i])
|
||||
{
|
||||
instr = parse_instruction(split[i]);
|
||||
exec_instruction(instr.nb, stack_from_index(stacks, instr.from), stack_from_index(stacks, instr.to));
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
static void free_stacks(t_stack_list *stacks)
|
||||
{
|
||||
void *tmp;
|
||||
|
||||
while(stacks)
|
||||
{
|
||||
while(stacks->crate)
|
||||
{
|
||||
tmp = stacks->crate->prev;
|
||||
free(stacks->crate);
|
||||
stacks->crate = tmp;
|
||||
}
|
||||
tmp = stacks->next;
|
||||
free(stacks);
|
||||
stacks = tmp;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
long int resolve_part1(char *input, char **split)
|
||||
{
|
||||
(void)input;
|
||||
(void)split;
|
||||
t_stack_list *stacks;
|
||||
|
||||
stacks = parse_input_stacks(split);
|
||||
do_all_instructions(split, stacks);
|
||||
print_result(stacks);
|
||||
free_stacks(stacks);
|
||||
return(0);
|
||||
}
|
211
2022/5/part2.c
Normal file
211
2022/5/part2.c
Normal file
@ -0,0 +1,211 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* part2.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: tomoron <tomoron@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/07/17 23:03:36 by tomoron #+# #+# */
|
||||
/* Updated: 2024/10/29 02:54:08 by tomoron ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include "libft/libft.h"
|
||||
#include "structs.h"
|
||||
|
||||
static t_stack_list *get_next_stack_node(t_stack_list **stacks, t_stack_list *cur)
|
||||
{
|
||||
if(!*stacks)
|
||||
*stacks = ft_calloc(1, sizeof(t_stack_list));
|
||||
if(!cur)
|
||||
return(*stacks);
|
||||
if(!cur->next)
|
||||
cur->next = ft_calloc(1, sizeof(t_stack_list));
|
||||
return(cur->next);
|
||||
}
|
||||
|
||||
static void add_crate(t_stack_list *node, char letter)
|
||||
{
|
||||
t_crate *bottom;
|
||||
t_crate *tmp;
|
||||
|
||||
bottom = node->crate;
|
||||
while(bottom && bottom->prev)
|
||||
bottom = bottom->prev;
|
||||
|
||||
tmp = ft_calloc(1, sizeof(t_crate));
|
||||
tmp->letter = letter;
|
||||
tmp->prev = 0;
|
||||
|
||||
if(bottom)
|
||||
bottom->prev = tmp;
|
||||
else
|
||||
node->crate = tmp;
|
||||
}
|
||||
|
||||
static void get_line_crates(char *line, t_stack_list **stacks)
|
||||
{
|
||||
int i;
|
||||
int len;
|
||||
t_stack_list *cur;
|
||||
|
||||
i = 1;
|
||||
len = ft_strlen(line);
|
||||
cur = get_next_stack_node(stacks, 0);
|
||||
while(i < len)
|
||||
{
|
||||
if(line[i] >= 'A' && line[i] <= 'Z')
|
||||
add_crate(cur, line[i]);
|
||||
i += 4;
|
||||
cur = get_next_stack_node(stacks, cur);
|
||||
}
|
||||
}
|
||||
|
||||
static t_stack_list *parse_input_stacks(char **split)
|
||||
{
|
||||
t_stack_list *stacks;
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
stacks = 0;
|
||||
while(split[i] && ft_strlen(split[i]) > 2 && !(split[i][1] >= '0' && split[i][1] <= '9'))
|
||||
{
|
||||
get_line_crates(split[i], &stacks);
|
||||
i++;
|
||||
}
|
||||
return(stacks);
|
||||
}
|
||||
|
||||
static t_instruction parse_instruction(char *line)
|
||||
{
|
||||
t_instruction res;
|
||||
|
||||
res.nb = 1;
|
||||
res.from = 1;
|
||||
res.to = 1;
|
||||
if(line && ft_strlen(line) >= 18)
|
||||
{
|
||||
line += 5;
|
||||
res.nb = ft_atoi(line);
|
||||
while(*line >= '0' && *line <='9')
|
||||
line++;
|
||||
line += 6;
|
||||
res.from = ft_atoi(line);
|
||||
while(*line >= '0' && *line <='9')
|
||||
line++;
|
||||
line += 4;
|
||||
res.to = ft_atoi(line);
|
||||
}
|
||||
return(res);
|
||||
}
|
||||
|
||||
static t_stack_list *stack_from_index(t_stack_list *stacks, int index)
|
||||
{
|
||||
int i;
|
||||
|
||||
i = 1;
|
||||
while(stacks->next && i < index)
|
||||
{
|
||||
stacks = stacks->next;
|
||||
i++;
|
||||
}
|
||||
return(stacks);
|
||||
}
|
||||
|
||||
static void print_stack(t_stack_list *stack)
|
||||
{
|
||||
if(!stack->crate)
|
||||
return;
|
||||
printf("%c", stack->crate->letter);
|
||||
return;
|
||||
t_crate *tmp;
|
||||
|
||||
tmp = stack->crate;
|
||||
while(tmp)
|
||||
{
|
||||
printf("%c",tmp->letter);
|
||||
tmp = tmp->prev;
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
static void print_result(t_stack_list *stacks)
|
||||
{
|
||||
while(stacks)
|
||||
{
|
||||
print_stack(stacks);
|
||||
stacks = stacks->next;
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
static void exec_instruction(int rep, t_stack_list *from, t_stack_list *to)
|
||||
{
|
||||
int i;
|
||||
t_crate *tmp;
|
||||
t_crate *top;
|
||||
|
||||
i = 1;
|
||||
top = from->crate;
|
||||
tmp = top;
|
||||
while(i < rep && tmp->prev)
|
||||
{
|
||||
tmp = tmp->prev;
|
||||
i++;
|
||||
}
|
||||
from->crate = tmp->prev;
|
||||
tmp->prev = to->crate;
|
||||
to->crate = top;
|
||||
}
|
||||
|
||||
static void do_all_instructions(char **split,t_stack_list *stacks)
|
||||
{
|
||||
int i;
|
||||
t_instruction instr;
|
||||
|
||||
(void)stacks;
|
||||
i = 0;
|
||||
while(split[i] && (split[i][0] == ' ' || split[i][0] == '['))
|
||||
i++;
|
||||
while(split[i])
|
||||
{
|
||||
instr = parse_instruction(split[i]);
|
||||
exec_instruction(instr.nb, stack_from_index(stacks, instr.from), stack_from_index(stacks, instr.to));
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
static void free_stacks(t_stack_list *stacks)
|
||||
{
|
||||
void *tmp;
|
||||
|
||||
while(stacks)
|
||||
{
|
||||
while(stacks->crate)
|
||||
{
|
||||
tmp = stacks->crate->prev;
|
||||
free(stacks->crate);
|
||||
stacks->crate = tmp;
|
||||
}
|
||||
tmp = stacks->next;
|
||||
free(stacks);
|
||||
stacks = tmp;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
long int resolve_part2(char *input, char **split)
|
||||
{
|
||||
(void)input;
|
||||
(void)split;
|
||||
t_stack_list *stacks;
|
||||
|
||||
stacks = parse_input_stacks(split);
|
||||
do_all_instructions(split, stacks);
|
||||
print_result(stacks);
|
||||
free_stacks(stacks);
|
||||
return(0);
|
||||
}
|
23
2022/5/structs.h
Normal file
23
2022/5/structs.h
Normal file
@ -0,0 +1,23 @@
|
||||
#ifndef STRUCTS_H
|
||||
# define STRUCTS_H
|
||||
|
||||
typedef struct s_crate
|
||||
{
|
||||
char letter;
|
||||
struct s_crate *prev;
|
||||
} t_crate;
|
||||
|
||||
typedef struct s_stack_list
|
||||
{
|
||||
t_crate *crate;
|
||||
struct s_stack_list *next;
|
||||
} t_stack_list;
|
||||
|
||||
typedef struct s_instruction
|
||||
{
|
||||
int nb;
|
||||
int from;
|
||||
int to;
|
||||
} t_instruction;
|
||||
|
||||
#endif
|
9
2022/5/test
Normal file
9
2022/5/test
Normal file
@ -0,0 +1,9 @@
|
||||
[D]
|
||||
[N] [C]
|
||||
[Z] [M] [P]
|
||||
1 2 3
|
||||
|
||||
move 1 from 2 to 1
|
||||
move 3 from 1 to 3
|
||||
move 2 from 2 to 1
|
||||
move 1 from 1 to 2
|
Reference in New Issue
Block a user