-
Notifications
You must be signed in to change notification settings - Fork 9
/
day_03a.cpp
51 lines (45 loc) · 1.4 KB
/
day_03a.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
47
48
49
50
51
#include <iostream>
#include <fstream>
#include <string>
#include <ranges>
#include <string_view>
#include <vector>
auto join_character_in_each_subranges = [](auto &&rng) { return std::string_view(&*rng.begin(), std::ranges::distance(rng)); };
int convert_to_int(const std::string_view s) {
int num = 0;
for (const auto c : s) {
num = num * 10 + (c - '0');
}
return num;
}
bool check_triangle(const std::vector<int>& numbers) {
if (numbers[0] + numbers[1] <= numbers[2]) return false;
if (numbers[1] + numbers[2] <= numbers[0]) return false;
if (numbers[2] + numbers[0] <= numbers[1]) return false;
return true;
}
int main(int argc, char* argv[]) {
std::string input = "../input/day_03_input";
if (argc > 1) {
input = argv[1];
}
std::ifstream file(input);
std::string line;
int count = 0;
while(std::getline(file, line)) {
std::vector<int> numbers;
numbers.reserve(3);
for (const auto num : line
| std::ranges::views::split(' ')
| std::ranges::views::transform(join_character_in_each_subranges))
{
if (num.empty()) continue;
numbers.push_back(convert_to_int(num));
std::cout << numbers.back() << ",";
}
std::cout << '\n';
if(check_triangle(numbers)) count++;
}
std::cout << count << '\n';
return 0;
}