Write a program in Rust to solve the following problem: --- This year, Santa brought…
- User
Write a program in Rust to solve the following problem:
This year, Santa brought little Bobby Tables a set of wires and bitwise logic gates! Unfortunately, little Bobby is a little under the recommended age range, and he needs help assembling the circuit.
Each wire has an identifier (some lowercase letters) and can carry a 16-bit signal (a number from 0 to 65535). A signal is provided to each wire by a gate, another wire, or some specific value. Each wire can only get a signal from one source, but can provide its signal to multiple destinations. A gate provides no signal until all of its inputs have a signal.
The included instructions booklet describes how to connect the parts together: x AND y -> z means to connect wires x and y to an AND gate, and then connect its output to wire z.
For example:
123 -> x means that the signal 123 is provided to wire x. x AND y -> z means that the bitwise AND of wire x and wire y is provided to wire z. p LSHIFT 2 -> q means that the value from wire p is left-shifted by 2 and then provided to wire q. NOT e -> f means that the bitwise complement of the value from wire e is provided to wire f.Other possible gates include OR (bitwise OR) and RSHIFT (right-shift). If, for some reason, you'd like to emulate the circuit instead, almost all programming languages (for example, C, JavaScript, or Python) provide operators for these gates.
For example, here is a simple circuit:
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
After it is run, these are the signals on the wires:
d: 72 e: 507 f: 492 g: 114 h: 65412 i: 65079 x: 123 y: 456
Given any set of input instructions, what signal is ultimately provided to wire a?
- Assistant
- User
What do you think of this solution?
use std::collections::HashMap;
#[derive(Debug, Clone)] struct Expr<'a> { value: Option, string: &'a str, }
fn main() { let instructions = "...";
let mut instruction_map = HashMap::<&str, Expr>::new(); for line in instructions.trim().split("\n") { let line = line.trim(); let [expr, id] = line.split(" -> ").collect::<Vec<&str>>()[..] else { todo!() }; let expr_str = expr.trim(); let id = id.trim(); let expr = Expr { value: None, string: expr_str, }; instruction_map.insert(id, expr); } fn eval_token<'a>(map: &mut HashMap<&'a str, Expr<'a>>, token: &'a str) -> u16 { return match token.parse::<u16>() { Ok(n) => n, _ => evaluate(map, token), }; } fn evaluate<'a>(map: &mut HashMap<&'a str, Expr<'a>>, id: &'a str) -> u16 { let mut expr = match map.get(id) { Some(e) => e.clone(), _ => panic!("No {id} in {map:?}"), }; return match expr.value { Some(v) => v, _ => { let tokens = expr.string.split(" ").collect::<Vec<&str>>(); let result = match tokens.len() { 1 => eval_token(map, expr.string), 2 => match tokens[0] { "NOT" => !eval_token(map, tokens[1]), _ => panic!("{} not supported!", expr.string), }, 3 => match tokens[1] { "OR" => eval_token(map, tokens[0]) | eval_token(map, tokens[2]), "AND" => eval_token(map, tokens[0]) & eval_token(map, tokens[2]), "LSHIFT" => eval_token(map, tokens[0]) << eval_token(map, tokens[2]), "RSHIFT" => eval_token(map, tokens[0]) >> eval_token(map, tokens[2]), _ => panic!("{} not supported!", expr.string), }, _ => panic!("{} not supported!", expr.string), }; expr.value = Some(result); map.insert(id, expr); return result; } }; } dbg!(evaluate(&mut instruction_map, "a")); - Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.