This commit is contained in:
2025-12-11 10:42:35 +01:00
parent cf51ef5a36
commit 31704f5d2a
6 changed files with 157 additions and 0 deletions

1
2nd_day/1/input.txt Normal file
View File

@@ -0,0 +1 @@
5542145-5582046,243-401,884211-917063,1174-1665,767028-791710,308275-370459,285243789-285316649,3303028-3361832,793080-871112,82187-123398,7788-14096,21-34,33187450-33443224,2750031-2956556,19974-42168,37655953-37738891,1759-2640,55544-75026,9938140738-9938223673,965895186-966026269,502675-625082,11041548-11204207,1-20,3679-7591,8642243-8776142,40-88,2872703083-2872760877,532-998,211488-230593,3088932-3236371,442734-459620,8484829519-8484873271,5859767462-5859911897,9987328-10008767,656641-673714,262248430-262271846

80
2nd_day/1/main.cpp Normal file
View File

@@ -0,0 +1,80 @@
#include <fstream>
#include <iostream>
#include <numeric>
#include <set>
#include <string>
#include <vector>
using namespace std;
vector<string> get_input_from_file(const string file_name) {
ifstream file(file_name);
string input;
getline(file, input);
input += ",";
size_t pos;
vector<string> ids;
while ((pos = input.find(",")) != string::npos) {
if (pos == 0) {
input.erase(0, 1);
continue;
}
string id = input.substr(0, pos);
ids.push_back(id);
input.erase(0, pos + 1);
}
file.close();
return ids;
}
bool is_id_valid(string id) {
int len = id.length();
if (len % 2 != 0) {
return true;
}
string first_half = id.substr(0, len / 2);
string second_half = id.substr(len / 2);
if (first_half == second_half) {
cout << "Id " << id << " is not valid!" << endl;
return false;
}
return true;
}
set<string> get_invalid_ids(const string range) {
const size_t delimeter_pos = range.find("-");
const int length = range.length();
const long long start = stoll(range.substr(0, delimeter_pos));
const long long end = stoll(range.substr(delimeter_pos + 1));
set<string> invalid_ids;
for (long long id = start; id <= end; id++) {
if (!is_id_valid(to_string(id))) {
invalid_ids.insert(to_string(id));
}
}
return invalid_ids;
}
int main() {
const string FILE_NAME = "input.txt";
vector<string> ids = get_input_from_file(FILE_NAME);
set<string> all_invalid_ids;
for (string id : ids) {
set<string> invalid = get_invalid_ids(id);
all_invalid_ids.insert(invalid.begin(), invalid.end());
}
cout << "Ammount of invalid ids: "
<< accumulate(all_invalid_ids.begin(), all_invalid_ids.end(), 0) << endl;
}

1
2nd_day/1/test.txt Normal file
View File

@@ -0,0 +1 @@
11-22,95-115,998-1012,1188511880-1188511890,222220-222224,1698522-1698528,446443-446449,38593856-38593862,565653-565659,824824821-824824827,2121212118-2121212124

1
2nd_day/2/input.txt Normal file
View File

@@ -0,0 +1 @@
5542145-5582046,243-401,884211-917063,1174-1665,767028-791710,308275-370459,285243789-285316649,3303028-3361832,793080-871112,82187-123398,7788-14096,21-34,33187450-33443224,2750031-2956556,19974-42168,37655953-37738891,1759-2640,55544-75026,9938140738-9938223673,965895186-966026269,502675-625082,11041548-11204207,1-20,3679-7591,8642243-8776142,40-88,2872703083-2872760877,532-998,211488-230593,3088932-3236371,442734-459620,8484829519-8484873271,5859767462-5859911897,9987328-10008767,656641-673714,262248430-262271846

73
2nd_day/2/main.cpp Normal file
View File

@@ -0,0 +1,73 @@
#include <fstream>
#include <iostream>
#include <set>
#include <string>
#include <vector>
using namespace std;
vector<string> get_input_from_file(const string file_name) {
ifstream file(file_name);
string input;
getline(file, input);
input += ",";
size_t pos;
vector<string> ids;
while ((pos = input.find(",")) != string::npos) {
if (pos == 0) {
input.erase(0, 1);
continue;
}
string id = input.substr(0, pos);
ids.push_back(id);
input.erase(0, pos + 1);
}
file.close();
return ids;
}
bool is_id_periodic(string id) {
if (id.size() < 2)
return false;
return (id + id).find(id, 1) < id.size();
}
vector<string> get_invalid_ids(const string range) {
const size_t delimeter_pos = range.find("-");
const int length = range.length();
const long long start = stoll(range.substr(0, delimeter_pos));
const long long end = stoll(range.substr(delimeter_pos + 1));
vector<string> periodic_ids;
for (long long id = start; id <= end; id++) {
if (is_id_periodic(to_string(id))) {
periodic_ids.push_back(to_string(id));
}
}
return periodic_ids;
}
int main() {
const string FILE_NAME = "input.txt";
vector<string> ids = get_input_from_file(FILE_NAME);
set<string> all_periodc_ids;
for (string id : ids) {
vector<string> invalid = get_invalid_ids(id);
all_periodc_ids.insert(invalid.begin(), invalid.end());
}
long long sum = 0;
for (string id : all_periodc_ids) {
sum += stoll(id);
}
cout << "Sum of invalid ids: " << sum << endl;
}

1
2nd_day/2/test.txt Normal file
View File

@@ -0,0 +1 @@
11-22,95-115,998-1012,1188511880-1188511890,222220-222224,1698522-1698528,446443-446449,38593856-38593862,565653-565659,824824821-824824827,2121212118-2121212124