You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
252 lines
5.4 KiB
C
252 lines
5.4 KiB
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <stdint.h>
|
|
#include <inttypes.h>
|
|
#include <string.h>
|
|
|
|
/* #define DEBUG */
|
|
|
|
#define INPUT_FILE ("../input.txt")
|
|
#define NUM_EDGES (500)
|
|
#define MAX_DIM (1000)
|
|
|
|
/* #define INPUT_FILE ("../input_test.txt") */
|
|
/* #define NUM_EDGES (10) */
|
|
/* #define MAX_DIM (10) */
|
|
|
|
typedef struct vertex_t
|
|
{
|
|
int x;
|
|
int y;
|
|
}vertex_t;
|
|
|
|
typedef struct edge_t
|
|
{
|
|
vertex_t a;
|
|
vertex_t b;
|
|
}edge_t;
|
|
|
|
// takes an array of edges and the length
|
|
// fills a grid according to part 1 rules
|
|
// computes overlapping points and returns this
|
|
int d5_part1(edge_t* edges, int len);
|
|
|
|
// takes an array of edges and the length
|
|
// fills a grid according to part 1 rules
|
|
// computes overlapping points and returns this
|
|
int d5_part2(edge_t* edges, int len);
|
|
|
|
int calc_overlapping_points(int _grid[MAX_DIM][MAX_DIM], size_t dimension);
|
|
void print_grid(int _grid[MAX_DIM][MAX_DIM], size_t dimension);
|
|
|
|
int main(void)
|
|
{
|
|
edge_t edges[NUM_EDGES];
|
|
memset(edges, 0, sizeof(edge_t) * NUM_EDGES);
|
|
|
|
FILE* fp = fopen(INPUT_FILE, "r");
|
|
if(fp == NULL)
|
|
{
|
|
printf("open file error...\n");
|
|
exit(-1);
|
|
}
|
|
|
|
int edge_ct = 0;
|
|
|
|
size_t len = 0;
|
|
ssize_t rc = 0;
|
|
char* line = NULL;
|
|
|
|
while((rc = getline(&line, &len, fp)) != -1)
|
|
{
|
|
if (edge_ct >= NUM_EDGES)
|
|
{
|
|
printf("edges[] out of bounds -- edge count: %d\n", edge_ct);
|
|
}
|
|
sscanf(line,
|
|
"%d,%d -> %d,%d",
|
|
&edges[edge_ct].a.x, &edges[edge_ct].a.y,
|
|
&edges[edge_ct].b.x, &edges[edge_ct].b.y);
|
|
#ifdef DEBUG
|
|
printf("%d,%d -> %d,%d\n",
|
|
edges[edge_ct].a.x, edges[edge_ct].a.y,
|
|
edges[edge_ct].b.x, edges[edge_ct].b.y);
|
|
#endif
|
|
edge_ct++;
|
|
}
|
|
printf("Overlapping points (part 1): %d\n", d5_part1(edges, NUM_EDGES));
|
|
printf("Overlapping points (part 2): %d\n", d5_part2(edges, NUM_EDGES));
|
|
return 0;
|
|
}
|
|
|
|
// takes an array of edges and the length
|
|
// fills a grid according to part 1 rules
|
|
// computes overlapping points and returns this
|
|
int d5_part1(edge_t* edges, int len)
|
|
{
|
|
int grid[MAX_DIM][MAX_DIM];
|
|
memset(grid, 0, MAX_DIM * MAX_DIM * sizeof(int));
|
|
|
|
for(int edge = 0; edge < len; edge++)
|
|
{
|
|
if(edges[edge].b.x == edges[edge].a.x)
|
|
{
|
|
// from y2 to y1
|
|
if(edges[edge].a.y > edges[edge].b.y)
|
|
{
|
|
for(int ind = edges[edge].b.y; ind <= edges[edge].a.y; ind++)
|
|
{
|
|
grid[edges[edge].a.x][ind]++;
|
|
}
|
|
}
|
|
else // from y1 to y2
|
|
{
|
|
for(int ind = edges[edge].a.y; ind <= edges[edge].b.y; ind++)
|
|
{
|
|
grid[edges[edge].a.x][ind]++;
|
|
}
|
|
}
|
|
}
|
|
else if(edges[edge].b.y == edges[edge].a.y)
|
|
{
|
|
if(edges[edge].a.x > edges[edge].b.x)
|
|
{
|
|
//
|
|
for(int ind = edges[edge].b.x; ind <= edges[edge].a.x; ind++)
|
|
{
|
|
grid[ind][edges[edge].a.y]++;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
for(int ind = edges[edge].a.x; ind <= edges[edge].b.x; ind++)
|
|
{
|
|
grid[ind][edges[edge].a.y]++;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
#ifdef DEBUG
|
|
printf("Part 1 grid:\n");
|
|
print_grid(grid, MAX_DIM);
|
|
#endif
|
|
return calc_overlapping_points(grid, MAX_DIM);
|
|
}
|
|
// takes an array of edges and the length
|
|
// fills a grid according to part 1 rules
|
|
// computes overlapping points and returns this
|
|
int d5_part2(edge_t* edges, int len)
|
|
{
|
|
int grid[MAX_DIM][MAX_DIM];
|
|
memset(grid, 0, MAX_DIM * MAX_DIM * sizeof(int));
|
|
for(int edge = 0; edge < len; edge++)
|
|
{
|
|
// now we have to account for horizontal, vertical, and diagonal lines, or lines with a 45 degree angle
|
|
// this just means the slope must be 1. so y2-y1/x2-x1 needs to be 1
|
|
if(edges[edge].b.x == edges[edge].a.x)
|
|
{
|
|
// from y2 to y1
|
|
if(edges[edge].a.y > edges[edge].b.y)
|
|
{
|
|
for(int ind = edges[edge].b.y; ind <= edges[edge].a.y; ind++)
|
|
{
|
|
grid[edges[edge].a.x][ind]++;
|
|
}
|
|
}
|
|
else // from y1 to y2
|
|
{
|
|
for(int ind = edges[edge].a.y; ind <= edges[edge].b.y; ind++)
|
|
{
|
|
grid[edges[edge].a.x][ind]++;
|
|
}
|
|
}
|
|
}
|
|
else if(edges[edge].b.y == edges[edge].a.y)
|
|
{
|
|
if(edges[edge].a.x > edges[edge].b.x)
|
|
{
|
|
//
|
|
for(int ind = edges[edge].b.x; ind <= edges[edge].a.x; ind++)
|
|
{
|
|
grid[ind][edges[edge].a.y]++;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
for(int ind = edges[edge].a.x; ind <= edges[edge].b.x; ind++)
|
|
{
|
|
grid[ind][edges[edge].a.y]++;
|
|
}
|
|
}
|
|
}
|
|
else if(abs(edges[edge].b.y - edges[edge].a.y) ==
|
|
abs(edges[edge].b.x - edges[edge].a.x))
|
|
{
|
|
int inc_x = (edges[edge].b.x - edges[edge].a.x) /
|
|
abs(edges[edge].b.x - edges[edge].a.x);
|
|
int inc_y = (edges[edge].b.y - edges[edge].a.y) /
|
|
abs(edges[edge].b.y - abs(edges[edge].a.y));
|
|
int distance = abs(edges[edge].b.x - edges[edge].a.x);
|
|
vertex_t start =
|
|
{
|
|
.x = edges[edge].a.x,
|
|
.y = edges[edge].a.y
|
|
};
|
|
for(int ind = 0; ind <= distance; ind++)
|
|
{
|
|
grid[start.x][start.y]++;
|
|
start.x += inc_x;
|
|
start.y += inc_y;
|
|
}
|
|
|
|
}
|
|
}
|
|
#ifdef DEBUG
|
|
print_grid(grid, MAX_DIM);
|
|
#endif
|
|
return calc_overlapping_points(grid, MAX_DIM);
|
|
}
|
|
|
|
int calc_overlapping_points(int _grid[MAX_DIM][MAX_DIM], size_t dimension)
|
|
{
|
|
int ret = 0;
|
|
for(int i = 0; i < (int)dimension; i++)
|
|
{
|
|
for(int j = 0; j < (int)dimension; j++)
|
|
{
|
|
if(_grid[i][j] > 1)
|
|
{
|
|
ret++;
|
|
}
|
|
}
|
|
}
|
|
return ret;
|
|
}
|
|
|
|
void print_grid(int _grid[MAX_DIM][MAX_DIM], size_t dimension)
|
|
{
|
|
printf(" |");
|
|
for(int i = 0; i < (int)dimension; i++)
|
|
{
|
|
printf("%2d ", i);
|
|
}
|
|
printf("\n");
|
|
for(int i = 0; i < (int)dimension; i++)
|
|
{
|
|
printf("%d | ", i);
|
|
for(int j = 0; j < (int)dimension; j++)
|
|
{
|
|
if(_grid[j][i] < 1)
|
|
{
|
|
printf("%-2s ", ".");
|
|
}
|
|
else
|
|
{
|
|
printf("%-2d ", _grid[j][i]);
|
|
}
|
|
}
|
|
printf("\n");
|
|
}
|
|
}
|