do day 3
This commit is contained in:
parent
a1ab4d590f
commit
42feac3994
4
input/2025-3.sample.txt
Normal file
4
input/2025-3.sample.txt
Normal file
@ -0,0 +1,4 @@
|
||||
987654321111111
|
||||
811111111111119
|
||||
234234234234278
|
||||
818181911112111
|
||||
59
src/day3.cpp
Normal file
59
src/day3.cpp
Normal file
@ -0,0 +1,59 @@
|
||||
// :autocmd BufWritePost *.cpp :silent exec "!./build 3"
|
||||
#include <cstdint>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <numeric>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <vector>
|
||||
|
||||
using Data = std::string;
|
||||
|
||||
int64_t part_1(const std::vector<Data> &);
|
||||
int64_t part_2(const std::vector<Data> &);
|
||||
|
||||
int main() {
|
||||
std::ifstream file{"input/2025-3.txt"};
|
||||
Data line;
|
||||
std::vector<Data> v;
|
||||
while (std::getline(file, line)) {
|
||||
v.push_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 auto search{line.substr(0, line.size() - cnt_to_activate + 1)};
|
||||
const char max_char{get_max_char(search)}, null_terminator{'\0'};
|
||||
const auto 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 auto recur{solve(line.substr(max_char_idx + 1), cnt_to_activate - 1)};
|
||||
return std::stoll(std::string(&max_char) + std::to_string(recur));
|
||||
}
|
||||
|
||||
int64_t part_1(const std::vector<Data> &v) {
|
||||
int64_t ans{};
|
||||
for (auto d : v) {
|
||||
ans += solve(d, 2);
|
||||
}
|
||||
return ans;
|
||||
}
|
||||
|
||||
int64_t part_2(const std::vector<Data> &v) {
|
||||
int64_t ans{};
|
||||
for (auto d : v) {
|
||||
ans += solve(d, 12);
|
||||
}
|
||||
return ans;
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user