summary refs log tree commit diff homepage
path: root/2020
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--2020/day07.c11
1 files changed, 11 insertions, 0 deletions
diff --git a/2020/day07.c b/2020/day07.c
index 3d97d5f..7445259 100644
--- a/2020/day07.c
+++ b/2020/day07.c
@@ -37,6 +37,16 @@ static bool canContain(struct Bag *bag, const char *adj, const char *col) {
 	}
 	return false;
 }
+static int containsCount(struct Bag *bag) {
+	int count = 0;
+	for (int i = 0; i < 4; ++i) {
+		if (!bag->cons[i].adj[0]) break;
+		count += bag->cons[i].cnt;
+		count += bag->cons[i].cnt
+			* containsCount(findBag(bag->cons[i].adj, bag->cons[i].col));
+	}
+	return count;
+}
 int main(void) {
 	while (EOF != scanf("%s %s bags contain", bags[len].adj, bags[len].col)) {
 		for (
@@ -57,4 +67,5 @@ int main(void) {
 		if (canContain(&bags[i], "shiny", "gold")) count++;
 	}
 	printf("%d\n", count);
+	printf("%d\n", containsCount(findBag("shiny", "gold")));
 }