advent_of_code/day_8/2.c

93 lines
1.9 KiB
C

#include <stdio.h>
#include <stdlib.h>
#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];
for (int l = 0;; l++) {
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 * l, point.y + dist_y * l};
if (antinode.x >= GRID_SIZE || antinode.x < 0 ||
antinode.y >= GRID_SIZE || antinode.y < 0) {
break;
}
if (antinodes[antinode.y][antinode.x] == 1) {
continue;
}
antinodes[antinode.y][antinode.x] = 1;
sum++;
}
}
}
}
printf("%u\n", sum);
fclose(fp);
return 0;
}