aoc2024/06/part2.c
2024-12-06 07:52:02 +01:00

79 lines
1.8 KiB
C

#include "_.h"
int its_infinite(char **inputs, int x, int y) {
int i = 0;
int direction = 0; // 0 = top; 1 = right; 2 = down; 3 = left
for (; i < 100000; i++) {
if (direction == 0) {
if (y - 1 < 0)
return 0;
if (inputs[y - 1][x] == '#')
direction++;
else
y--;
}
else if (direction == 1) {
if (x + 1 >= strlen(inputs[y]))
return 0;
if (inputs[y][x + 1] == '#')
direction++;
else
x++;
}
else if (direction == 2) {
if (inputs[y + 1] == NULL)
return 0;
if (inputs[y + 1][x] == '#')
direction++;
else
y++;
}
else if (direction == 3) {
if (x - 1 < 0)
return 0;
if (inputs[y][x - 1] == '#')
direction = 0;
else
x--;
}
}
return 1;
}
int main(int argc, char **argv) {
if (argc != 2)
return 1;
char *input = argv[1];
char **inputs = str_split(strdup(input), '\n');
int x = 0, y = 0;
for (int i = 0; inputs[i] != NULL; i++)
for (int j = 0; inputs[i][j] != '\0'; j++)
if (inputs[i][j] == '^') {
y = i;
x = j;
}
int count = 0;
for (int i = 0; inputs[i] != NULL; i++) {
for (int j = 0; inputs[i][j] != '\0'; j++) {
int replace = 0;
if (inputs[i][j] == '#')
replace = 1;
inputs[i][j] = '#';
count += its_infinite(inputs, x, y);
inputs[i][j] = '.';
if (replace)
inputs[i][j] = '#';
}
}
printf("count: %d\n", count);
return 0;
}