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
|
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int part_one()
{
FILE *fp = fopen("input", "r");
if (!fp)
exit(EXIT_FAILURE);
int res = 0;
char *line = NULL;
size_t len = 0;
ssize_t read = 0;
while ((read = getline(&line, &len, fp)) != -1) {
(void)read;
int low, high, cnt = 0;
char ch;
char str[256];
sscanf(line, "%d-%d %c: %s\n", &low, &high, &ch, &str[0]);
for (int i = 0; i < strlen(str); i++) {
if (str[i] == ch) {
cnt++;
}
}
if (cnt >= low && cnt <= high)
res++;
}
fclose(fp);
return res;
}
int part_two()
{
FILE *fp = fopen("input", "r");
if (!fp)
exit(EXIT_FAILURE);
int res = 0;
char *line = NULL;
size_t len = 0;
ssize_t read = 0;
while ((read = getline(&line, &len, fp)) != -1) {
(void)read;
int low, high, cnt = 0;
char ch, first, second;
char str[256];
sscanf(line, "%d-%d %c: %s\n", &low, &high, &ch, &str[0]);
first = str[low - 1];
second = str[high - 1];
if (first == ch && second != ch || first != ch && second == ch)
res++;
}
fclose(fp);
return res;
}
int main(int argc, char *argv[])
{
printf("%d\n", part_one());
printf("%d\n", part_two());
return 0;
}
|