Implement a couple straightforward heuristics

This commit is contained in:
tali 2022-12-19 17:35:15 -05:00
parent f76cc06bc9
commit e6bf7ec12b
2 changed files with 198 additions and 0 deletions

197
fish/src/eval/mod.rs Normal file
View File

@ -0,0 +1,197 @@
use mino::matrix::{COLUMNS, EMPTY_ROW, FULL_ROW};
use mino::Mat;
pub fn max_height(matrix: &Mat) -> i32 {
matrix.rows() as i32
}
pub fn i_deps(matrix: &Mat) -> i32 {
let mut depth = [0u8; COLUMNS as usize];
let mut count = 0;
for y in 0..matrix.rows() {
// 012345689xxxx
// ↑
// _______xxx___
let mut mask = 0b111 << (COLUMNS - 2);
// _______x.x___
let mut test = 0b101 << (COLUMNS - 2);
for x in (0..COLUMNS).rev() {
if matrix.test_row(y, mask, test) {
depth[x as usize] += 1;
depth[x as usize] %= 4;
if depth[x as usize] == 3 {
count += 1;
}
} else {
depth[x as usize] = 0;
}
mask = mask >> 1;
test = test >> 1;
}
}
count
}
pub fn row_trans(matrix: &Mat) -> i32 {
let mut prev_row = FULL_ROW;
let mut count = 0;
for &curr_row in &matrix[..] {
count += (curr_row ^ prev_row).count_ones();
prev_row = curr_row;
}
count += (prev_row ^ EMPTY_ROW).count_ones();
count as i32
}
#[cfg(test)]
mod test {
use super::*;
use mino::mat;
#[test]
fn test_i_deps() {
assert_eq!(i_deps(Mat::EMPTY), 0);
assert_eq!(
i_deps(mat! {
"xxxxx...xx";
"xxxxx..xxx";
"xxxxx.xx.x";
"xxxxx.xxxx";
}),
0
);
assert_eq!(
i_deps(mat! {
"xxx.....xx";
"xxxx..xxxx";
"xxxxx.xxxx";
"x.xxx.xxxx";
}),
0
);
assert_eq!(
i_deps(mat! {
"xxxxx.xxxx";
"xxxxx.xxxx";
"xxxxx.xxxx";
"xxxxx.xxxx";
}),
1
);
assert_eq!(
i_deps(mat! {
"xxxxx.xxxx";
"xxxxx.xxxx";
"xxxxx.xxxx";
}),
1
);
assert_eq!(
i_deps(mat! {
"xxxxxxxx.x";
"xxxxxxxx.x";
"xxxxxxxx.x";
"xxxxxxxx.x";
"x.xxxxxxxx";
"x.xxxxxxxx";
"x.xxxxxxxx";
"xxxxx.xxxx";
"xxxxx.xxxx";
"xxxxx.xxxx";
}),
3,
);
assert_eq!(
i_deps(mat! {
// 6 rows
"xxxxx.xxxx";
"xxxxx.xxxx";
"xxxxx.xxxx";
"xxxxx.xxxx";
"xxxxx.xxxx";
"xxxxx.xxxx";
}),
1
);
assert_eq!(
i_deps(mat! {
// 7 rows
"xxxxx.xxxx";
"xxxxx.xxxx";
"xxxxx.xxxx";
"xxxxx.xxxx";
"xxxxx.xxxx";
"xxxxx.xxxx";
"xxxxx.xxxx";
}),
2
);
assert_eq!(
i_deps(mat! {
"x.x......x";
"x.xxx.xxxx";
"x.xxx.xxxx";
"x.xxx.xxxx";
}),
2
);
}
#[test]
fn test_row_trans() {
assert_eq!(row_trans(Mat::EMPTY), 10);
assert_eq!(
row_trans(mat! {
"x.........";
"xx........";
"xxx.......";
"xxxx......";
"xxxxx.....";
"xxxxxx....";
"xxxxxxx...";
"xxxxxxxx..";
"xxxxxxxxx.";
"xxxxxxxxxx";
}),
10,
);
assert_eq!(
row_trans(mat! {
"xxxxx.xxxx";
}),
9 + 1,
);
assert_eq!(
row_trans(mat! {
"xxxxx.xxxx";
"xxxxx.xxxx";
"xxxxx.xxxx";
"xxxxx.xxxx";
"xxxxx.xxxx";
}),
9 + 1,
);
assert_eq!(
row_trans(mat! {
"xxxxx.xxxx";
"xxxx.Xxxxx";
}),
9 + 2 + 1
);
assert_eq!(
row_trans(mat! {
"xxxxx.xxx.";
"xx....xxxx";
}),
8 + 4 + 4
);
assert_eq!(
row_trans(mat! {
"xxxx..xxxx";
"xxxx...xxx";
"xxxxx.xxxx";
}),
8 + 1 + 2 + 1,
);
}
}

View File

@ -3,6 +3,7 @@
extern crate alloc;
pub mod ai;
pub mod eval;
pub mod find;
#[cfg(feature = "io")]