-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathmain.cpp
More file actions
87 lines (76 loc) · 2.18 KB
/
Copy pathmain.cpp
File metadata and controls
87 lines (76 loc) · 2.18 KB
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
#include "solver.hpp"
#include <iostream>
#include <string>
#include <vector>
#include <chrono>
namespace {
/**
* Input patterns:
* [1-9] means that square has a digit assigned,
* . means empty square
* Example:
*
* 123...456
* .........
* .........
* 456...789
* .........
* .........
* 789...123
* .........
* .........
*
*/
board read_board(std::istream& in) {
board parsed(9, std::vector<int>(9));
int lines = 1;
std::string line;
while (std::getline(in, line) && lines <= 9) {
if (line.size() != 9) {
throw std::runtime_error("Line #" + std::to_string(lines) + " has invalid size.");
}
for (size_t ci = 0; ci < line.size(); ++ci) {
char c = line[ci];
if (c == '.') {
continue;
} else if (c >= '0' && c <= '9') {
parsed[lines - 1][ci] = c - '0';
} else {
throw std::runtime_error("Line #" + std::to_string(lines) + "contains invalid character: '" + c + "'");
}
}
++lines;
}
if (lines != 10) {
throw std::runtime_error("The input is missing a line");
}
return parsed;
}
} // end anonymous namespace
int main() {
try {
auto board = read_board(std::cin);
auto t1 = std::chrono::high_resolution_clock::now();
Solver s;
if (!s.apply_board(board)) {
std::clog << "There is a contradiction in the parsed!\n";
return 2;
}
if (s.solve()) {
std::chrono::duration<double, std::milli> time_taken = std::chrono::high_resolution_clock::now() - t1;
std::clog << "Solution found in " << time_taken.count() << " ms\n";
auto solution = s.get_solution();
for (auto const& row : solution) {
for (auto const& col : row) {
std::cout << col << ' ';
}
std::cout << '\n';
}
} else {
std::clog << "Solving the provided parsed is not possible\n";
}
} catch(std::exception const& ex) {
std::clog << "Failed parsing because: " << ex.what() << std::endl;
return 1;
}
}