easters

easters.dev solutions (C)
git clone git://bsandro.tech/easters
Log | Files | Refs | LICENSE

friday.c (2199B)


      1 #include <stdio.h>
      2 #include <stdbool.h>
      3 #include "cputime.h"
      4 
      5 #define MAP_W 42
      6 #define MAP_H 29
      7 
      8 typedef struct {
      9     char grid[MAP_H][MAP_W];
     10 } Map;
     11 
     12 typedef struct {
     13     int x;
     14     int y;
     15     char name;
     16     bool processed;
     17 } Extant;
     18 
     19 typedef struct {
     20     Extant items[64];
     21     int len;
     22 } ArExtants;
     23 
     24 struct Node;
     25 typedef struct {
     26     int len;
     27     struct Node *n;
     28 } Connection;
     29 
     30 typedef struct {
     31     char name;
     32     Connection *conns;
     33 } Node;
     34 
     35 typedef struct {
     36     Node items[256];
     37     int len;
     38 } ArNodes;
     39 
     40 inline bool isPlant(char c) {
     41     return (c>='a'&&c<='z')||(c>='A'&&c<='Z');
     42 }
     43 
     44 inline int numConnections(Map *map, int x, int y) {
     45     int n = 0;
     46     if (map->grid[y-1][x]=='|') n++;
     47     if (map->grid[y+1][x]=='|') n++;
     48     if (map->grid[y][x-1]=='-') n++;
     49     if (map->grid[y][x+1]=='-') n++;
     50     return n;
     51 }
     52 
     53 ArExtants findExtants(Map *map) {
     54     ArExtants extants = {0};
     55     for (int y=1;y<MAP_H-1;++y) {
     56         for (int x=1;x<MAP_W-1;++x) {
     57             if (isPlant(map->grid[y][x])) {
     58                 if (numConnections(map, x, y)==1) {
     59                     extants.items[extants.len++] = (Extant){x,y,map->grid[y][x],false};
     60                 }
     61             }
     62         }
     63     }
     64     return extants;
     65 }
     66 
     67 int main(void) {
     68     int prev = 0;
     69     Map map = {0};
     70     int x = 1;
     71     int y = 1;
     72     for (int c=getchar();c!=EOF;c=getchar()) {
     73         if (c=='\n') {
     74             y++;
     75             x=1;
     76         } else {
     77             map.grid[y][x++] = c;
     78         }
     79         if (c=='\n'&&prev=='\n') break; // map end
     80         prev = c;
     81     }
     82 
     83     ArExtants extants = findExtants(&map);
     84 
     85     printf("%d extants:\n", extants.len);
     86     for (int i=0;i<extants.len;++i) {
     87         Extant e = extants.items[i];
     88         printf("%c at %d,%d\n", map.grid[e.y][e.x], e.x, e.y);
     89     }
     90 
     91     ArNodes nodes = {0};
     92     for (int i=0;i<extants.len;++i) {
     93         if (!extants.items[i].processed) {
     94             nodes.items[nodes.len++] = (Node){extants.items[i].name, NULL};
     95             // traverse map, fill nodes with distances
     96             // feedback loop - have to mark extants as processed when we finish traversal
     97             // the direction of movement is important for parsing numbers
     98         }
     99     }
    100 
    101     return 0;
    102 }