diff options
-rw-r--r-- | 2020/day18.c | 27 |
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); +} |