diff --git a/build b/build new file mode 100755 index 0000000..e5527e3 --- /dev/null +++ b/build @@ -0,0 +1,4 @@ +#!/bin/bash + +set -e +g++ src/day$1.cpp -std=c++23 -g3 diff --git a/dbg b/dbg new file mode 100755 index 0000000..b390c82 --- /dev/null +++ b/dbg @@ -0,0 +1,5 @@ +#!/bin/bash + +set -e +./build $1 +gdb a.out diff --git a/run b/run index d963807..07171d4 100755 --- a/run +++ b/run @@ -2,5 +2,5 @@ set -e -g++ src/day$1.cpp -std=c++23 +./build $1 ./a.out diff --git a/src/day2.cpp b/src/day2.cpp new file mode 100644 index 0000000..7f936a8 --- /dev/null +++ b/src/day2.cpp @@ -0,0 +1,85 @@ +// :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; +}