From 0e3a38d2e3705a031b6084229e72cd0fb9632427 Mon Sep 17 00:00:00 2001 From: Marvin Borner Date: Fri, 18 Dec 2020 18:59:14 +0100 Subject: Feeling nostalgic --- 2015/01/solve.c | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 2015/01/solve.c (limited to '2015/01/solve.c') diff --git a/2015/01/solve.c b/2015/01/solve.c new file mode 100644 index 0000000..287bf3f --- /dev/null +++ b/2015/01/solve.c @@ -0,0 +1,55 @@ +#include +#include +#include + +int part_one(FILE *fp) +{ + int res = 0; + + char ch = 0; + while ((ch = fgetc(fp)) != EOF) { + if (ch == '(') + res++; + else if (ch == ')') + res--; + } + + return res; +} + +int part_two(FILE *fp) +{ + int res = 0, floor = 0; + + char ch = 0; + while ((ch = fgetc(fp)) != EOF) { + res++; + + if (ch == '(') + floor++; + else if (ch == ')') + floor--; + + if (floor == -1) + break; + } + + return res; +} + +int main(int argc, char *argv[]) +{ + FILE *fp = fopen("input", "r"); + if (!fp) + exit(EXIT_FAILURE); + + clock_t tic = clock(); + printf("%d\n", part_one(fp)); + rewind(fp); + printf("%d\n", part_two(fp)); + clock_t toc = clock(); + printf("TIME: %f seconds\n", (double)(toc - tic) / CLOCKS_PER_SEC); + + fclose(fp); + return 0; +} -- cgit v1.2.3