summaryrefslogtreecommitdiff
path: root/src/bin/d2jk.rs
blob: e9202d2946f0614cb4bcdeb2967e391f91857185 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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!();
    }
}