add day 15

This commit is contained in:
2024-12-15 15:07:14 +01:00
parent 6f1b66db46
commit b6b46e0d82
5 changed files with 508 additions and 0 deletions

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

136
2024/15/part1.c Normal file
View File

@ -0,0 +1,136 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* part1.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tomoron <tomoron@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/17 23:03:36 by tomoron #+# #+# */
/* Updated: 2024/12/15 14:45:04 by tomoron ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdio.h>
#include <stdlib.h>
#include "libft/libft.h"
typedef struct s_robot
{
int x;
int y;
} t_robot;
static t_robot get_robot(char **map)
{
int i;
int j;
i = 0;
while(map[i])
{
j = 0;
while(map[i][j])
{
if(map[i][j] == '@')
{
map[i][j] = '.';
return((t_robot){j, i});
}
j++;
}
i++;
}
return((t_robot){0, 0});
}
static void try_move(char **map, int dir[2], t_robot *robot)
{
int i;
int j;
i = robot->y + dir[0];
j = robot->x + dir[1];
while(map[i][j] == 'O')
{
i += dir[0];
j += dir[1];
}
if(map[i][j] == '#')
return;
robot->y += dir[0];
robot->x += dir[1];
if(map[robot->y][robot->x] == 'O')
{
map[robot->y][robot->x] = '.';
map[i][j] = 'O';
}
}
static long int calc_score(char **map)
{
int i;
int j;
long int res;
i = 0;
res = 0;
while(map[i][0] == '#')
{
j = 0;
while(map[i][j])
{
if(map[i][j] == 'O')
res += (100 * i) + j;
j++;
}
i++;
}
return(res);
}
static void exec_line(char **map, char *line, t_robot *robot)
{
int dirs[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
int *dir;
while(*line)
{
switch(*line)
{
case '^':
dir = dirs[0];
break;
case 'v':
dir = dirs[1];
break;
case '<':
dir = dirs[2];
break;
default:
dir = dirs[3];
break;
}
try_move(map, dir, robot);
line++;
}
}
long int resolve_part1(char *input, char **split)
{
(void)input;
long int res;
t_robot robot;
char **instr;
instr = split;
while(**instr == '#')
instr++;
robot = get_robot(split);
while(*instr)
{
exec_line(split, *instr, &robot);
instr++;
}
res = calc_score(split);
return(res);
}

214
2024/15/part2.c Normal file
View File

@ -0,0 +1,214 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* part2.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tomoron <tomoron@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/17 23:03:36 by tomoron #+# #+# */
/* Updated: 2024/12/15 15:06:58 by tomoron ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdio.h>
#include <stdlib.h>
#include "libft/libft.h"
typedef struct s_robot
{
int x;
int y;
} t_robot;
static t_robot get_robot(char **map)
{
int i;
int j;
i = 0;
while(map[i])
{
j = 0;
while(map[i][j])
{
if(map[i][j] == '@')
{
map[i][j] = '.';
return((t_robot){j, i});
}
j++;
}
i++;
}
return((t_robot){0, 0});
}
static int move(char **map, int dir[2], int pos[2], char c, int check)
{
int np[2];
np[0] = pos[0] + dir[0];
np[1] = pos[1] + dir[1];
if(map[np[0]][np[1]] == '#')
return(0);
if(map[np[0]][np[1]] == '.')
{
if(!check)
{
map[np[0]][np[1]] = c;
map[pos[0]][pos[1]] = '.';
}
return(1);
}
if((map[np[0]][np[1]] == ']'
&& move(map, dir, (int [2]){np[0], np[1] - 1},'[', check)
&& ((dir[1] == -1 && check) || move(map, dir, np, ']', check)))
|| (map[np[0]][np[1]] == '['
&& move(map, dir, (int [2]){np[0], np[1] + 1},']', check)
&& ((dir[1] == 1 && check) || move(map, dir, np, '[', check)))
)
{
if(!check)
{
map[np[0]][np[1]] = c;
map[pos[0]][pos[1]] = '.';
}
return(1);
}
return(0);
}
static long int calc_score(char **map)
{
int i;
int j;
long int res;
i = 0;
res = 0;
while(map[i] && map[i][0] == '#')
{
j = 0;
while(map[i][j])
{
if(map[i][j] == '[')
res += (100 * i) + j;
j++;
}
i++;
}
return(res);
}
static void exec_line(char **map, char *line, t_robot *robot)
{
int dirs[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
int *dir;
while(*line)
{
switch(*line)
{
case '^':
dir = dirs[0];
break;
case 'v':
dir = dirs[1];
break;
case '<':
dir = dirs[2];
break;
default:
dir = dirs[3];
break;
}
if(move(map, dir, (int [2]){robot->y, robot->x}, map[robot->y][robot->x], 1))
{
move(map, dir, (int [2]){robot->y, robot->x}, map[robot->y][robot->x], 0);
robot->y += dir[0];
robot->x += dir[1];
}
line++;
}
}
void enlarge_line(char *src, char *dest)
{
int i;
i = 0;
while(*src)
{
if(*src == '#')
{
dest[i] = '#';
dest[i + 1] = '#';
}
if(*src == 'O')
{
dest[i] = '[';
dest[i + 1] = ']';
}
if(*src == '@')
{
dest[i] = '@';
dest[i + 1] = '.';
}
if(*src == '.')
{
dest[i] = '.';
dest[i + 1] = '.';
}
src++;
i += 2;
}
}
char **enlarge_map(char **map)
{
char **res;
int width;
int height;
int i;
height = 0;
while(map[height][0] == '#')
height++;
width = 0;
while(map[0][width])
width++;
width *= 2;
res = malloc((height + 1) * sizeof(char *));
res[height] = 0;
i = 0;
while(i < height)
{
res[i] = malloc(width + 1);
res[i][width] = 0;
enlarge_line(map[i], res[i]);
i++;
}
return(res);
}
long int resolve_part2(char *input, char **split)
{
(void)input;
long int res;
t_robot robot;
char **instr;
instr = split;
while(**instr == '#')
instr++;
split = enlarge_map(split);
robot = get_robot(split);
while(*instr)
{
exec_line(split, *instr, &robot);
instr++;
}
res = calc_score(split);
return(res);
}

22
2024/15/test Normal file
View File

@ -0,0 +1,22 @@
##########
#..O..O.O#
#......O.#
#.OO..O.O#
#..O@..O.#
#O#..O...#
#O..O..O.#
#.OO.O.OO#
#....O...#
##########
<vv>^<v^>v>^vv^v>v<>v^v<v<^vv<<<^><<><>>v<vvv<>^v^>^<<<><<v<<<v^vv^v>^
vvv<<^>^v^^><<>>><>^<<><^vv^^<>vvv<>><^^v>^>vv<>v<<<<v<^v>^<^^>>>^<v<v
><>vv>v^v^<>><>>>><^^>vv>v<^^^>>v^v^<^^>v^^>v^<^v>v<>>v^v^<v>v^^<^^vv<
<<v<^>>^^^^>>>v^<>vvv^><v<<<>^^^vv^<vvv>^>v<^^^^v<>^>vvvv><>>v^<<^^^^^
^><^><>>><>^^<<^^v>>><^<v>^<vv>>v>>>^v><>^v><<<<v>>v<v<v>vvv>^<><<>^><
^>><>^v<><^vvv<^^<><v<<<<<><^v<<<><<<^^<v<^^^><^>>^<v^><<<^>>^v<v^v<v^
>^>>^v>vv>^<<^v<>><<><<v<<v><>v<^vv<<<>^^v^>^^>>><<^v>>v^v><^^>>^<>vv^
<><^^>^^^<><vvvvv^v<v<<>^v<v>v<<^><<><<><<<^^<<<^<<>><<><^^^>^^<>^>v<>
^^>vv<^v^v<vv>^<><v<^v>^^^>>>^^vvv^>vvv<>>>^<^>>>>>^<<^v>^vvv<>^<><<v>
v^^>>><<^^<>>^v^<v^vv<>v^<<>^<^v^v><^<<<><<^<v><v<>vv>>v><v^<vv<>v^<<^