aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarvin Borner2020-12-18 18:20:42 +0100
committerMarvin Borner2020-12-18 18:20:42 +0100
commit3552f1a1e29504ff3f3e633478ef5566d53eb1b8 (patch)
tree44e02d9d84f6c5349ae1358502a30391aa39116e
parent38d65efd2f535e185d0083ff6ee6f2d5d4bbc17f (diff)
ok
-rw-r--r--2020/18/Makefile2
-rw-r--r--2020/18/solve.c20
2 files changed, 12 insertions, 10 deletions
diff --git a/2020/18/Makefile b/2020/18/Makefile
index 9c7ab36..fc2ff1a 100644
--- a/2020/18/Makefile
+++ b/2020/18/Makefile
@@ -1,7 +1,7 @@
.PHONY: solve.c
solve.o: solve.c
- @gcc -g -Ofast $+ -o $@
+ @gcc -Ofast $+ -o $@
clean:
@rm -f *.o
diff --git a/2020/18/solve.c b/2020/18/solve.c
index 97d4ede..ae319fe 100644
--- a/2020/18/solve.c
+++ b/2020/18/solve.c
@@ -13,7 +13,7 @@ long add_or_mul(long *res, int num, int mul)
return *res;
}
-long evil(char *str)
+long evil(char *str, int prec)
{
long res = 0;
int mul = 0, skip = 0, brace_cnt = 0;
@@ -31,7 +31,7 @@ long evil(char *str)
if (*p == ' ') {
continue;
} else if (*p == '(') {
- add_or_mul(&res, evil(p + 1), mul);
+ add_or_mul(&res, evil(p + 1, prec), mul);
skip = 1;
} else if (*p == ')') {
break; // That's why I don't use switch..case
@@ -52,18 +52,20 @@ long part_one(FILE *fp)
char *line = NULL;
size_t len;
- while (getline(&line, &len, fp) != -1) {
- long a = evil(line);
- printf("%lu\n", a);
- res += a;
- }
+ while (getline(&line, &len, fp) != -1)
+ res += evil(line, 0);
return res;
}
-int part_two(FILE *fp)
+long part_two(FILE *fp)
{
- int res = 0;
+ long res = 0;
+
+ char *line = NULL;
+ size_t len;
+ while (getline(&line, &len, fp) != -1)
+ res += evil(line, 1);
return res;
}