-
Notifications
You must be signed in to change notification settings - Fork 0
/
4.cpp
46 lines (42 loc) · 1.19 KB
/
4.cpp
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
#include "lib/global.hpp"
#include "lib/os_linux.cpp"
#include "lib/format.cpp"
#include "lib/hashmap.cpp"
#include "lib/number.cpp"
#include "lib/algorithm.cpp"
int main() {
auto arr = array_load_from_file("input/4"_arr);
s64 number = 0;
s64 sum = 0;
Array_dyn<u8> winning;
s64 state = 0;
s64 winning_count = 0;
for (s64 i = 0; i < arr.size; ++i) {
u8 c = arr[i];
if ('0' <= c and c <= '9') {
number = 10*number + (c - '0');
} else if ((c == ' ' or c == '\n') and number) {
if (state == 0) {
array_push_back(&winning, number);
} else {
bool is_winning = false;
for (u8 j: winning) is_winning |= j == number;
winning_count += is_winning;
}
number = 0;
} else if (c == '|') {
state = 1;
} else if (c == ':') {
number = 0;
}
if (c == '\n') {
if (winning_count > 0) {
sum += 1 << (winning_count - 1);
}
winning.size = 0;
state = 0;
winning_count = 0;
}
}
format_print("%d\n", sum);
}