-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmath.rs
More file actions
458 lines (392 loc) · 9.41 KB
/
math.rs
File metadata and controls
458 lines (392 loc) · 9.41 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
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
use std::cmp::{min, max};
use crate::packed::{get_from_packed_state, set_in_packed_state};
// n choose k
// 0 for k > n
// uses multiplication formula
// no caching
// only usize accuracy
pub fn nk(n:usize, k:usize) -> usize
{
if k > n {
return 0;
}
let mut ans = 1;
let mut n = n;
let k = min(k, n-k);
for j in 1..k+1
{
if n%j == 0
{
ans *= n/j;
} else if ans%j == 0
{
ans = ans/j*n;
}
else {
ans = (ans*n)/j;
}
n -= 1;
}
return ans;
}
// number of ways to assign length(numbers) colors
// to sum(numbers) distinguishable places such that
// color i appears exactly numbers[i] times
pub fn multinomial_coefficient(numbers: &[usize]) -> usize
{
let mut left = 0;
for num in numbers
{
left += num;
}
let mut r = 1;
for &num in numbers
{
r *= nk(left, num);
left -= num;
}
return r;
}
// vec of all ways to choose k from n
pub fn nk_list(n: usize, k: usize) -> Vec<Vec<bool>>
{
let mut r = Vec::with_capacity(nk(n, k));
_nk_list_helper(n, k, &mut vec![false; n], 0, 0, &mut r);
return r;
}
fn _nk_list_helper(
n: usize,
k: usize,
current: &mut Vec<bool>,
assigned: usize,
index: usize,
v: &mut Vec<Vec<bool>>
)
{
if assigned == k
{
let rex = current.clone();
v.push(rex);
}
else if index < current.len() {
current[index] = false;
_nk_list_helper(n, k, current, assigned, index+1, v);
current[index] = true;
_nk_list_helper(n, k, current, assigned+1, index+1, v);
current[index] = false;
}
}
// convert vector of length 25 to 5x5 array (board)
pub fn vec_to_board(v: &Vec<bool>) -> [[bool;5];5]
{
let mut a = [[false;5];5];
for i in 0..25
{
a[i/5][i%5] = v[i];
}
return a;
}
// count the total number of special squares
// and the max. number of special squares in a row or column
pub fn count_special(state: &[[usize;5];5]) -> (usize, usize)
{
let mut count_total = 0;
let mut rows_special = [0;5];
let mut cols_special = [0;5];
for r in 0..5
{
for c in 0..5
{
if is_square_special(&state, r, c)
{
count_total += 1;
rows_special[r] += 1;
cols_special[c] += 1;
}
}
}
let mut count_line = 0;
for i in 0..5
{
count_line = max(count_line, rows_special[i]);
count_line = max(count_line, cols_special[i]);
}
return (count_total, count_line);
}
// WRONG TODO fix and recompute the counts!
fn is_square_special(state: &[[usize;5];5], row: usize, col: usize) -> bool
{
(state[row][col] == 2 || state[row][col] == 3) && is_special_location(&state, row, col)
}
pub fn count_symbols(state: &[[usize;5];5]) -> [usize;4]
{
let mut count = [0; 4];
for r in 0..5
{
for c in 0..5
{
count[state[r][c]] += 1;
}
}
return count;
}
pub fn count_assigned(state: &[[usize;5];5]) -> usize
{
let mut count = 0;
for r in 0..5
{
for c in 0..5
{
if state[r][c] != 0
{
count += 1;
}
}
}
return count;
}
pub fn count_assigned_packed(packed_state: u64) -> usize
{
let mut count = 0;
for r in 0..5
{
for c in 0..5
{
if get_from_packed_state(packed_state,r,c) != 0
{
count += 1;
}
}
}
return count;
}
// contains at least k true values in a row or column
pub fn contains_line(a: &[[bool;5];5], k: usize) -> bool
{
for line in 0..5
{
let mut rc = 0;
let mut cc = 0;
for i in 0..5
{
if a[line][i]
{
rc += 1;
}
if a[i][line]
{
cc += 1;
}
}
if rc >= k || cc >= k
{
return true;
}
}
return false;
}
// count how many squares are special locations
// special locations = no bombs on it's row or no bombs on it's column
// when a special location is occupied by a 2 or 3, it's a special square
pub fn count_special_locations_bomb_bitset(a: &[[bool;5];5]) -> usize
{
let mut count = 0;
for row in 0..5
{
for col in 0..5
{
if is_special_location_bomb_bitset(&a, row, col)
{
count += 1;
}
}
}
return count;
}
// given boolean array of bombs=true, tell whether a given location is special
// (no bombs in row or no bombs in column)
pub fn is_special_location_bomb_bitset(a: &[[bool;5];5], row: usize, col: usize) -> bool
{
let mut bombs_on_row = false;
let mut bombs_on_col = false;
for i in 0..5
{
if a[row][i]
{
bombs_on_row = true;
}
if a[i][col]
{
bombs_on_col = true;
}
}
(!bombs_on_row) || (!bombs_on_col)
}
pub fn is_special_location(state: &[[usize;5];5], row: usize, col: usize) -> bool
{
let mut bombs_on_row = false;
let mut bombs_on_col = false;
for i in 0..5
{
if state[row][i] == 0
{
bombs_on_row = true;
}
if state[i][col] == 0
{
bombs_on_col = true;
}
}
(!bombs_on_row) || (!bombs_on_col)
}
// count how many squares are true
pub fn count_set_squares(a: &[[bool;5];5]) -> usize
{
let mut count = 0;
for r in 0..5
{
for c in 0..5
{
if a[r][c]
{
count += 1;
}
}
}
return count;
}
// all ways to separate certain amounts of the four symbols into two
// depending on boolean array only. False at only[symbol] means that none of
// that symbol may be taken
pub fn split_multiset(
available_symbols: &[usize;4], // how many of each symbol there are
size_fms: usize, // number of symbols in first multiset
only: [bool;4],
) -> Vec<[[usize;4];2]>
{
let mut v = Vec::new();
split_multiset_helper(&mut [0;4], 0, 0, &only, &available_symbols, size_fms, &mut v);
return v;
}
fn split_multiset_helper(
current: &mut[usize;4],
size: usize,
index: usize,
only: &[bool;4],
available_symbols: &[usize;4],
size_fms: usize,
v: &mut Vec<[[usize;4];2]>,
)
{
if index == 4 && size == size_fms
{
let mut opposite = available_symbols.clone();
for symbol in 0..4
{
opposite[symbol] -= current[symbol];
}
let ta = [current.clone(), opposite];
v.push(ta);
}
else if index < 4
{
for c in 0..available_symbols[index]+1
{
if size+c <= size_fms && (c == 0 || only[index])
{
current[index] = c;
split_multiset_helper(current, size+c, index+1, &only, &available_symbols, size_fms, v);
}
}
}
}
// swap rows and columns of array a: a[i][j] <-> a[j][i] for all i, j
pub fn transpose_in_place(a: &mut [[usize;5];5])
{
for row in 0..5
{
for col in row+1..5
{
let tmp = a[row][col];
a[row][col] = a[col][row];
a[col][row] = tmp;
}
}
}
// swap rows and columns of array a: a[i][j] <-> a[j][i] for all i, j
pub fn transpose_packed(packed_state: u64) -> u64
{
let mut transposed_packed_state = 0;
for row in 0..5
{
for col in 0..5
{
let value = get_from_packed_state(packed_state,row,col);
transposed_packed_state = set_in_packed_state(transposed_packed_state,col,row, value);
}
}
transposed_packed_state
}
pub fn print_board(board: &[[usize; 5]; 5]) {
for row in 0..5 {
for col in 0..5 {
if board[row][col] == 0
{
print!(". ", );
} else {
print!("{} ", board[row][col]);
}
}
println!();
}
}
pub fn print_board_with_cons(
board: &[[usize; 5]; 5],
sr: &[usize;5],
sc: &[usize;5],
br: &[usize;5],
bc: &[usize;5],
)
{
for col in 0..5
{
print!("{} ", sc[col]);
}
println!();
for col in 0..5
{
print!("{} ", bc[col]);
}
println!();
for _ in 0..5
{
print!("--");
}
println!();
for row in 0..5 {
for col in 0..8 {
if col == 5
{
print!("| ");
}
else if col == 6
{
print!("{} ", br[row]);
}
else if col == 7
{
print!("{} ", sr[row]);
}
else {
if board[row][col] == 0
{
print!(". ", );
}
else {
print!("{} ", board[row][col]);
}
}
}
println!();
}
}