finish day 2
This commit is contained in:
54
day2.cpp
54
day2.cpp
@@ -41,7 +41,7 @@ namespace day2 {
|
||||
}
|
||||
}
|
||||
|
||||
Range range;
|
||||
Range range{};
|
||||
uint64_t parsed;
|
||||
std::from_chars(start.data(), start.end(), parsed);
|
||||
range.start = parsed;
|
||||
@@ -50,11 +50,11 @@ namespace day2 {
|
||||
return range;
|
||||
}
|
||||
|
||||
size_t find_divisor(uint64_t n) {
|
||||
size_t find_divisor(const uint64_t n) {
|
||||
for (size_t i = 1;; i++) {
|
||||
size_t div = n / std::pow(10, i);
|
||||
if (div == 0) {
|
||||
return std::pow(10, (i / 2));
|
||||
return std::pow(10, i / 2);
|
||||
}
|
||||
}
|
||||
assert(0 && "Unreachable");
|
||||
@@ -75,9 +75,9 @@ namespace day2 {
|
||||
uint64_t sum_repetitions(const Range range) {
|
||||
uint64_t total = 0;
|
||||
for (uint64_t n = range.start; n <= range.end; n++) {
|
||||
auto div = find_divisor(n);
|
||||
auto left = n / div;
|
||||
auto right = n % div;
|
||||
const auto div = find_divisor(n);
|
||||
const auto left = n / div;
|
||||
const auto right = n % div;
|
||||
if (left == right) total += n;
|
||||
}
|
||||
|
||||
@@ -99,7 +99,26 @@ namespace day2 {
|
||||
uint64_t sum_repetitions_2(const Range range, std::string &buf) {
|
||||
uint64_t total = 0;
|
||||
for (uint64_t n = range.start; n <= range.end; n++) {
|
||||
std::format_to(buf, "{}", n);
|
||||
std::format_to(std::back_inserter(buf), "{}", n);
|
||||
size_t size = buf.size();
|
||||
for (int i = 0; i < size; ++i) {
|
||||
if (size % i != 0 || size / i == 1) continue;
|
||||
std::string_view first_partition(buf.data(), i);
|
||||
bool all_equal = true;
|
||||
for (int j = i; j < size; j += i) {
|
||||
std::string_view partition(buf.data() + j, i);
|
||||
if (partition != first_partition) {
|
||||
all_equal = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (all_equal) {
|
||||
total += n;
|
||||
break;
|
||||
}
|
||||
}
|
||||
buf.clear();
|
||||
}
|
||||
|
||||
return total;
|
||||
@@ -135,7 +154,7 @@ namespace day2 {
|
||||
std::ifstream fs("day2input.txt");
|
||||
std::stringstream ss{};
|
||||
ss << fs.rdbuf();
|
||||
CHECK(solve_first(ss.str()) == 1227775554);
|
||||
CHECK(solve_first(ss.str()) == 21898734247);
|
||||
}
|
||||
|
||||
TEST_CASE("Find divisor works") {
|
||||
@@ -149,4 +168,23 @@ namespace day2 {
|
||||
CHECK(count_repetitions({.start = 1188511880, .end = 1188511890}) == 1);
|
||||
}
|
||||
|
||||
TEST_CASE("sum_repetitions_2") {
|
||||
std::string buf;
|
||||
buf.reserve(100);
|
||||
CHECK(sum_repetitions_2({95, 115}, buf) == 210);
|
||||
buf.clear();
|
||||
CHECK(sum_repetitions_2({998, 1012}, buf) == 999 + 1010);
|
||||
}
|
||||
|
||||
TEST_CASE("Solves second sample") {
|
||||
CHECK(solve_second(sample) == 4174379265);
|
||||
}
|
||||
|
||||
TEST_CASE("Solves second input") {
|
||||
std::ifstream fs("day2input.txt");
|
||||
std::stringstream ss{};
|
||||
ss << fs.rdbuf();
|
||||
CHECK(solve_second(ss.view()) == 28915664389);
|
||||
}
|
||||
|
||||
} // namespace day2
|
||||
|
||||
Reference in New Issue
Block a user