To solve this problem, we need to model the circuit and its gates in Rust and then evaluate the provided instructions to determine the signal on a specific wire. Here's how we can achieve this:
- Parse the instructions to determine the operations and the involved wires and constants.
- Evaluate the instructions taking care of dependencies, i.e., ensure that a wire's signal is only evaluated once all its input signals are determined.
Here's a complete Rust program to do just that:
use std::collections::HashMap;
#[derive(Debug, Clone)]
enum Operation {
Assign(Value), // = x
And(Value, Value), // AND x y
Or(Value, Value), // OR x y
LShift(Value, u16), // LSHIFT x n
RShift(Value, u16), // RSHIFT x n
Not(Value), // NOT x
}
#[derive(Debug, Clone)]
enum Value {
Wire(String), // Wire named x
Constant(u16), // Constant value
}
fn parse_value(s: &str) -> Value {
match s.parse::<u16>() {
Ok(num) => Value::Constant(num),
Err(_) => Value::Wire(s.to_string()),
}
}
fn parse_instruction(instruction: &str) -> (String, Operation) {
let mut parts = instruction.split(" -> ");
let left = parts.next().unwrap();
let right = parts.next().unwrap().to_string();
let operation = if left.contains("AND") {
let parts: Vec<&str> = left.split(" AND ").collect();
Operation::And(parse_value(parts[0]), parse_value(parts[1]))
} else if left.contains("OR") {
let parts: Vec<&str> = left.split(" OR ").collect();
Operation::Or(parse_value(parts[0]), parse_value(parts[1]))
} else if left.contains("LSHIFT") {
let parts: Vec<&str> = left.split(" LSHIFT ").collect();
Operation::LShift(parse_value(parts[0]), parts[1].parse().unwrap())
} else if left.contains("RSHIFT") {
let parts: Vec<&str> = left.split(" RSHIFT ").collect();
Operation::RShift(parse_value(parts[0]), parts[1].parse().unwrap())
} else if left.contains("NOT") {
let parts: Vec<&str> = left.split("NOT ").collect();
Operation::Not(parse_value(parts[1]))
} else {
Operation::Assign(parse_value(left))
};
(right, operation)
}
fn evaluate(
wire: &str,
operations: &HashMap<String, Operation>,
values: &mut HashMap<String, u16>
) -> u16 {
if values.contains_key(wire) {
return values[wire];
}
let operation = operations.get(wire).unwrap();
let result = match operation.clone() {
Operation::Assign(val) => resolve_value(&val, operations, values),
Operation::And(val1, val2) => resolve_value(&val1, operations, values) & resolve_value(&val2, operations, values),
Operation::Or(val1, val2) => resolve_value(&val1, operations, values) | resolve_value(&val2, operations, values),
Operation::LShift(val, shift) => resolve_value(&val, operations, values) << shift,
Operation::RShift(val, shift) => resolve_value(&val, operations, values) >> shift,
Operation::Not(val) => !resolve_value(&val, operations, values),
};
values.insert(wire.to_string(), result);
result
}
fn resolve_value(
value: &Value,
operations: &HashMap<String, Operation>,
values: &mut HashMap<String, u16>
) -> u16 {
match value {
Value::Wire(wire) => evaluate(wire, operations, values),
Value::Constant(constant) => *constant,
}
}
fn main() {
let instructions = vec![
"123 -> x",
"456 -> y",
"x AND y -> d",
"x OR y -> e",
"x LSHIFT 2 -> f",
"y RSHIFT 2 -> g",
"NOT x -> h",
"NOT y -> i"
];
let mut operations = HashMap::new();
for instr in instructions {
let (wire, operation) = parse_instruction(instr);
operations.insert(wire, operation);
}
let mut values = HashMap::new();
let result = evaluate("a", &operations, &mut values); // Change "a" to any wire you want to evaluate
println!("Signal provided to wire a: {}", result);
}
Explanation:
-
Enum Definition:
Operation: Represents various operations that can be performed. This includes simple assignment, binary operations (AND, OR), shifts (LSHIFT, RSHIFT), and unary operations (NOT).
Value: Represents either a wire identifier or a constant value.
-
Parser Functions:
parse_value(): Parses a value, either a wire or a constant.
parse_instruction(): Parses a full instruction line into a wire and an associated operation.
-
Evaluation Logic:
evaluate(): Recursively determines the signal on a given wire by evaluating its associated operation. Caches results in the values map to avoid redundant calculations.
resolve_value(): Resolves a Value into its signal by either evaluating a wire or returning a constant.
-
Main Function:
- Instructions are parsed into the
operations map.
- The signal from wire
"a" is evaluated and printed.
Usage:
Change "a" in the evaluate("a", ...) call to the specific wire you need to evaluate. This ensures the signal for the specified wire is calculated and displayed.
This Rust program effectively simulates the described circuit and handles the bitwise logic operations and dependencies between wires.