#include #include struct coord { int x; int y; }; struct rect { int x; int y; int w; int h; }; struct coord *parse_coords(const char *fname, unsigned *len) { FILE *f = fopen(fname, "r"); if (!f) { perror(fname); return NULL; } GArray *array = g_array_new(FALSE, FALSE, sizeof(struct coord)); *len = 0; char *line = NULL; size_t line_len = 0; while (getline(&line, &line_len, f) != -1) { struct coord coord = {0}; if (sscanf(line, "%d, %d", &coord.x, &coord.y) != 2) continue; g_array_append_val(array, coord); (*len)++; } free(line); fclose(f); struct coord *res = (struct coord *)g_array_free(array, FALSE); return res; } struct rect find_limit(struct coord *coords, unsigned len) { struct rect rect = {0}; if (!len) return rect; int xmax = rect.x = coords->x; int ymax = rect.y = coords->y; for (struct coord *coord = coords + 1; coord < coords + len; coord++) { if (coord->x < rect.x) rect.x = coord->x; if (coord->y < rect.y) rect.y = coord->y; if (coord->x > xmax) xmax = coord->x; if (coord->y > ymax) ymax = coord->y; } rect.w = xmax - rect.x + 1; rect.h = ymax - rect.y + 1; return rect; } int main() { unsigned coords_len; struct coord *coords = parse_coords("input", &coords_len); if (!coords) return 1; /* * NOTE: This method I'm using to figure our the biggest non-infinite * influence range for a coordinate is suboptimal and won't work for all inputs. * I could try to figure out how to calcluate it properly or a better algorithm * than checking literally each tile in a defined area, but I'm too lazy. * If it works for my input it's fine ;) */ struct rect limit = find_limit(coords, coords_len); printf("%d,%d\n%d,%d\n", limit.x, limit.y, limit.x + limit.w - 1, limit.y + limit.h - 1); unsigned total = 0; for (int y = limit.y; y < limit.y + limit.h; y++) { for (int x = limit.x; x < limit.x + limit.w; x++) { unsigned dist = 0; for (unsigned i = 0; i < coords_len; i++) { dist += abs(coords[i].x - x) + abs(coords[i].y - y); } if (dist < 10000) total++; } } printf("Area: %u\n", total); g_free(coords); return 0; }