-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path02.c
49 lines (39 loc) · 967 Bytes
/
02.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 <assert.h>
#include <stdio.h>
#include "inputs/02.h"
int day2(void) {
int count_valid = 0;
const unsigned char *s = input;
while (*s != '\0') {
// read lower limit
int lower_limit = 0;
while (*s >= '0' && *s <= '9') {
lower_limit = (lower_limit * 10) + (*s - '0');
s++;
}
// '-'
s++;
// read higher limit
int higher_limit = 0;
while (*s >= '0' && *s <= '9') {
higher_limit = (higher_limit * 10) + (*s - '0');
s++;
}
// ' '
s++;
// read char requirement
char c = *s++;
// ": "
s += 2;
// read until "\n", check if c is between low and higher limit
if ((s[lower_limit - 1] == c && s[higher_limit - 1] != c) ||
(s[higher_limit - 1] == c && s[lower_limit - 1] != c)) {
count_valid++;
}
while (*s != '\n' && *s != '\0') s++;
if (*s == '\n') s++;
}
printf("%d\n", count_valid);
assert(count_valid == 342);
return 0;
}