day 2, part 2 working!

This commit is contained in:
Parker Macdonald 2024-12-08 02:45:43 -07:00
parent 4bcf49b54c
commit 1782eec64f

123
day_8/2.c Normal file
View file

@ -0,0 +1,123 @@
#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++;
}
}
}
}
#ifdef DEBUG
for (unsigned int i = 0; i < GRID_SIZE; i++) {
for (unsigned int j = 0; j < GRID_SIZE; j++) {
unsigned char found_freq = 0;
for (unsigned int k = 0; k < 'z' + 1; k++) {
for (unsigned int l = 0; l < data[k].index; l++) {
if (data[k].points[l].x == j && data[k].points[l].y == i) {
printf("%c", k);
found_freq = 1;
break;
}
}
if (found_freq) {
break;
}
}
if (!found_freq) {
if (antinodes[i][j] == 1) {
printf("#");
continue;
}
printf(".");
}
}
printf("\n");
}
#endif
printf("%u\n", sum);
fclose(fp);
return 0;
}