c 2024 day 4 part 1

This commit is contained in:
Bill Rossi 2024-12-04 05:51:19 -05:00
parent d7fc4fb192
commit 413d4d4631

111
c/2024/4/ceres_search.c Normal file
View File

@ -0,0 +1,111 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include "../../lib/aoc.h"
char *target_string = "XMAS";
#define TARGET_STRING_LENGTH 4
typedef struct Puzzle {
char *text;
int width;
int height;
} Puzzle;
char puzzle_char_at(Puzzle *puzzle, int row, int column) {
return puzzle->text[column + (puzzle->width * row)];
}
bool puzzle_inbounds(Puzzle *puzzle, int row, int column) {
return row < puzzle->height && row >= 0 && column >= 0 && column < puzzle->width;
}
int target_string_count(Puzzle *puzzle, int index) {
if (puzzle->text[index] != target_string[0]) return 0;
int count = 0;
int row = index / puzzle->width;
int column = index % puzzle->width;
// right
for (int i = 1; i < TARGET_STRING_LENGTH; i++) {
if (!puzzle_inbounds(puzzle, row, column + i)) break;
if (puzzle_char_at(puzzle, row, column + i) != target_string[i]) break;
if (i == TARGET_STRING_LENGTH - 1) count += 1;
}
// down right
for (int i = 1; i < TARGET_STRING_LENGTH; i++) {
if (!puzzle_inbounds(puzzle, row + i, column + i)) break;
if (puzzle_char_at(puzzle, row + i, column + i) != target_string[i]) break;
if (i == TARGET_STRING_LENGTH - 1) count += 1;
}
// down
for (int i = 1; i < TARGET_STRING_LENGTH; i++) {
if (!puzzle_inbounds(puzzle, row + i, column)) break;
if (puzzle_char_at(puzzle, row + i, column) != target_string[i]) break;
if (i == TARGET_STRING_LENGTH - 1) count += 1;
}
// down left
for (int i = 1; i < TARGET_STRING_LENGTH; i++) {
if (!puzzle_inbounds(puzzle, row + i, column - i)) break;
if (puzzle_char_at(puzzle, row + i, column - i) != target_string[i]) break;
if (i == TARGET_STRING_LENGTH - 1) count += 1;
}
// left
for (int i = 1; i < TARGET_STRING_LENGTH; i++) {
if (!puzzle_inbounds(puzzle, row, column - i)) break;
if (puzzle_char_at(puzzle, row, column - i) != target_string[i]) break;
if (i == TARGET_STRING_LENGTH - 1) count += 1;
}
// up left
for (int i = 1; i < TARGET_STRING_LENGTH; i++) {
if (!puzzle_inbounds(puzzle, row - i, column - i)) break;
if (puzzle_char_at(puzzle, row - i, column - i) != target_string[i]) break;
if (i == TARGET_STRING_LENGTH - 1) count += 1;
}
// up
for (int i = 1; i < TARGET_STRING_LENGTH; i++) {
if (!puzzle_inbounds(puzzle, row - i, column)) break;
if (puzzle_char_at(puzzle, row - i, column) != target_string[i]) break;
if (i == TARGET_STRING_LENGTH - 1) count += 1;
}
// up right
for (int i = 1; i < TARGET_STRING_LENGTH; i++) {
if (!puzzle_inbounds(puzzle, row - i, column + i)) break;
if (puzzle_char_at(puzzle, row - i, column + i) != target_string[i]) break;
if (i == TARGET_STRING_LENGTH - 1) count += 1;
}
return count;
}
int main() {
char *input = aoc_read_input();
int width = strchr(input, '\n') - input + 1;
int height = strlen(input) / width;
Puzzle p;
p.text = input;
p.width = width;
p.height = height;
int sum = 0;
for (int i = 0; i < strlen(input); i++) {
sum += target_string_count(&p, i);
}
printf("Part 1: %d\n", sum);
// printf("Part 2: %d\n", cond_sum);
aoc_free();
return 0;
}