add day 8 and add free on day 7
This commit is contained in:
110
2024/8/part1.c
Normal file
110
2024/8/part1.c
Normal file
@ -0,0 +1,110 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* part1.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: tomoron <tomoron@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/07/17 23:03:36 by tomoron #+# #+# */
|
||||
/* Updated: 2024/12/08 13:37:33 by tomoron ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "libft/libft.h"
|
||||
|
||||
static long int get_nb_antinode_char(char **map, char **locations, int limits[2], int ref_pos[2])
|
||||
{
|
||||
char c;
|
||||
long int res;
|
||||
int pos[2];
|
||||
int np[2];
|
||||
|
||||
res = 0;
|
||||
c = map[ref_pos[0]][ref_pos[1]];
|
||||
pos[0] = 0;
|
||||
while(map[pos[0]])
|
||||
{
|
||||
pos[1] = 0;
|
||||
while(map[pos[0]][pos[1]])
|
||||
{
|
||||
if(ref_pos[0] != pos[0] && ref_pos[1] != pos[1] && map[pos[0]][pos[1]] == c)
|
||||
{
|
||||
np[0] = ref_pos[0] - (pos[0] - ref_pos[0]);
|
||||
np[1] = ref_pos[1] - (pos[1] - ref_pos[1]);
|
||||
if(np[0] >= 0 && np[0] < limits[0] && np[1] >= 0 && np[1] < limits[1] && locations[np[0]][np[1]] != '#')
|
||||
{
|
||||
res++;
|
||||
locations[np[0]][np[1]] = '#';
|
||||
}
|
||||
np[0] = pos[0] + (pos[0] - ref_pos[0]);
|
||||
np[1] = pos[1] + (pos[1] - ref_pos[1]);
|
||||
if(np[0] >= 0 && np[0] < limits[0] && np[1] >= 0 && np[1] < limits[1] && locations[np[0]][np[1]] != '#')
|
||||
{
|
||||
res++;
|
||||
locations[np[0]][np[1]] = '#';
|
||||
}
|
||||
}
|
||||
pos[1]++;
|
||||
}
|
||||
pos[0]++;
|
||||
}
|
||||
map[ref_pos[0]][ref_pos[1]] = '.';
|
||||
return(res);
|
||||
}
|
||||
|
||||
static long int find_antinode(char **map, int height, int width)
|
||||
{
|
||||
int x;
|
||||
int y;
|
||||
long int res;
|
||||
char **locations;
|
||||
|
||||
y = 0;
|
||||
res = 0;
|
||||
locations = malloc(height * sizeof(char *));
|
||||
while(y < height)
|
||||
{
|
||||
locations[y] = malloc(width);
|
||||
ft_memset(locations[y], '.', width);
|
||||
y++;
|
||||
}
|
||||
y = 0;
|
||||
while(map[y])
|
||||
{
|
||||
x = 0;
|
||||
while(map[y][x])
|
||||
{
|
||||
if(map[y][x] != '.')
|
||||
res += get_nb_antinode_char(map, locations, (int [2]){height, width}, (int [2]){y, x});
|
||||
x++;
|
||||
}
|
||||
y++;
|
||||
}
|
||||
y = 0;
|
||||
while(y < height)
|
||||
{
|
||||
free(locations[y]);
|
||||
y++;
|
||||
}
|
||||
free(locations);
|
||||
return(res);
|
||||
}
|
||||
|
||||
long int resolve_part1(char *input, char **split)
|
||||
{
|
||||
(void)input;
|
||||
int height;
|
||||
int width;
|
||||
long int res;
|
||||
|
||||
height = 0;
|
||||
while(split[height])
|
||||
height++;
|
||||
width = 0;
|
||||
while(split[0][width])
|
||||
width++;
|
||||
res = find_antinode(split, height, width);
|
||||
return(res);
|
||||
}
|
Reference in New Issue
Block a user