do day 2
This commit is contained in:
parent
457f95ae62
commit
ed5a433df3
4
build
Executable file
4
build
Executable file
@ -0,0 +1,4 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
set -e
|
||||||
|
g++ src/day$1.cpp -std=c++23 -g3
|
||||||
2
run
2
run
@ -2,5 +2,5 @@
|
|||||||
|
|
||||||
set -e
|
set -e
|
||||||
|
|
||||||
g++ src/day$1.cpp -std=c++23
|
./build $1
|
||||||
./a.out
|
./a.out
|
||||||
|
|||||||
85
src/day2.cpp
Normal file
85
src/day2.cpp
Normal file
@ -0,0 +1,85 @@
|
|||||||
|
// :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;
|
||||||
|
};
|
||||||
|
|
||||||
|
using std::cout, std::endl, std::vector, std::ifstream;
|
||||||
|
|
||||||
|
int64_t part_1(const vector<Data> &);
|
||||||
|
unsigned long long part_2(const vector<Data> &);
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
ifstream file{"input/2025-2.txt"};
|
||||||
|
Data line;
|
||||||
|
char throwaway;
|
||||||
|
vector<Data> 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<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;
|
||||||
|
}
|
||||||
|
|
||||||
|
unsigned long long part_2(const vector<Data> &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;
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user