84 lines
1.9 KiB
C++
84 lines
1.9 KiB
C++
// :autocmd BufWritePost *.cpp :silent exec "!./build 2"
|
|
#include <fstream>
|
|
#include <iostream>
|
|
#include <numeric>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
struct Data {
|
|
int64_t min;
|
|
int64_t max;
|
|
};
|
|
|
|
int64_t part_1(const std::vector<Data> &);
|
|
int64_t part_2(const std::vector<Data> &);
|
|
|
|
int main() {
|
|
std::ifstream file{"input/2025-2.txt"};
|
|
Data line;
|
|
char throwaway;
|
|
std::vector<Data> v;
|
|
while (file >> line.min >> throwaway >> line.max) {
|
|
v.push_back(line);
|
|
file >> throwaway;
|
|
}
|
|
std::cout << "Part 1: " << part_1(v) << std::endl;
|
|
std::cout << "Part 2: " << part_2(v) << std::endl;
|
|
}
|
|
|
|
int64_t part_1(const std::vector<Data> &v) {
|
|
int64_t answer{0};
|
|
for (auto d : v) {
|
|
for (auto n{d.min}; n <= d.max; ++n) {
|
|
auto temp{std::to_string(n)};
|
|
auto lhs{temp.substr(0, (temp.size() / 2))};
|
|
auto rhs{temp.substr(lhs.size())};
|
|
if (lhs == rhs) {
|
|
answer += std::stoll(lhs + rhs);
|
|
}
|
|
}
|
|
}
|
|
return answer;
|
|
}
|
|
|
|
int64_t solve_n(int64_t n) {
|
|
auto n_str{std::to_string(n)};
|
|
for (size_t i{1}; i <= n_str.size() / 2; ++i) {
|
|
auto lhs{n_str.substr(0, i)};
|
|
if (n_str.size() % lhs.size() == 0) {
|
|
int64_t repeat_cnt = n_str.size() / lhs.size();
|
|
std::string comparator{""};
|
|
for (auto i{0}; i < repeat_cnt; ++i) {
|
|
comparator += lhs;
|
|
}
|
|
if (comparator == n_str) {
|
|
return n;
|
|
}
|
|
}
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
int64_t part_2(const std::vector<Data> &v) {
|
|
int64_t ans{0};
|
|
for (auto d : v) {
|
|
for (auto n{d.min}; n <= d.max; ++n) {
|
|
ans += solve_n(n);
|
|
}
|
|
}
|
|
|
|
// todo: dead code, delete me
|
|
int64_t foobar = std::accumulate(v.begin(), v.end(), 0, [&ans](int64_t acc, Data d) {
|
|
for (auto n{d.min}; n <= d.max; ++n) {
|
|
acc += solve_n(n);
|
|
}
|
|
return acc;
|
|
});
|
|
|
|
// help please :)
|
|
// why ans != foobar? looks like same but not same
|
|
// stepping through with debugger, it even seems the same on last iteration of accumulate
|
|
|
|
return ans;
|
|
}
|