solve_h48h3k2.cpp (2523B)
1 /* Simple demo for an H48 solver */ 2 3 #include "../nissy.h" 4 #include <filesystem> 5 #include <fstream> 6 #include <iostream> 7 #include <string> 8 9 int main() { 10 // Get verbose output 11 nissy::set_logger([](const char* s, void *u) { std::cout << s; }, NULL); 12 13 // Get the scramble from the user 14 std::cout << "Enter scramble: "; 15 std::string scramble; 16 std::getline(std::cin, scramble); 17 18 // Apply scramble to a solved cube 19 nissy::cube c; 20 if (!c.move(scramble).ok()) { 21 std::cout << "Invalid scramble!" << std::endl; 22 return 1; 23 } 24 25 // Ask for a limit on the solution's length 26 int maxmoves; 27 std::cout << "Maximum number of moves: "; 28 std::cin >> maxmoves; 29 30 // Load the solver 31 auto h48h3k2 = std::get<nissy::solver>(nissy::solver::get("h48h3k2")); 32 std::filesystem::path tableDir("tables"); 33 std::filesystem::create_directories(tableDir); // Ignored if dir exists 34 std::filesystem::path tableFile("tables/" + h48h3k2.id); 35 36 // If the table is not present, generate it 37 if (!std::filesystem::exists(tableFile)) { 38 std::cout << "Data for h48h3k2 solver was not found, " 39 << "generating it..." << std::endl; 40 auto err = h48h3k2.generate_data(); 41 if (!err.ok()) { 42 std::cout << "Unexpected error! Error code: " 43 << err.value << std::endl; 44 return 1; 45 } 46 std::ofstream ofs(tableFile, std::ios::binary); 47 ofs.write(reinterpret_cast<char *>(h48h3k2.data.data()), 48 h48h3k2.size); 49 std::cout << "Table generated and written to " 50 << tableFile << std::endl; 51 ofs.close(); 52 } 53 // Otherwise read it from file 54 else { 55 std::cout << "Loading data table from file" << std::endl; 56 std::ifstream ifs(tableFile, std::ios::binary); 57 h48h3k2.read_data(ifs); 58 ifs.close(); 59 std::cout << "Data loaded, checking table..." << std::endl; 60 auto err = h48h3k2.check_data(); 61 if (!err.ok()) { 62 std::cout << "Error reading data table from file! " 63 << "Error code: " << err.value << std::endl; 64 return 1; 65 } 66 std::cout << "Data table loaded" << std::endl; 67 } 68 69 // Solve 70 auto solve_result = h48h3k2.solve(c, nissy::nissflag::NORMAL, 71 0, maxmoves, 1, 20, 8); 72 73 // Write the result 74 if (!solve_result.err.ok()) { 75 std::cout << "Error solving! Error code: " << 76 solve_result.err.value << std::endl; 77 return 1; 78 } 79 80 if (solve_result.solutions.size() == 0) { 81 std::cout << "No solution found!" << std::endl; 82 } else { 83 auto& sol = solve_result.solutions[0]; 84 auto len = nissy::count_moves(sol).value; 85 std::cout << "Solution (" << len << " moves): " 86 << sol << std::endl; 87 } 88 89 return 0; 90 }