From c3406a02a178421218e152ddf0609f50268be23b Mon Sep 17 00:00:00 2001 From: June McEnroe Date: Fri, 6 Dec 2019 05:53:42 -0500 Subject: Solve day 6 part 1 --- 2019/day06.c | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 2019/day06.c 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 +#include +#include + +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); +} -- cgit 1.4.1