From 85a2a9b23af21f1fedd7060e1ad2817d856d1c61 Mon Sep 17 00:00:00 2001 From: June McEnroe Date: Fri, 18 Dec 2020 00:32:54 -0500 Subject: Solve day 18 part 1 --- 2020/day18.c | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 2020/day18.c (limited to '2020') 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 +#include +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); +} -- cgit 1.4.1