fix 12b
authorMart Lubbers <mart@martlubbers.net>
Mon, 13 Dec 2021 07:19:19 +0000 (08:19 +0100)
committerMart Lubbers <mart@martlubbers.net>
Mon, 13 Dec 2021 07:19:19 +0000 (08:19 +0100)
12b.c
Makefile

diff --git a/12b.c b/12b.c
index 5be7962..38aaf41 100644 (file)
--- a/12b.c
+++ b/12b.c
@@ -51,10 +51,15 @@ char **copy_visited(char **visited, int nvisited)
        return r;
 }
 
-int find_end(struct node *node, char **visited, int nvisited, bool twice)
+int find_end(struct node *node, char **visited, int nvisited, bool twice, char **path, int npath)
 {
-       if (strcmp(node->key, "end") == 0)
+       if (strcmp(node->key, "end") == 0) {
+               printf("path: ");
+               for (int i = 0; i<npath; i++)
+                       printf("%s,", path[i]);
+               printf("\n");
                return 1;
+       }
        if (islower(node->key[0])) {
                for (int i = 0; i<nvisited; i++)
                        if (strcmp(node->key, visited[i]) == 0)
@@ -62,11 +67,13 @@ int find_end(struct node *node, char **visited, int nvisited, bool twice)
                visited = realloc(visited, sizeof(char *)*(nvisited+1));
                visited[nvisited++] = node->key;
        }
+       path = realloc(path, sizeof(char *)*(npath+1));
+       path[npath++] = node->key;
        int r = 0;
        for (struct conn *c = node->connections; c != NULL; c = c->hh.next) {
                if (twice && strcmp(node->key, "start") != 0 && islower(node->key[0]))
-                       r += find_end(c->node, copy_visited(visited, nvisited-1), nvisited-1, false);
-               r += find_end(c->node, copy_visited(visited, nvisited), nvisited, twice);
+                       r += find_end(c->node, copy_visited(visited, nvisited-1), nvisited-1, false, copy_visited(path, npath), npath);
+               r += find_end(c->node, copy_visited(visited, nvisited), nvisited, twice, copy_visited(path, npath), npath);
        }
        free(visited);
        return r;
@@ -86,5 +93,6 @@ int main()
                               get_or_create(&graph, toname));
        }
 
-       printf("%d\n", find_end(get_or_create(&graph, "start"), NULL, 0, true));
+       find_end(get_or_create(&graph, "start"), NULL, 0, true, NULL, 0);
+//     printf("%d\n", find_end(get_or_create(&graph, "start"), NULL, 0, true, NULL, 0));
 }
index e656642..50c1a9f 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -13,6 +13,9 @@ clean:
 
 run: $(addprefix run_,$(BINARIES))
 
+run_12b: 12b
+       ./12b < 12.txt | sort -u | wc -l
+
 run_%a: %a
        ./$< < $*.txt