From 5bbd6b916f9e3bb16efc24e71b0b1c7bab986dd6 Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Thu, 19 Jan 2023 14:59:32 +0800 Subject: [PATCH] feat: static resolve function for FastCal --- src/fast_cal/fast_cal.cc | 25 +++++++++++++++++++------ src/fast_cal/fast_cal.h | 12 +++++++----- src/main.cc | 23 ++++++++++++++--------- 3 files changed, 40 insertions(+), 20 deletions(-) diff --git a/src/fast_cal/fast_cal.cc b/src/fast_cal/fast_cal.cc index 42c7b8f..ecec5af 100644 --- a/src/fast_cal/fast_cal.cc +++ b/src/fast_cal/fast_cal.cc @@ -4,17 +4,17 @@ #include "fast_cal.h" #include "raw_code.h" +auto resolved = [](uint64_t code) { + return ((code >> (3 * 0xD)) & 0b111) == B_2x2; // check 2x2 block address +}; + /// klotski resolved -> 2x2 block at address 13 (aka 0xD) RawCode FastCal::solve(RawCode code) { - return FastCal::target(code, [](uint64_t code) { - return ((code >> (3 * 0xD)) & 0b111) == B_2x2; // check 2x2 block address - }); + return FastCal::target(code, resolved); } std::vector FastCal::solve_multi(RawCode code) { - return FastCal::target_multi(code, [](uint64_t code) { - return ((code >> (3 * 0xD)) & 0b111) == B_2x2; // check 2x2 block address - }); + return FastCal::target_multi(code, resolved); } /// backtrack of FastCal tree @@ -45,3 +45,16 @@ std::vector FastCal::backtrack(RawCode code) { std::reverse(path.begin(), path.end()); // reverse path cases return path; } + +std::vector FastCal::resolve(RawCode start) { + return FastCal::search(start, resolved); +} + +std::vector FastCal::search(RawCode start, const FastCal::match_t &match) { + auto fc = FastCal(); + auto result = fc.target(start, match); + if (result == FC_NOT_FOUND) { + return std::vector{}; // target not matched + } + return fc.backtrack(result); // backtrack target path +} diff --git a/src/fast_cal/fast_cal.h b/src/fast_cal/fast_cal.h index 395745b..d5af9c7 100644 --- a/src/fast_cal/fast_cal.h +++ b/src/fast_cal/fast_cal.h @@ -20,19 +20,21 @@ public: std::vector backtrack(RawCode code); /// BFS search functions + // TODO: build function with void return -> build total tree RawCode solve(RawCode code); std::vector furthest(RawCode code); std::vector solve_multi(RawCode code); RawCode target(RawCode code, const match_t &match); std::vector target_multi(RawCode code, const match_t &match); + /// static BFS search functions +// static std::vector get_furthest(RawCode start); - // TODO: static search functions - - // TODO: search / search_multi / resolve / resolve_multi - // TODO: static furthest function - + static std::vector resolve(RawCode start); + static std::vector search(RawCode start, const match_t &match); +// static std::vector> resolve_multi(RawCode start); +// static std::vector> search_multi(RawCode start, const match_t &match); private: struct fast_cal_t { diff --git a/src/main.cc b/src/main.cc index 6eaf102..416f42a 100644 --- a/src/main.cc +++ b/src/main.cc @@ -25,9 +25,9 @@ int main() { // AllCases::build(); - auto f = FastCal(); +// auto f = FastCal(); // auto ret = f.solve((uint64_t)RawCode::from_common_code("1a9bf0c")); - auto ret = f.solve(RawCode::from_common_code("1a9bf0c")); +// auto ret = f.solve(RawCode::from_common_code("1a9bf0c")); // auto ret = f.target(RawCode::from_common_code("1a9bf0c"), [](uint64_t code) { // return code == (uint64_t)RawCode::from_common_code(0xDB23B6C00); @@ -36,15 +36,20 @@ int main() { // auto ret = f.solve(RawCode::from_common_code("1aaef0c")); - if (ret == FC_NOT_FOUND) { - std::cout << "not found" << std::endl; - } else { - for (const auto &r : f.backtrack(ret)) { - std::cout << r << std::endl; - } - std::cout << "step number: " << f.step_num(ret) << std::endl; +// if (ret == FC_NOT_FOUND) { +// std::cout << "not found" << std::endl; +// } else { +// for (const auto &r : f.backtrack(ret)) { +// std::cout << r << std::endl; +// } +// std::cout << "step number: " << f.step_num(ret) << std::endl; +// } + + for (const auto &c : FastCal::resolve(RawCode::from_common_code("1a9bf0c"))) { + std::cout << c << std::endl; } + // std::cout << f.step_num(ret) << std::endl; // auto f = FastCal(RawCode::from_common_code("1a9bf0c").unwrap());