summary refs log tree commit diff homepage
path: root/2018
diff options
context:
space:
mode:
Diffstat (limited to '2018')
-rw-r--r--2018/day08.c26
1 files changed, 21 insertions, 5 deletions
diff --git a/2018/day08.c b/2018/day08.c
index a240786..7357ed8 100644
--- a/2018/day08.c
+++ b/2018/day08.c
@@ -5,24 +5,40 @@ typedef unsigned uint;
 
 static uint sum;
 
-static void meta(void) {
+static uint meta(void) {
 	uint meta;
 	scanf("%u", &meta);
 	sum += meta;
+	return meta;
 }
 
-static void node(void) {
+static uint node(void) {
 	uint nchild, nmeta;
 	scanf("%u %u", &nchild, &nmeta);
+	uint children[nchild];
+	uint metas[nmeta];
 	for (uint i = 0; i < nchild; ++i) {
-		node();
+		children[i] = node();
 	}
 	for (uint i = 0; i < nmeta; ++i) {
-		meta();
+		metas[i] = meta();
 	}
+	uint val = 0;
+	if (nchild) {
+		for (uint i = 0; i < nmeta; ++i) {
+			if (!metas[i] || metas[i] > nchild) continue;
+			val += children[metas[i] - 1];
+		}
+	} else {
+		for (uint i = 0; i < nmeta; ++i) {
+			val += metas[i];
+		}
+	}
+	return val;
 }
 
 int main() {
-	node();
+	uint val = node();
 	printf("%u\n", sum);
+	printf("%u\n", val);
 }
b98cfeb7ed94af6238be5af8c9d&follow=1'>Update to man-pages 5.09June McEnroe 2020-12-14Update to FreeBSD 12.2June McEnroe 2020-06-08Update to OpenBSD 6.7June McEnroe 2020-05-04Add hack for macOS to search extra man sectionsJune McEnroe 2020-05-04Don't clear MANSECTJune McEnroe