-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday06b.c
49 lines (43 loc) · 1.27 KB
/
day06b.c
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
#include "common.h"
#include "stringview.h"
size_t count_yes_answers (int * a, int n_group_members) {
size_t count = 0;
for (size_t i = 0; i < 26; ++i) {
if (a[i] == n_group_members) {
++count;
}
}
return count;
}
int main (int argc, char ** argv) {
size_t total = 0;
int n_group_members = 0;
int responses [26];
memset(responses, 0, sizeof(responses));
do {
char buf [1024];
char * s = fgets(buf, sizeof(buf), stdin);
if (s == NULL) {
total += count_yes_answers(responses, n_group_members);
n_group_members = 0;
memset(responses, 0, sizeof(responses));
break;
}
StringView_t line = sv_view_c_string(s);
sv_eat_spaces(&line);
if (sv_is_empty(&line)) {
total += count_yes_answers(responses, n_group_members);
n_group_members = 0;
memset(responses, 0, sizeof(responses));
} else {
n_group_members += 1;
do {
char c = sv_eat_char(&line);
ASSERT(c >= 'a' && c <= 'z');
responses[c-'a'] += 1;
sv_eat_spaces(&line);
} while (!sv_is_empty(&line));
}
} while (1);
DISP(total);
}