From 6e09bb9bc428102d944e670aacb8eaa9b3ff53c4 Mon Sep 17 00:00:00 2001 From: Ian C Date: Sat, 25 Dec 2021 20:24:31 +0000 Subject: Added data for day 13. Skeleton for day 12 but not sure how to approach it. --- 12.c | 84 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 12.c (limited to '12.c') diff --git a/12.c b/12.c new file mode 100644 index 0000000..238b418 --- /dev/null +++ b/12.c @@ -0,0 +1,84 @@ +#include +#include +#include + +#define MAX_CONNECTIONS 50 +#define MAX_NODES 50 + +typedef struct node +{ + char name[32]; + int links; + struct node *link[MAX_CONNECTIONS]; +} Node; + +static Node node[MAX_NODES] = {0}; +static int nodes = 0; +static Node *start = NULL; +static Node *end = NULL; + +static char *ReadLine(char *p, size_t size, FILE *fp) +{ + if ((p=fgets(p, size, fp))) + { + size_t l = strlen(p); + + while(l && p[l-1] == '\n') + { + p[--l] = 0; + } + } + + return p; +} + +static Node *GetOrAddNode(const char *name) +{ + int f; + + for(f = 0; f < nodes; f++) + { + if (strcmp(name, node[f].name) == 0) + { + return node + f; + } + } + + if (f == MAX_NODES) + { + fprintf(stderr, "Too many nodes\n"); + exit(1); + } + + strcpy(node[f].name, name); + + return node + f; +} + +static void Link(Node *node1, Node *node2) +{ + node1->link[node1->links++] = node2; + node2->link[node2->links++] = node1; +} + +int main(void) +{ + char buff[4096]; + + start = GetOrAddNode("start"); + end = GetOrAddNode("end"); + + while(ReadLine(buff, sizeof buff, stdin)) + { + Node *node1, *node2; + char *p; + + p = strtok(buff, "-"); + node1 = GetOrAddNode(p); + p = strtok(NULL, "-"); + node2 = GetOrAddNode(p); + Link(node1, node2); + } + + return 0; +} -- cgit v1.3