aboutsummaryrefslogtreecommitdiff
path: root/cpp/examples/solve_h48h3k2.cpp
blob: 7f06376536c5a960557298d29fcaa92393ef8823 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/* Simple demo for an H48 solver */

#include "../nissy.h"
#include <filesystem>
#include <fstream>
#include <iostream>
#include <string>

int main() {
	// Get verbose output
	nissy::set_logger([](const char* s, void *u) { std::cout << s; }, NULL);

	// Get the scramble from the user
	std::cout << "Enter scramble: ";
	std::string scramble;
	std::getline(std::cin, scramble);

	// Apply scramble to a solved cube
	nissy::cube c;
	if (!c.move(scramble).ok()) {
		std::cout << "Invalid scramble!" << std::endl;
		return 1;
	}

	// Ask for a limit on the solution's length
	int maxmoves;
	std::cout << "Maximum number of moves: ";
	std::cin >> maxmoves;

	// Load the solver
	auto h48h3k2 = std::get<nissy::solver>(nissy::solver::get("h48h3k2"));
	std::filesystem::path tableDir("tables");
	std::filesystem::create_directories(tableDir); // Ignored if dir exists
	std::filesystem::path tableFile("tables/" + h48h3k2.id);

	// If the table is not present, generate it
	if (!std::filesystem::exists(tableFile)) {
		std::cout << "Data for h48h3k2 solver was not found, "
		    << "generating it..." << std::endl;
		auto err = h48h3k2.generate_data();
		if (!err.ok()) {
			std::cout << "Unexpected error! Error code: "
			    << err.value << std::endl;
			return 1;
		}
		std::ofstream ofs(tableFile, std::ios::binary);
		ofs.write(reinterpret_cast<char *>(h48h3k2.data.data()),
		    h48h3k2.size);
		std::cout << "Table generated and written to "
		    << tableFile << std::endl;
		ofs.close();
	}
	// Otherwise read it from file
	else {
		std::cout << "Loading data table from file" << std::endl;
		std::ifstream ifs(tableFile, std::ios::binary);
		h48h3k2.read_data(ifs);
		ifs.close();
		std::cout << "Data loaded, checking table..." << std::endl;
		auto err = h48h3k2.check_data();
		if (!err.ok()) {
			std::cout << "Error reading data table from file! "
			    << "Error code: " << err.value << std::endl;
			return 1;
		}
		std::cout << "Data table loaded" << std::endl;
	}

	// Solve
	auto solve_result = h48h3k2.solve(c, nissy::nissflag::NORMAL,
	    0, maxmoves, 1, -1, 8);

	// Write the result
	if (!solve_result.err.ok()) {
		std::cout << "Error solving! Error code: " <<
		    solve_result.err.value << std::endl;
		return 1;
	}

	if (solve_result.solutions.size() == 0) {
		std::cout << "No solution found!" << std::endl;
	} else {
		auto& sol = solve_result.solutions[0];
		auto len = nissy::count_moves(sol).value;
		std::cout << "Solution (" << len << " moves): "
		    << sol << std::endl;
	}

	return 0;
}

Generated with cgit - Back to sebastiano.tronto.net