mirror of
https://github.com/szymon-jozef/advent_of_code_2025.git
synced 2026-09-07 12:10:41 +02:00
day 5, 1st challange
This commit is contained in:
11
5th_day/1/example.txt
Normal file
11
5th_day/1/example.txt
Normal 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
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
79
5th_day/1/main.cpp
Normal 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;
|
||||
}
|
||||
Reference in New Issue
Block a user