diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/bin/d2jk.rs | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/src/bin/d2jk.rs b/src/bin/d2jk.rs new file mode 100644 index 0000000..e9202d2 --- /dev/null +++ b/src/bin/d2jk.rs @@ -0,0 +1,53 @@ +use std::collections::HashMap; + +use itertools::Itertools; + +fn main() { + let truth_table_file_path = std::env::args().nth(1).unwrap(); + let truth_table_file = std::fs::read_to_string(truth_table_file_path).unwrap(); + + let mut lines = truth_table_file.lines(); + let truth_table_inputs_line = lines.next().unwrap(); + let truth_table_outputs_line = lines.next().unwrap(); + + let truth_table_inputs: HashMap<&str, usize> = truth_table_inputs_line + .split_whitespace() + .enumerate() + .map(|(i, input)| (input, i)) + .collect(); + let truth_table_outputs = truth_table_outputs_line.split_whitespace().collect_vec(); + + let mut new_outputs = vec![]; + for output in &truth_table_outputs { + if truth_table_inputs.contains_key(output) { + new_outputs.push(format!("{output}_J")); + new_outputs.push(format!("{output}_K")); + } else { + new_outputs.push(output.to_string()); + } + } + + println!("{truth_table_inputs_line}"); + println!("{}", new_outputs.join(" ")); + + for line in lines { + let (input, output) = line.split_once(char::is_whitespace).unwrap(); + print!("{input} "); + + for (value, output_name) in output.chars().zip(&truth_table_outputs) { + if let Some(&i) = truth_table_inputs.get(output_name) { + match (&input[i..i + 1], value) { + ("0", '0') => print!("0-"), + ("0", '1') => print!("1-"), + ("1", '0') => print!("-1"), + ("1", '1') => print!("-0"), + _ => unreachable!(), + } + } else { + print!("{value}"); + } + } + + println!(); + } +} |