From f6b142d0d5332b2bd308cc37da93c860be595653 Mon Sep 17 00:00:00 2001 From: Parker Macdonald Date: Sun, 8 Dec 2024 01:35:44 -0700 Subject: [PATCH] day 8 part 1 --- day_8/1.c | 91 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100644 day_8/1.c diff --git a/day_8/1.c b/day_8/1.c new file mode 100644 index 0000000..34c4d03 --- /dev/null +++ b/day_8/1.c @@ -0,0 +1,91 @@ +#include +#include + +#define GRID_SIZE 50 + +typedef struct point { + int x, y; +} point_t; + +typedef struct lots_of_points { + point_t points[4]; + unsigned char index; +} lots_of_points_t; + +int main(void) { + FILE *fp = fopen("input", "r"); + + if (fp == NULL) { + perror("input"); + return 1; + } + + char antinodes[GRID_SIZE][GRID_SIZE] = {0}; + + lots_of_points_t data['z' + 1] = {0}; + + for (unsigned int i = 0; i < GRID_SIZE; i++) { + unsigned char buffer[GRID_SIZE] = {0}; + size_t read = fread(&buffer, sizeof(char), GRID_SIZE, fp); + + if (read == 0) { + fprintf(stderr, "oopsy daisy\n"); + fclose(fp); + return 1; + } + + // move past the newline + fseek(fp, 1, SEEK_CUR); + + for (unsigned int j = 0; j < GRID_SIZE; j++) { + const unsigned char freq = buffer[j]; + if (freq != '.') { + const point_t point = {(int)j, (int)i}; + data[freq].points[data[freq].index] = point; + data[freq].index++; + } + } + } + + unsigned int sum = 0; + for (unsigned int i = 0; i < 'z' + 1; i++) { + if (data[i].index == 0) { + continue; + } + + for (unsigned int j = 0; j < data[i].index; j++) { + const point_t point = data[i].points[j]; + for (unsigned int k = 0; k < data[i].index; k++) { + if (j == k) { + continue; + } + + const point_t other_point = data[i].points[k]; + + const int dist_x = point.x - other_point.x; + const int dist_y = point.y - other_point.y; + + const point_t antinode = {point.x + dist_x, point.y + dist_y}; + + if (antinode.x >= GRID_SIZE || antinode.x < 0 || + antinode.y >= GRID_SIZE || antinode.y < 0) { + continue; + } + + if (antinodes[antinode.y][antinode.x] == 1) { + continue; + } + + antinodes[antinode.y][antinode.x] = 1; + + sum++; + } + } + } + + printf("%u\n", sum); + + fclose(fp); + + return 0; +} \ No newline at end of file