remove inputs
This commit is contained in:
69
2022/9/Makefile
Normal file
69
2022/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/11/01 13:13:44 by tomoron ### ########.fr #
|
||||
# #
|
||||
# **************************************************************************** #
|
||||
|
||||
SRCS = main.c\
|
||||
part1.c\
|
||||
part2.c
|
||||
|
||||
OBJS = $(SRCS:.c=.o)
|
||||
|
||||
FLAGS = -Wextra -Werror -Wall -g -lm -Wno-unused-command-line-argument
|
||||
|
||||
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/9/main.c
Normal file
67
2022/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);
|
||||
}
|
83
2022/9/part1.c
Normal file
83
2022/9/part1.c
Normal file
@ -0,0 +1,83 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* part1.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: tomoron <tomoron@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/07/17 23:03:36 by tomoron #+# #+# */
|
||||
/* Updated: 2024/11/04 13:32:41 by tomoron ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <math.h>
|
||||
#include "libft/libft.h"
|
||||
#include "structs.h"
|
||||
|
||||
t_instruction get_instruction(char *line)
|
||||
{
|
||||
t_instruction res;
|
||||
|
||||
res.dir[0] = 0;
|
||||
res.dir[1] = 0;
|
||||
res.rep = 0;
|
||||
if(!line[0])
|
||||
return(res);
|
||||
|
||||
if(line[0] == 'U')
|
||||
res.dir[1] = -1;
|
||||
if(line[0] == 'D')
|
||||
res.dir[1] = 1;
|
||||
if(line[0] == 'L')
|
||||
res.dir[0] = -1;
|
||||
if(line[0] == 'R')
|
||||
res.dir[0] = 1;
|
||||
res.rep = ft_atoi(line + 2);
|
||||
return(res);
|
||||
}
|
||||
|
||||
static void move_tail(int tail[2], int head[2])
|
||||
{
|
||||
int dist_up;
|
||||
int dist_side;
|
||||
|
||||
dist_up = tail[0] - head[0];
|
||||
dist_side = tail[1] - head[1];
|
||||
if(dist_up == -2)
|
||||
tail[0] = head[0] - 1;
|
||||
if(dist_up == 2)
|
||||
tail[0] = head[0] + 1;
|
||||
if(dist_side == -2)
|
||||
tail[1] = head[1] - 1;
|
||||
if(dist_side == 2)
|
||||
tail[1] = head[1] + 1;
|
||||
}
|
||||
|
||||
long int resolve_part1(char *input, char **split)
|
||||
{
|
||||
(void)input;
|
||||
int i;
|
||||
int head[2];
|
||||
int tail[2];
|
||||
t_instruction instr;
|
||||
|
||||
i = 0;
|
||||
ft_bzero(head, sizeof(int) * 2);
|
||||
ft_bzero(tail, sizeof(int) * 2);
|
||||
while(split[i])
|
||||
{
|
||||
instr = get_instruction(split[i]);
|
||||
while(instr.rep > 0)
|
||||
{
|
||||
head[0] += instr.dir[0];
|
||||
head[1] += instr.dir[1];
|
||||
printf("%d, %d\n", head[0], head[1]);
|
||||
move_tail(tail, head);
|
||||
instr.rep--;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
return(0);
|
||||
}
|
22
2022/9/part2.c
Normal file
22
2022/9/part2.c
Normal file
@ -0,0 +1,22 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* part2.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: tomoron <tomoron@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/07/17 23:03:36 by tomoron #+# #+# */
|
||||
/* Updated: 2024/07/17 23:18:02 by tomoron ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "libft/libft.h"
|
||||
|
||||
long int resolve_part2(char *input, char **split)
|
||||
{
|
||||
(void)input;
|
||||
(void)split;
|
||||
return(0);
|
||||
}
|
23
2022/9/structs.h
Normal file
23
2022/9/structs.h
Normal file
@ -0,0 +1,23 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* structs.h :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: tomoron <tomoron@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/11/01 12:50:02 by tomoron #+# #+# */
|
||||
/* Updated: 2024/11/01 13:01:52 by tomoron ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef STRUCTS_H
|
||||
# define STRUCTS_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
typedef struct s_instruction
|
||||
{
|
||||
int dir[2];
|
||||
int rep;
|
||||
} t_instruction;
|
||||
#endif
|
8
2022/9/test
Normal file
8
2022/9/test
Normal file
@ -0,0 +1,8 @@
|
||||
R 4
|
||||
U 4
|
||||
L 3
|
||||
D 1
|
||||
R 4
|
||||
D 1
|
||||
L 5
|
||||
R 2
|
Reference in New Issue
Block a user