// :autocmd BufWritePost *.cpp :silent exec "!./build 2" #include #include #include #include #include struct Data { int64_t min; int64_t max; }; using std::cout, std::endl, std::vector, std::ifstream; int64_t part_1(const vector &); unsigned long long part_2(const vector &); int main() { ifstream file{"input/2025-2.txt"}; Data line; char throwaway; vector v; while (file >> line.min >> throwaway >> line.max) { v.push_back(line); file >> throwaway; } cout << "Part 1: " << part_1(v) << endl; cout << "Part 2: " << part_2(v) << endl; } int64_t part_1(const vector &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; } unsigned long long part_2(const vector &v) { unsigned long long ans{0}; for (auto d : v) { for (auto n{d.min}; n <= d.max; ++n) { ans += solve_n(n); } } // todo: dead code, delete me unsigned long long foobar = std::accumulate(v.begin(), v.end(), 0, [&ans](unsigned long long 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; }