summary refs log tree commit diff homepage
diff options
context:
space:
mode:
authorJune McEnroe <june@causal.agency>2018-12-08 21:51:37 -0500
committerJune McEnroe <june@causal.agency>2018-12-08 21:51:37 -0500
commit5f306248a8aa5adac034e137515ed740d9744fbe (patch)
treea1efabf9ccf11c323b8d8728480f5415a9549162
parentSolve day 8 part 1 (diff)
downloadaoc-5f306248a8aa5adac034e137515ed740d9744fbe.tar.gz
aoc-5f306248a8aa5adac034e137515ed740d9744fbe.zip
Solve day 8 part 2
Diffstat (limited to '')
-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);
 }
lass='tag-annotated-deco' href='/libretls/tag/?h=3.3.3p1'>3.3.3p1June McEnroe 2021-05-25build: Add scripts to EXTRA_DISTJune McEnroe 2021-05-25import: Add missing scripts/wrap-compiler-for-flag-checkJune McEnroe 2021-05-08Import LibreSSL 3.3.3June McEnroe 2021-04-18build: Remove added x509_verify.3 links 3.3.2June McEnroe 2021-04-18tls: Use EC_KEY_set_ex_dataJune McEnroe 2021-04-18Import LibreSSL 3.3.2June McEnroe 2021-03-05Bump version to 3.3.1p1 3.3.1p1June McEnroe 2021-03-05build: Add OpenSSL includes to libcompatJune McEnroe Some compat sources (getentropy_linux.c for example) require OpenSSL. Reported by Robert Scheck. 2020-12-15Import LibreSSL 3.3.1June McEnroe 2020-11-24Import LibreSSL 3.3.0June McEnroe 2020-10-22Import LibreSSL 3.2.2June McEnroe 2020-09-29Import LibreSSL 3.2.1June McEnroe 2020-09-29import: Add m4/ax_add_fortify_source.m4June McEnroe 2020-08-05build: Add README.7 to EXTRA_DIST 3.2.0June McEnroe 2020-08-03doc: Indicate that only OpenSSL 1.1.1b and newer workJune McEnroe