aoc

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

b.c (797B) [raw]


      1 #include <stdio.h>
      2 #include <stdlib.h>
      3 
      4 int grid[1000][1000] = {0};
      5 
      6 void draw(int x1, int y1, int x2, int y2) {
      7     int r = x1;
      8     int c = y1;
      9     int rstep = x1 == x2 ? 0 : (x1 < x2 ? 1 : -1);
     10     int cstep = y1 == y2 ? 0 : (y1 < y2 ? 1 : -1);
     11     while (r != x2 || c != y2) {
     12         grid[r][c]++;
     13         r += rstep;
     14         c += cstep;
     15     }
     16     /* loop stops before marking last point! */
     17     grid[x2][y2]++;
     18 }
     19 
     20 int main(void) {
     21     int x1, y1, x2, y2;
     22     while (scanf("%d,%d -> %d,%d\n", &x1, &y1, &x2, &y2) != EOF) {
     23         draw(x1, y1, x2, y2);
     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 }