aocc23

Advent of Code 2023
git clone git://www.tkruger.se/aocc23.git
Log | Files | Refs | README

common.c (939B)


      1 #include "common.h"
      2 
      3 void nbrs(coordinate_t *res, const size_t i, const size_t j, const size_t nrows,
      4           const size_t ncols) {
      5   size_t idx = 0;
      6   if (i > 0) {
      7     if (j > 0) {
      8       res[idx].y = i - 1;
      9       res[idx].x = j - 1;
     10       idx++;
     11     }
     12 
     13     res[idx].y = i - 1;
     14     res[idx].x = j;
     15     idx++;
     16 
     17     if (j < ncols - 1) {
     18       res[idx].y = i - 1;
     19       res[idx].x = j + 1;
     20       idx++;
     21     }
     22   }
     23 
     24   if (j > 0) {
     25     if (i < nrows - 1) {
     26       res[idx].y = i + 1;
     27       res[idx].x = j - 1;
     28       idx++;
     29     }
     30 
     31     res[idx].y = i;
     32     res[idx].x = j - 1;
     33     idx++;
     34   }
     35 
     36   if (i < nrows - 1) {
     37     if (j < ncols - 1) {
     38       res[idx].y = i + 1;
     39       res[idx].x = j + 1;
     40       idx++;
     41     }
     42 
     43     res[idx].y = i + 1;
     44     res[idx].x = j;
     45     idx++;
     46   }
     47 
     48   if (j < ncols - 1) {
     49     res[idx].y = i;
     50     res[idx].x = j + 1;
     51     idx++;
     52   }
     53 
     54   while (idx < 9) {
     55     res[idx].x = SIZE_MAX;
     56     res[idx].y = SIZE_MAX;
     57     idx++;
     58   }
     59 }