56 lines
1.6 KiB
C++
56 lines
1.6 KiB
C++
// :autocmd BufWritePost *.cpp :silent exec "!script/async-build 3"
|
|
#include <fstream>
|
|
#include <iostream>
|
|
#include <numeric>
|
|
#include <vector>
|
|
|
|
int64_t part_1(const std::vector<std::string> &);
|
|
int64_t part_2(const std::vector<std::string> &);
|
|
|
|
int main() {
|
|
std::ifstream file{"input/2025-3.txt"};
|
|
std::string line;
|
|
std::vector<std::string> v;
|
|
while (std::getline(file, line)) {
|
|
v.emplace_back(line);
|
|
}
|
|
std::cout << "Part 1: " << part_1(v) << std::endl;
|
|
std::cout << "Part 2: " << part_2(v) << std::endl;
|
|
}
|
|
|
|
char get_max_char(std::string_view s) {
|
|
return std::accumulate(s.begin(), s.end(), '0',
|
|
[](const char maximium_char, const char c) {
|
|
return std::max(maximium_char, c);
|
|
});
|
|
}
|
|
|
|
int64_t solve(const std::string_view line, size_t cnt_to_activate) {
|
|
const std::string_view search{line.substr(0, line.size() - cnt_to_activate + 1)};
|
|
const char max_char{get_max_char(search)}, null_terminator{'\0'};
|
|
const size_t max_char_idx{search.find(&max_char, 0)};
|
|
const int64_t max_char_as_int = max_char - '0';
|
|
if (cnt_to_activate == 1) {
|
|
return max_char_as_int;
|
|
}
|
|
const int64_t recur{solve(line.substr(max_char_idx + 1), cnt_to_activate - 1)};
|
|
// slightly hacky... integral concat better?
|
|
return std::stoll(std::string(&max_char) + std::to_string(recur));
|
|
}
|
|
|
|
int64_t part_1(const std::vector<std::string> &v) {
|
|
int64_t ans{};
|
|
for (auto d : v) {
|
|
ans += solve(d, 2);
|
|
}
|
|
return ans;
|
|
}
|
|
|
|
int64_t part_2(const std::vector<std::string> &v) {
|
|
int64_t ans{};
|
|
for (auto d : v) {
|
|
ans += solve(d, 12);
|
|
}
|
|
return ans;
|
|
}
|