day 5, 1st challange

This commit is contained in:
2025-12-16 19:56:20 +01:00
parent 0d70e23588
commit 991f642cbd
3 changed files with 1258 additions and 0 deletions

11
5th_day/1/example.txt Normal file
View File

@@ -0,0 +1,11 @@
3-5
10-14
16-20
12-18
1
5
8
11
17
32

1168
5th_day/1/input.txt Normal file

File diff suppressed because it is too large Load Diff

79
5th_day/1/main.cpp Normal file
View File

@@ -0,0 +1,79 @@
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
struct Range {
long long begining;
long long end;
};
vector<string> read_file(const string &FILE_NAME) {
ifstream file(FILE_NAME);
string s;
vector<string> result;
while (getline(file, s)) {
result.push_back(s);
}
return result;
}
vector<Range> get_fresh_ids(const vector<string> input) {
vector<Range> fresh_ids;
for (string line : input) {
if (line.empty()) {
break;
}
string begining, end;
begining = line.substr(0, line.find("-"));
end = line.substr(line.find("-") + 1);
Range l_range = {stoll(begining), stoll(end)};
fresh_ids.push_back(l_range);
}
return fresh_ids;
}
vector<long long> get_product_ids(const vector<string> input) {
vector<long long> product_ids;
bool end_of_fresh_ids = false;
for (string line : input) {
if (line.empty()) {
end_of_fresh_ids = true;
continue;
}
if (!end_of_fresh_ids) {
continue;
}
product_ids.push_back(stoll(line));
}
return product_ids;
}
int main() {
const string FILE_NAME = "input.txt";
vector<string> input = read_file(FILE_NAME);
vector<Range> fresh_ids = get_fresh_ids(input);
vector<long long> product_ids = get_product_ids(input);
int count = 0;
for (long long number : product_ids) {
for (const Range &range : fresh_ids) {
if (number >= range.begining && number <= range.end) {
count++;
break;
}
}
}
cout << count;
return 0;
}