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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
|
#include <vector>
#include <array>
#include <numeric>
#include <iostream>
constexpr size_t M = 3;
constexpr size_t N = 3;
template<size_t MM, size_t NN>
using Mat = std::array<std::array<int8_t,MM*NN>,MM*NN>;
template<size_t MM, size_t NN>
using Vec = std::array<int8_t,MM*NN>;
struct point {
point():x{0},y{0}{}
point(size_t x_, size_t y_):x{x_},y{y_}{}
size_t x,y;
point& inc() {
point& ret = *this;
++y;
if(y >= N){
++x;
y -= N;
}
return *this;
}
size_t stride() const {
return x + y*N;
}
bool equal(const point& rhs) const {
return (x == rhs.x && y == rhs.y);
}
};
void print_vec(const Vec& x){
for(size_t j = 0; j < N; ++j){
for(size_t i = 0; i < M; ++i){
point ind{i,j};
std::cout<<x[ind.stride()]<<" ";
}
std::cout<<"\n";
}
std::cout<<std::endl;
}
void print_perm(const std::array<size_t,MN>& x){
for(size_t j = 0; j < MN; ++j){
std::cout<<x[j]<<"\n";
}
std::cout<<std::endl;
}
void print_mat(const Mat& x, const std::array<size_t,MN>& P){
for(size_t j = 0; j < MN; ++j){
for(size_t i = 0; i < MN; ++i){
std::cout<<x[P[j]][i]<<" ";
}
std::cout<<"\n";
}
std::cout<<std::endl;
}
bool lu_decompose_gf2(Mat& R, std::array<size_t,MN>& P){
for(size_t i = 0; i < MN; ++i){
bool found_pivot = false;
size_t pivot_row = i;
for(size_t k = i; k < MN && (not found_pivot); ++k){
if(R[P[k]][i]){
found_pivot = true;
pivot_row = k;
}
}
if(not found_pivot){
return false;
}else if ( i != pivot_row ){
std::swap(P[i],P[pivot_row]);
}
for(size_t k = i+1u; k < MN; ++k){
if(R[P[k]][i]){
for(size_t j = i+1u; j < MN; ++j){
R[P[k]][j] ^= (R[P[i]][j]);
}
}
}
}
return true;
}
template<uint64_t MM, uint64_t NN>
int solve_lights_out(const std::array<MM*NN,int8_t>& b){
return 0;
}
extern "C"{
int fake_main(){
Mat<M,N> A;
Vec<M,N> b{
1,0,1,
1,0,1,
0,0,0
};
// Works best if we use odd N's
// even N might not be solvable in roughly half of the cases
// Init
std::cout<<"Init"<<std::endl;
for(point i{}; i.x < M && i.y < N; i.inc()){
for(point j{}; j.x < M && j.y < N; j.inc()){
if(
(i.equal(j)) ||
(i.x == j.x && (i.y+1) == j.y) ||
(i.x == j.x && (j.y+1) == i.y) ||
(i.y == j.y && (i.x+1) == j.x) ||
(i.y == j.y && (j.x+1) == i.x)
){
A[i.stride()][j.stride()] = true;
}else{
A[i.stride()][j.stride()] = false;
}
// std::cout<<A[i.stride()][j.stride()]<<std::endl;
}
// Populate how you like it.
//b[i.stride()] = false;
}
std::array<size_t, MN> P_ident;
std::iota(P_ident.begin(),P_ident.end(),0);
print_mat(A,P_ident);
std::array<size_t, MN> P;
std::iota(P.begin(),P.end(),0);
// Solve it :)
std::cout<<"Solve"<<std::endl;
if(!lu_decompose_gf2(A,P)){
return -1;
}
std::array<size_t,MN> P_inv{};
for(size_t i = 0; i < MN; ++i){
P_inv[P[i]] = i;
}
std::cout<<"LR"<<std::endl;
print_mat(A,P);
std::cout<<"P"<<std::endl;
print_perm(P);
std::cout<<"Lights-Off puzzle"<<std::endl;
print_vec(b);
Vec y;
for(size_t i = 0; i < MN; ++i){
y[i] = b[P[i]];
for(size_t j = 0; j < i; ++j){
y[i] ^= A[P[i]][j] & y[j];
}
}
Vec x;
for(size_t i = MN; i > 0; --i){
x[i-1] = y[i-1];
for(size_t j = i; j < MN; ++j){
x[i-1] ^= (A[P[i-1]][j] and x[j]);
}
}
std::cout<<"Solution"<<std::endl;
print_vec(x);
return 0;
}
}
int main(){
return fake_main();
}
|