summary refs log tree commit diff homepage
path: root/2020/day18.c
blob: 337f107b08c1e6e822e7d7de9407ed5d980b7af0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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);
}