Files
aoc/2025/4/part1.c
2025-12-04 11:14:22 +01:00

72 lines
2.0 KiB
C

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* part1.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tomoron <tomoron@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/17 23:03:36 by tomoron #+# #+# */
/* Updated: 2025/12/04 11:12:50 by tomoron ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdio.h>
#include <stdlib.h>
#include "libft/libft.h"
static long int is_box(int x, int y, int len_x, int len_y, char **map)
{
if(x < 0 || y < 0 || x >= len_x || y >= len_y)
return(0);
return(map[y][x] == '@' || map[y][x] == 'x');
}
static long int count_neighbors(int x, int y, int len_x, int len_y, char **split)
{
int res = 0;
res += is_box(x - 1, y - 1, len_x, len_y, split);
res += is_box(x, y -1, len_x, len_y, split);
res += is_box(x + 1, y - 1, len_x, len_y, split);
res += is_box(x + 1, y, len_x, len_y, split);
res += is_box(x + 1, y + 1, len_x, len_y, split);
res += is_box(x, y + 1, len_x, len_y, split);
res += is_box(x - 1, y + 1, len_x, len_y, split);
res += is_box(x - 1, y, len_x, len_y, split);
return(res);
}
long int resolve_part1(char *input, char **split)
{
(void)input;
int x;
int y;
int len_x;
int len_y;
long int res;
len_x = ft_strlen(split[0]);
len_y = 0;
res = 0;
while(split[len_y])
len_y++;
y = 0;
while(y < len_y)
{
x = 0;
while(x < len_x)
{
if(split[y][x] == '@')
if(count_neighbors(x, y, len_x, len_y, split) < 4)
{
split[y][x] = 'x';
res++;
}
x++;
}
y++;
}
return(res);
}