aoc_omni/c/2018/6/problem_06.c
2025-11-30 21:22:21 -05:00

84 lines
1.9 KiB
C

#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include <stdlib.h>
typedef struct {
int x;
int y;
int count;
} Coordinates;
int hat_dist(Coordinates c1, Coordinates c2) {
return abs(c1.x - c2.x) + abs(c1.y - c2.y);
}
int part_1(FILE *input) {
rewind(input);
Coordinates coord_list[50] = {0};
Coordinates *coord = coord_list;
Coordinates max = { .x = 0, .y = 0 };
Coordinates min = { .x = 1000, .y = 1000 };
while(fscanf(input, "%d, %d\n", &(coord->x), &(coord->y)) == 2) {
if (coord->x > max.x) max.x = coord->x;
if (coord->y > max.y) max.y = coord->y;
if (coord->x < min.x) min.x = coord->x;
if (coord->y < min.y) min.y = coord->y;
// printf("%d, %d\n", coord->x, coord->y);
coord++;
}
int width = max.x - min.x + 1;
int height = max.y - min.y + 1;
// Time to malloc ha haaaaaaa
char *grid = malloc(width * height * sizeof(char));
Coordinates curr = {0};
for(int i = 0; i < width * height; i++) {
curr.x = i % width + min.x;
curr.y = i / height + min.y;
int min_dist = width + height;
char min_coord_index = 0;
for(int j = 0; j < 50; j++) {
int hat = hat_dist(curr, coord_list[j]);
if (hat == 0) {
min_dist = hat;
min_coord_index = -2;
break;
} else if (hat < min_dist){
min_dist = hat;
min_coord_index = j;
} else if (hat == min_dist) {
min_coord_index = -1;
}
}
grid[i] = min_coord_index;
if(curr.x == min.x) puts("");
printf("%c", min_coord_index + 'A');
}
puts("");
free(grid);
return -1;
}
int part_2(FILE *input) {
rewind(input);
return -1;
}
int main() {
char *input_file_path = "../data/2018/6/input.txt";
FILE *input = fopen(input_file_path, "r");
if (input == NULL) {
printf("Can't open file %s\n", input_file_path);
return 1;
}
printf("Part 1: %d\n", part_1(input));
printf("Part 2: %d\n", part_2(input));
fclose(input);
}