aoc

Advent of Code Solutions
git clone git://git.alexkarle.com.com/aoc
Log | Files | Refs | README | LICENSE

a.c (982B) [raw]


      1 #include <stdio.h>
      2 #include <stdlib.h>
      3 
      4 int main(void) {
      5     int grid[1000][1000] = {0};
      6 
      7     int x1, y1, x2, y2;
      8     while (scanf("%d,%d -> %d,%d\n", &x1, &y1, &x2, &y2) != EOF) {
      9         /* key: for vert/horiz lines doesn't matter which one you
     10          * start at (since you draw the same line either way */
     11         if (x1 == x2) {
     12             int dist = abs(y1 - y2);
     13             int start = y1 < y2 ? y1 : y2;
     14             for (int i = start; i <= start + dist; i++) {
     15                 grid[x1][i]++;
     16             }
     17         } else if (y1 == y2) {
     18             int dist = abs(x1 - x2);
     19             int start = x1 < x2 ? x1 : x2;
     20             for (int i = start; i <= start + dist; i++) {
     21                 grid[i][y1]++;
     22             }
     23         }
     24     }
     25     
     26     int count = 0;
     27     for (int r = 0; r < 1000; r++) {
     28         for (int c = 0; c < 1000; c++) {
     29             if (grid[r][c] > 1) {
     30                 count++;
     31             }
     32         }
     33     }
     34     printf("%d\n", count);
     35     return 0;
     36 }