diff options
-rw-r--r-- | src/2015/day20/aoc.cpp | 71 | ||||
-rw-r--r-- | src/2015/day20/aoc.h | 1 | ||||
-rw-r--r-- | test/test_2015.cpp | 6 |
3 files changed, 77 insertions, 1 deletions
diff --git a/src/2015/day20/aoc.cpp b/src/2015/day20/aoc.cpp index 1c33c66..2dcceb0 100644 --- a/src/2015/day20/aoc.cpp +++ b/src/2015/day20/aoc.cpp @@ -1,5 +1,76 @@ #include "aoc.h" +#include <algorithm> +#include <set> +#include <vector> namespace aoc2015 { +std::pair<int, int> by2(int x) { + int a = 0; + while (true) { + int total = 0; + for (int i = 0; i <= a; i++) { + total += 1 << i; + } + // printf("%d -> %d\n", a, total * 10); + if (total * 10 > x) { + return {1 << (a - 1), 1 << a}; + } + a++; + } } + +std::vector<int> primefactor(int x) { + std::vector<int> v; + while (x % 2 == 0) { + v.push_back(2); + x /= 2; + } + for (int i = 3; i * i <= x; i += 2) { + while (x % i == 0) { + v.push_back(i); + x /= i; + } + } + if (x > 2) { + v.push_back(x); + } + + return v; +} + +// backtrace +void combo(size_t m, size_t x, std::vector<int>& is, std::set<int>& rs, const std::vector<int>& ps) { + if (is.size() >= m) { + int x = 1; + std::for_each(is.begin(), is.end(), [&x, &ps](int i) { x *= ps[i]; }); + rs.insert(x); + } else { + for (size_t i = x; i < ps.size(); i++) { + is.push_back(i); + combo(m, i + 1, is, rs, ps); + is.pop_back(); + } + } +} + +int combo(int i) { + std::vector<int> ps = primefactor(i); + std::vector<int> is; + std::set<int> rs; + rs.insert(1); + rs.insert(i); + for (size_t i = 1; i < ps.size(); i++) { + combo(i, 0, is, rs, ps); + } + + int total{0}; + std::for_each(rs.begin(), rs.end(), [&total](int i) { total += i * 10; }); + return total; +} + +std::pair<int, int> day20(int x) { + return {combo(x), 0}; +} + +} // namespace aoc2015 diff --git a/src/2015/day20/aoc.h b/src/2015/day20/aoc.h index c439c03..80c8da6 100644 --- a/src/2015/day20/aoc.h +++ b/src/2015/day20/aoc.h @@ -3,4 +3,5 @@ namespace aoc2015 { +std::pair<int, int> day20(int); } diff --git a/test/test_2015.cpp b/test/test_2015.cpp index 9529c49..5fc6bc9 100644 --- a/test/test_2015.cpp +++ b/test/test_2015.cpp @@ -209,4 +209,8 @@ TEST_CASE("Medicine for Rudolph", "[day19]") { REQUIRE(195 == p.second); } -TEST_CASE("Infinite Elves and Infinite Houses", "[day20]") {} +TEST_CASE("Infinite Elves and Infinite Houses", "[day20]") { + auto p = aoc2015::day20(4); + // auto p = aoc2015::day20(36000000); + printf("%d %d\n", p.first, p.second); +} |