-
Notifications
You must be signed in to change notification settings - Fork 1
/
csp_valid.rs
216 lines (187 loc) · 5.9 KB
/
csp_valid.rs
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
use crate::math::{nk, nk_list, vec_to_board, split_multiset, multinomial_coefficient, count_set_squares, contains_line, count_special_locations_bomb_bitset, is_special_location_bomb_bitset, count_special};
use std::cmp::{min, max};
// board with counts
pub struct CountingBoard
{
board: [[usize; 5]; 5],
sum_rows: [usize; 5],
sum_cols: [usize; 5],
bombs_rows: [usize; 5],
bombs_cols: [usize; 5],
ass_rows: [usize;5],
ass_cols: [usize;5],
assigned: usize,
count: [usize; 4],
}
// count states with nr_symbols which don't have too many free multipliers
// per_line max. number of free multipliers in a row or column ("line")
// in_total max. number of free multipliers in total
pub fn count_valid_dumb(nr_symbols: &[usize; 4], in_total: usize, per_line: usize) -> usize
{
let mut cb = CountingBoard {
board: [[127; 5]; 5],
sum_rows: [0;5],
sum_cols: [0;5],
bombs_rows: [0;5],
bombs_cols: [0;5],
ass_rows: [0;5],
ass_cols: [0;5],
assigned: 0,
count: [0; 4]
};
let mut sols = 0;
let mut tries = 0;
sh_valid(&mut cb, &nr_symbols, per_line, in_total, 0, 0, &mut sols, &mut tries);
return sols;
}
fn sh_valid(
cb: &mut CountingBoard,
nr_symbols: &[usize; 4],
per_line: usize,
in_total: usize,
x: usize,
y: usize,
sols: &mut usize,
tries: &mut usize,
) -> ()
{
// check number of symbols
for symbol in 0..4
{
if cb.count[symbol] > nr_symbols[symbol]
{
return;
}
}
if cb.assigned == 25
{
*tries += 1;
if (*tries)%10_000_000 == 0
{
println!("{}, {}M", *sols, (*tries)/1_000_000);
}
if fits_free_multiplier_constraint(cb, in_total, per_line)
{
*sols += 1;
}
}
else {
for symbol in 0..4
{
cb.board[x][y] = symbol;
if symbol == 0
{
cb.bombs_rows[x] += 1;
cb.bombs_cols[y] += 1;
} else {
cb.sum_rows[x] += symbol;
cb.sum_cols[y] += symbol;
}
cb.ass_rows[x] += 1;
cb.ass_cols[y] += 1;
cb.assigned += 1;
cb.count[symbol] += 1;
sh_valid(
cb,
&nr_symbols,
per_line,
in_total,
(x * 5 + y + 1) / 5,
(x * 5 + y + 1) % 5,
sols,
tries
);
cb.board[x][y] = 127;
if symbol == 0
{
cb.bombs_rows[x] -= 1;
cb.bombs_cols[y] -= 1;
} else {
cb.sum_rows[x] -= symbol;
cb.sum_cols[y] -= symbol;
}
cb.ass_rows[x] -= 1;
cb.ass_cols[y] -= 1;
cb.assigned -= 1;
cb.count[symbol] -= 1;
}
}
}
// tells if a fully assigned state is legal
fn fits_free_multiplier_constraint(
cb: &CountingBoard,
max_fm_total: usize,
max_fm_per_line: usize,
) -> bool
{
let (fm_total, fm_per_line) = count_special(&cb.board);
if fm_total > max_fm_total || fm_per_line > max_fm_per_line
{
return false;
}
true
}
// counts valid states using smart math instead of brute force
pub fn count_valid_smart(nr_symbols: &[usize; 4], in_total: usize, per_line: usize) -> usize
{
let mut c = 0;
// for each way to place bombs
for v in nk_list(25, nr_symbols[0])
{
// board_bombs
let bb = vec_to_board(&v);
// count of special locations
let csl = count_special_locations_bomb_bitset(&bb);
// for every number of specials that could be legal
let max_cs = min(max(per_line, in_total), csl);
let mut ways = vec![0; max_cs+1];
for cs in 0..max_cs+1
{
if nr_symbols[1] + cs < csl // not enough 1's to fill rest of special places
{
continue;
}
let mut nr_non_special = nr_symbols.clone();
nr_non_special[0] = 0;
nr_non_special[1] -= csl - cs;
for special_combo in nk_list(csl, cs)
{
// create board
let mut board_special = [[false; 5]; 5];
let mut index = 0;
for row in 0..5
{
for col in 0..5
{
if is_special_location_bomb_bitset(&bb, row, col)
{
if special_combo[index] // special square
{
board_special[row][col] = true;
}
index += 1;
}
}
}
// with or without line?
// Not violating any constraints?
let lines_illegal = contains_line(&board_special, per_line + 1);
let total_illegal = count_set_squares(&board_special) > in_total;
if lines_illegal || total_illegal
{
// illegal board
continue
}
ways[cs] += 1;
}
// for every amount of 2/3's to be assigned
for [as23, a_rest] in split_multiset(&nr_non_special, cs, [false, false, true, true])
{
// every way to assign this amount of 2/3's to the chosen special squares
let ways23 = nk(as23[2] + as23[3], as23[2]);
c += multinomial_coefficient(&a_rest) * ways23 * ways[cs];
}
}
}
return c;
}