Skip to content

Commit

Permalink
Merge pull request #42 from 4atj/matcher
Browse files Browse the repository at this point in the history
Better goal matching
  • Loading branch information
JayXon authored Jan 31, 2024
2 parents fcbc942 + a011c1c commit d2d88c0
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 21 deletions.
8 changes: 5 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ fn is_leaf_expr(op_idx: OpIndex, length: usize) -> bool {
fn save(level: &mut CacheLevel, expr: Expr, n: usize, cache: &Cache, hashset_cache: &HashSetCache) {
const ALL_MASK: Mask = (1 << INPUTS.len()) - 1;

if (!USE_ALL_VARS || expr.var_mask == ALL_MASK) && match_goal(&expr) {
if (!USE_ALL_VARS || expr.var_mask == ALL_MASK) && Matcher::new().match_all(&expr) {
println!("{expr}");
return;
}
Expand Down Expand Up @@ -123,13 +123,14 @@ fn find_binary_operators(
if let (Some(&op_idx), Some(op)) = (OP_BINARY_INDEX_TABLE.get(idx), BINARY_OPERATORS.get(idx)) {
if op.name.len() == op_len && op.can_apply(el, er) {
if MATCH_1BY1 && is_leaf_expr(op_idx, n) {
let mut matcher = Matcher::new();
if el
.output
.iter()
.zip(er.output.iter())
.enumerate()
.all(|(i, (&ol, &or))| match (op.apply)(ol, or) {
Some(o) => match_one(i, o),
Some(o) => matcher.match_one(i, o),
None => false,
})
{
Expand Down Expand Up @@ -199,11 +200,12 @@ fn find_unary_operators(
if let (Some(&op_idx), Some(op)) = (OP_UNARY_INDEX_TABLE.get(idx), UNARY_OPERATORS.get(idx)) {
if op.can_apply(er) {
if MATCH_1BY1 && is_leaf_expr(op_idx, n) {
let mut matcher = Matcher::new();
if er
.output
.iter()
.enumerate()
.all(|(i, &or)| match_one(i, (op.apply)(or)))
.all(|(i, &or)| matcher.match_one(i, (op.apply)(or)))
{
println!("{}{}", op_idx, er);
}
Expand Down
33 changes: 15 additions & 18 deletions src/params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,23 @@ pub const INPUTS: &[Input] = &[Input {
vec: &['E' as i32, 'W' as i32, 'N' as i32, 'S' as i32],
}];

/// This function gets applied to the output when comparing to GOAL.
///
/// If you only care about e.g. truthiness, change this to
///
/// (n != 0) as Num
///
/// and use only 0/1 in the GOAL.
pub fn mapping(n: Num) -> Num {
n
}
pub struct Matcher {}

pub fn match_one(index: usize, output: Num) -> bool {
mapping(output) == GOAL[index]
}
impl Matcher {
pub fn new() -> Matcher {
Matcher {}
}

pub fn match_one(&mut self, index: usize, output: Num) -> bool {
output == GOAL[index]
}

pub fn match_goal(expr: &Expr) -> bool {
expr.output
.iter()
.enumerate()
.all(|(i, &o)| match_one(i, o))
pub fn match_all(&mut self, expr: &Expr) -> bool {
expr.output
.iter()
.enumerate()
.all(|(i, &o)| self.match_one(i, o))
}
}

pub const GOAL: &[Num] = &[1, -1, 0, 0];
Expand Down

0 comments on commit d2d88c0

Please sign in to comment.