summary refs log tree commit diff homepage
diff options
context:
space:
mode:
authorJune McEnroe <june@causal.agency>2020-12-18 00:32:54 -0500
committerJune McEnroe <june@causal.agency>2020-12-18 00:32:54 -0500
commit85a2a9b23af21f1fedd7060e1ad2817d856d1c61 (patch)
treefc2747c88c29f2a07405aaed130bb49d46ce1185
parentSolve day 17 part 2 (diff)
downloadaoc-85a2a9b23af21f1fedd7060e1ad2817d856d1c61.tar.gz
aoc-85a2a9b23af21f1fedd7060e1ad2817d856d1c61.zip
Solve day 18 part 1
-rw-r--r--2020/day18.c27
1 files changed, 27 insertions, 0 deletions
diff --git a/2020/day18.c b/2020/day18.c
new file mode 100644
index 0000000..337f107
--- /dev/null
+++ b/2020/day18.c
@@ -0,0 +1,27 @@
+#include <stdio.h>
+#include <stdlib.h>
+static long parse(void) {
+	long result = 0;
+	char op = '+';
+	for (char ch; EOF != (ch = getchar());) {
+		long val = 0;
+		switch (ch) {
+			break; case ' ': continue;
+			break; case '(': val = parse();
+			break; case ')': return result;
+			break; case '\n': return result;
+			break; case '0' ... '9': val = ch - '0';
+			break; default: op = ch; continue;
+		}
+		switch (op) {
+			break; case '+': result += val;
+			break; case '*': result *= val;
+		}
+	}
+	return result;
+}
+int main(void) {
+	long sum = 0;
+	for (long line; (line = parse()); sum += line);
+	printf("%ld\n", sum);
+}