diff options
author | June McEnroe <june@causal.agency> | 2019-12-06 05:53:42 -0500 |
---|---|---|
committer | June McEnroe <june@causal.agency> | 2019-12-06 05:53:42 -0500 |
commit | 68832a6f2b5ee7725e9cc21c21c57e1f3d151ff6 (patch) | |
tree | 7b16440022ec48aaa021aedb51c13b01fd2e58d8 /2019 | |
parent | Solve day 5 part 2 (diff) | |
download | aoc-68832a6f2b5ee7725e9cc21c21c57e1f3d151ff6.tar.gz aoc-68832a6f2b5ee7725e9cc21c21c57e1f3d151ff6.zip |
Solve day 6 part 1
Diffstat (limited to '')
-rw-r--r-- | 2019/day06.c | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/2019/day06.c b/2019/day06.c new file mode 100644 index 0000000..fbfda57 --- /dev/null +++ b/2019/day06.c @@ -0,0 +1,40 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +static struct Node { + char name[4]; + size_t parent; +} tree[2048]; + +static size_t node(const char *name) { + size_t i; + for (i = 0; tree[i].name[0]; ++i) { + if (strcmp(name, tree[i].name)) continue; + return i; + } + strncpy(tree[i].name, name, sizeof(tree[i].name)); + return i; +} + +static size_t ancestors(size_t i) { + size_t n = 0; + while (i) { + i = tree[i].parent; + n++; + } + return n; +} + +int main(void) { + node("COM"); + char parent[4], child[4]; + while (EOF != scanf("%3s)%3s\n", parent, child)) { + tree[node(child)].parent = node(parent); + } + size_t orbits = 0; + for (size_t i = 0; tree[i].name[0]; ++i) { + orbits += ancestors(i); + } + printf("%zu\n", orbits); +} |