blob: 81a0ec4e5f1bad5153c35873a9fd709a06b2fda0 (
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
|
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
static 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;
}
static 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[])
{
(void)argc;
(void)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;
}
|