diff --git a/c/2024/4/ceres_search.c b/c/2024/4/ceres_search.c index 4561463..6f68b1a 100644 --- a/c/2024/4/ceres_search.c +++ b/c/2024/4/ceres_search.c @@ -87,6 +87,30 @@ int target_string_count(Puzzle *puzzle, int index) { return count; } +char mas[4]; +int xmas_count(Puzzle *puzzle, int index) { + if (puzzle->text[index] != 'A') return 0; + int count = 0; + int row = index / puzzle->width; + int column = index % puzzle->width; + if ( + puzzle_inbounds(puzzle, row - 1, column + 1) && + puzzle_inbounds(puzzle, row + 1, column + 1) && + puzzle_inbounds(puzzle, row + 1, column - 1) && + puzzle_inbounds(puzzle, row - 1, column - 1) + ) { + mas[0] = puzzle_char_at(puzzle, row - 1, column + 1); + mas[1] = puzzle_char_at(puzzle, row + 1, column + 1); + mas[2] = puzzle_char_at(puzzle, row + 1, column - 1); + mas[3] = puzzle_char_at(puzzle, row - 1, column - 1); + if (((mas[0] == 'M' && mas[2] == 'S') || (mas[0] == 'S' && mas[2] == 'M')) && + ((mas[1] == 'M' && mas[3] == 'S') || (mas[1] == 'S' && mas[3] == 'M'))) { + return 1; + } + } + return 0; +} + int main() { char *input = aoc_read_input(); int width = strchr(input, '\n') - input + 1; @@ -98,13 +122,15 @@ int main() { p.height = height; int sum = 0; + int xmas_sum = 0; for (int i = 0; i < strlen(input); i++) { sum += target_string_count(&p, i); + xmas_sum += xmas_count(&p, i); } printf("Part 1: %d\n", sum); - // printf("Part 2: %d\n", cond_sum); + printf("Part 2: %d\n", xmas_sum); aoc_free(); return 0;