From 6739567d854e796509c40cfe5e0380977928d5f8 Mon Sep 17 00:00:00 2001 From: Yjn024 Date: Tue, 27 Dec 2022 14:54:30 +0800 Subject: [PATCH] rafactor --- src/base.rs | 5 ++- src/main.rs | 121 +------------------------------------------------- src/solver.rs | 120 +++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 126 insertions(+), 120 deletions(-) diff --git a/src/base.rs b/src/base.rs index 35841a1..357f78b 100644 --- a/src/base.rs +++ b/src/base.rs @@ -117,7 +117,10 @@ impl Debug for SimpleComponent { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { Self::Number(_arg0) => f.debug_tuple("Number").field(&self.to_string()).finish(), - _ => f.debug_struct("SimpleExp").field("exp", &self.to_string()).finish(), + _ => f + .debug_struct("SimpleExp") + .field("exp", &self.to_string()) + .finish(), } } } diff --git a/src/main.rs b/src/main.rs index 8819efe..1a6cf09 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,16 +2,11 @@ pub mod base; pub mod math; +pub mod solver; use std::process; use std::time::Instant; -use base::Component; -use base::Expression; - -use base::SimpleComponent; -use math::permute; - fn main() { println!("Enter 4 numbers and split them using spaces."); @@ -63,7 +58,7 @@ fn main() { numbers.sort(); let start = Instant::now(); - let solutions = solve(&numbers, &super_mode, &max); + let solutions = solver::solve(&numbers, &super_mode, &max); if solutions.len() == 0 { println!("No solutions found"); } else { @@ -79,118 +74,6 @@ fn main() { } } -fn solve(numbers_raw: &Vec, super_mode: &bool, limit: &usize) -> Vec { - let nums = { - let mut temp = Vec::new(); - for i in numbers_raw { - temp.push(SimpleComponent::Number(*i)); - } - temp - }; - let mut solutions: Vec = Vec::new(); - let mut operators = ['+', '-', '*', '/'].to_vec(); - if *super_mode { - operators.append(&mut ['^', '>', '<', '|', '&'].to_vec()); - } - for item1 in permute(nums) { - let items = if *super_mode { - math::roll_vec(item1.clone()) - } else { - [item1.clone()].to_vec() - }; - for operator in operators.iter() { - for operator1 in operators.iter() { - for operator2 in operators.iter() { - for item in items.iter() { - let mut exps = Vec::new(); - - exps.push(Expression::create( - Component::create(item[0], *operator1, item[1]), - *operator, - Component::create(item[2], *operator2, item[3]), - )); - exps.push(Expression::create( - Component::of_simple(item[0]), - *operator, - Component::create( - item[1], - *operator1, - SimpleComponent::create( - item[2].get_num(), - *operator2, - item[3].get_num(), - ), - ), - )); - - exps.push(Expression::create( - Component::create( - SimpleComponent::create( - item[0].get_num(), - *operator, - item[1].get_num(), - ), - *operator1, - item[2], - ), - *operator2, - Component::of_simple(item[3]), - )); - - exps.push(Expression::create( - Component::of_simple(item[0]), - *operator, - Component::create( - item[1], - *operator1, - SimpleComponent::create( - item[2].get_num(), - *operator2, - item[3].get_num(), - ), - ), - )); - - for exp in exps { - if limit <= &1 { - if is_24(&exp) { - return vec![exp]; - } - } else { - if is_24(&exp) { - push_if_absent(&mut solutions, exp); - } - } - } - - if solutions.len() >= *limit { - return solutions; - } - } - } - } - } - } - - solutions -} - -fn push_if_absent(vec: &mut Vec, item: T) -where - T: PartialEq, -{ - if !vec.contains(&item) { - vec.push(item); - } -} - -fn is_24(expression: &Expression) -> bool { - match expression.calculate() { - Ok(n) => (n as f64 - 24.0).abs() < 0.000001, - Err(_) => false, - } -} - fn input_string() -> String { let mut input = String::new(); std::io::stdin() diff --git a/src/solver.rs b/src/solver.rs index e69de29..9df04a4 100644 --- a/src/solver.rs +++ b/src/solver.rs @@ -0,0 +1,120 @@ +use base::Component; +use base::Expression; + +use base::SimpleComponent; +use math::permute; + +use crate::base; +use crate::math; + +pub fn solve(numbers_raw: &Vec, super_mode: &bool, limit: &usize) -> Vec { + let nums = { + let mut temp = Vec::new(); + for i in numbers_raw { + temp.push(SimpleComponent::Number(*i)); + } + temp + }; + let mut solutions: Vec = Vec::new(); + let mut operators = ['+', '-', '*', '/'].to_vec(); + if *super_mode { + operators.append(&mut ['^', '>', '<', '|', '&'].to_vec()); + } + for item1 in permute(nums) { + let items = if *super_mode { + math::roll_vec(item1.clone()) + } else { + [item1.clone()].to_vec() + }; + for operator in operators.iter() { + for operator1 in operators.iter() { + for operator2 in operators.iter() { + for item in items.iter() { + let mut exps = Vec::new(); + + exps.push(Expression::create( + Component::create(item[0], *operator1, item[1]), + *operator, + Component::create(item[2], *operator2, item[3]), + )); + exps.push(Expression::create( + Component::of_simple(item[0]), + *operator, + Component::create( + item[1], + *operator1, + SimpleComponent::create( + item[2].get_num(), + *operator2, + item[3].get_num(), + ), + ), + )); + + exps.push(Expression::create( + Component::create( + SimpleComponent::create( + item[0].get_num(), + *operator, + item[1].get_num(), + ), + *operator1, + item[2], + ), + *operator2, + Component::of_simple(item[3]), + )); + + exps.push(Expression::create( + Component::of_simple(item[0]), + *operator, + Component::create( + item[1], + *operator1, + SimpleComponent::create( + item[2].get_num(), + *operator2, + item[3].get_num(), + ), + ), + )); + + for exp in exps { + if limit <= &1 { + if is_24(&exp) { + return vec![exp]; + } + } else { + if is_24(&exp) { + push_if_absent(&mut solutions, exp); + } + } + } + + if solutions.len() >= *limit { + return solutions; + } + } + } + } + } + } + + solutions +} + +fn push_if_absent(vec: &mut Vec, item: T) +where + T: PartialEq, +{ + if !vec.contains(&item) { + vec.push(item); + } +} + +fn is_24(expression: &Expression) -> bool { + match expression.calculate() { + Ok(n) => (n as f64 - 24.0).abs() < 0.000001, + Err(_) => false, + } +}