aoc2024/15/part1.c
2024-12-15 07:53:49 +01:00

76 lines
1.7 KiB
C

#include "_.h"
void display_map(char **map) {
for (int i = 0; map[i] != NULL; i++)
printf("%s\n", map[i]);
}
int move_object(char ***map_t, int **pos_t, int move) {
char **map = *map_t;
int *pos = *pos_t;
int add_x = move == '<' ? -1 : move == '>' ? 1 : 0;
int add_y = move == '^' ? -1 : move == 'v' ? 1 : 0;
if (map[pos[0] + add_y][pos[1] + add_x] == '#')
return 1;
int *new_pos = calloc(2, sizeof(int));
new_pos[0] = pos[0] + add_y;
new_pos[1] = pos[1] + add_x;
if (map[new_pos[0]][new_pos[1]] == 'O')
if (move_object(map_t, &new_pos, move) == 1)
return 1;
map[pos[0] + add_y][pos[1] + add_x] = map[pos[0]][pos[1]];
map[pos[0]][pos[1]] = '.';
pos[0] = pos[0] + add_y;
pos[1] = pos[1] + add_x;
return 0;
}
long sum_boxe(char **map) {
long sum = 0;
for (int i = 0; map[i] != NULL; i++)
for (int j = 0; map[i][j] != '\0'; j++)
if (map[i][j] == 'O')
sum += (100 * i + j);
return sum;
}
int main(int argc, char **argv) {
if (argc != 3)
return 1;
char **map = str_split(strdup(argv[1]), '\n');
char **move_map = str_split(strdup(argv[2]), '\n');
int *pos_robot = calloc(2, sizeof(int));
for (int i = 0; map[i] != NULL; i++) {
for (int j = 0; map[i][j] != '\0'; j++) {
if (map[i][j] == '@') {
pos_robot[0] = i;
pos_robot[1] = j;
}
}
}
for (int i = 0; move_map[i] != NULL; i++)
for (int j = 0; move_map[i][j] != '\0'; j++)
move_object(&map, &pos_robot, move_map[i][j]);
//display_map(map);
long sum = sum_boxe(map);
printf("sum: %ld\n", sum);
return 0;
}