aboutsummaryrefslogtreecommitdiff
path: root/2020/18/solve.c
blob: 0fc3eb44c82ec2879f79f028122c3aee73fb5b08 (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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

long add_or_mul(long *res, long num, int mul)
{
	if (mul == 0)
		*res += num;
	else
		*res *= num;
	return *res;
}

long evil(char *str, int prec)
{
	long res = 0;
	int mul = 0, skip = 0, brace_cnt = 0;
	for (char *p = str; *p; p++) {
		if (skip) {
			if (*p == '(') {
				brace_cnt++;
			} else if (*p == ')' && !(brace_cnt--)) {
				brace_cnt = 0;
				skip = 0;
			}
			continue;
		}

		if (*p == ' ') {
			continue;
		} else if (*p == '(') {
			add_or_mul(&res, evil(p + 1, prec), mul);
			skip = 1;
		} else if (*p == ')') {
			break; // That's why I don't use switch..case
		} else if (*p == '*') {
			if (prec)
				return add_or_mul(&res, evil(p + 1, prec), 1);
			mul = 1;
		} else if (*p == '+') {
			mul = 0;
		} else if (isdigit(*p)) {
			add_or_mul(&res, *p - '0', mul);
		}
	}
	return res;
}

long part_one(FILE *fp)
{
	long res = 0;

	char *line = NULL;
	size_t len;
	while (getline(&line, &len, fp) != -1)
		res += evil(line, 0);

	return res;
}

long part_two(FILE *fp)
{
	long res = 0;

	char *line = NULL;
	size_t len;
	while (getline(&line, &len, fp) != -1)
		res += evil(line, 1);

	return res;
}

int main(int argc, char *argv[])
{
	FILE *fp = fopen("input", "r");
	if (!fp)
		exit(EXIT_FAILURE);

	clock_t tic = clock();
	printf("%lu\n", part_one(fp));
	rewind(fp);
	printf("%lu\n", part_two(fp));
	clock_t toc = clock();
	printf("TIME: %f seconds\n", (double)(toc - tic) / CLOCKS_PER_SEC);

	fclose(fp);
	return 0;
}