From 24139814cc8b588abc5ceba42599a2eff4eaf57a Mon Sep 17 00:00:00 2001 From: Arne Keller Date: Mon, 15 Mar 2021 19:17:03 +0100 Subject: [PATCH] Output of specific solution --- src/bin/svg.rs | 26 +++++++++++++++++++------- src/lib.rs | 18 ++++++++++-------- src/main.rs | 4 ++-- 3 files changed, 31 insertions(+), 17 deletions(-) diff --git a/src/bin/svg.rs b/src/bin/svg.rs index 70a40dc..acfc166 100644 --- a/src/bin/svg.rs +++ b/src/bin/svg.rs @@ -1,3 +1,5 @@ +use std::env::args; + use svg_fmt::*; use kv::*; @@ -14,6 +16,8 @@ const COLORS: &'static [Color] = &[ ]; fn main() { + let args = args().collect::>(); + let vars = [A, B, C]; let function = vec![0, 0, 1, 1, 1, 1, 1, 0].into_iter().map(Into::into).collect(); let groups = find_groups(&function, &vars, One); @@ -22,25 +26,33 @@ fn main() { eprintln!("{}", print_implicant(x)); } eprintln!("prime:"); - let prime = find_prime(&function, &vars, One, &groups); + let (prime, other) = find_prime(&function, &vars, One, &groups); for x in &prime { eprintln!("{}", print_implicant(x)); } eprintln!("solutions:"); - let solutions = all_solutions(function.clone(), &vars, One, &prime, &groups); - for sol in solutions { + let solutions = all_solutions(function.clone(), &vars, One, &prime, &other); + for sol in &solutions { for x in &prime { eprint!("{} | ", print_implicant(x)); } - for x in &sol { + for x in sol { eprint!("{} | ", print_implicant(x)); } eprintln!(); } - let block_masks = groups.iter() - .map(block_to_mask) - .collect::>(); + let block_masks: Vec<_> = if let Some(Ok(index)) = args.get(1).map(|x| x.parse::()) { + prime.iter() + .map(block_to_mask) + .chain(solutions[index].iter().map(block_to_mask)) + .collect() + } else { + prime.iter() + .map(block_to_mask) + .chain(other.iter().map(block_to_mask)) + .collect() + }; let (grid, w, h) = grid(3); println!("{}", BeginSvg { w: w * SIZE_FACTOR + 3, h: h * SIZE_FACTOR + 3 }); diff --git a/src/lib.rs b/src/lib.rs index d0712ec..ed50770 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,4 +1,4 @@ -use std::fmt::{self, Display}; +use std::fmt; use itertools::Itertools; @@ -99,7 +99,7 @@ pub fn find_groups(func: &FunctionSpec, vars: &[Variable], typ: Output) -> Vec(func: &FunctionSpec, vars: &[Variable], typ: Output, blocks: &'a [Block]) -> Vec<&'a [(Variable, Output)]> { +pub fn find_prime<'a>(func: &FunctionSpec, vars: &[Variable], typ: Output, blocks: &'a [Block]) -> (Vec<&'a [(Variable, Output)]>, Vec<&'a [(Variable, Output)]>) { assert_eq!(func.len(), 2usize.pow(vars.len() as u32)); let block_masks = blocks.iter() @@ -125,10 +125,12 @@ pub fn find_prime<'a>(func: &FunctionSpec, vars: &[Variable], typ: Output, block prime[i] = true; } } - prime.into_iter().enumerate().filter(|&(_, prime)| prime).map(|(i, _)| &*blocks[i]).collect() + let a = prime.iter().enumerate().filter(|&(_, &prime)| prime).map(|(i, _)| &*blocks[i]).collect(); + let b = prime.iter().enumerate().filter(|&(_, &prime)| !prime).map(|(i, _)| &*blocks[i]).collect(); + (a, b) } -pub fn all_solutions(mut func: FunctionSpec, vars: &[Variable], typ: Output, prime: &[BlockRef], other: &[Block]) -> Vec> { +pub fn all_solutions(mut func: FunctionSpec, vars: &[Variable], typ: Output, prime: &[BlockRef], other: &[BlockRef]) -> Vec> { assert_eq!(func.len(), 2usize.pow(vars.len() as u32)); // first mark all inputs covered by prime blocks as Any @@ -153,7 +155,7 @@ pub fn all_solutions(mut func: FunctionSpec, vars: &[Variable], typ: Output, pri all_recursive(func, vars, typ, other, &other_masks) } -fn all_recursive(func: FunctionSpec, vars: &[Variable], typ: Output, other: &[Block], other_masks: &[(Variable, Variable)]) -> Vec> { +fn all_recursive(func: FunctionSpec, vars: &[Variable], typ: Output, other: &[BlockRef], other_masks: &[(Variable, Variable)]) -> Vec> { let mut all = Vec::new(); for i in 0..other.len() { let block = &other[i]; @@ -173,10 +175,10 @@ fn all_recursive(func: FunctionSpec, vars: &[Variable], typ: Output, other: &[Bl } let extensions = all_recursive(func, vars, typ, other, other_masks); if extensions.is_empty() { - all.push(vec![block.clone()]); + all.push(vec![block.to_vec()]); } for mut extension in extensions { - extension.push(block.clone()); + extension.push(block.to_vec()); all.push(extension); } } @@ -253,7 +255,7 @@ fn test_3var() { vec![(A, One), (C, Zero)], vec![(B, Zero), (C, Zero)], ]); - let prime = find_prime(&fun, &mut [A, B, C], Zero, &groups); + let prime = find_prime(&fun, &mut [A, B, C], Zero, &groups).0; assert_eq!(prime, vec![ vec![(A, Zero), (B, Zero)], vec![(A, One), (C, Zero)], diff --git a/src/main.rs b/src/main.rs index 24555f5..157a032 100644 --- a/src/main.rs +++ b/src/main.rs @@ -8,12 +8,12 @@ fn main() { println!("{}", print_implicant(x)); } println!("prime:"); - let prime = find_prime(&function, &[A, B, C], One, &groups); + let (prime, other) = find_prime(&function, &[A, B, C], One, &groups); for x in &prime { println!("{}", print_implicant(x)); } println!("solutions:"); - let solutions = all_solutions(function, &[A, B, C], One, &prime, &groups); + let solutions = all_solutions(function, &[A, B, C], One, &prime, &other); for sol in solutions { for x in &prime { print!("{} | ", print_implicant(x));