Advertisement
EusebioDM

Untitled

Sep 4th, 2019
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 2.90 KB | None | 0 0
  1. use std::str::Chars;
  2.  
  3. trait Turtle {
  4.     fn move_forwards(&mut self);
  5.     fn turn_right(&mut self);
  6.     fn has_flower(&self) -> bool;
  7. }
  8.  
  9. struct TestTurtle {
  10.     pub moves: Vec<char>
  11. }
  12.  
  13. impl Turtle for TestTurtle {
  14.     fn move_forwards(&mut self) {
  15.         self.moves.push('F');
  16.     }
  17.  
  18.     fn turn_right(&mut self) {
  19.         self.moves.push('R');
  20.     }
  21.  
  22.     fn has_flower(&self) -> bool {
  23.         false
  24.     }
  25. }
  26.  
  27.  
  28. enum Instruction {
  29.     Forwards,
  30.     Right,
  31.     Group { elements: Vec<Box<Instruction>> },
  32.     Repeat { num: u8, element: Box<Instruction> },
  33.     Conditional { first: Box<Instruction>, second: Box<Instruction> },
  34. }
  35.  
  36. impl Instruction {
  37.     fn parse(stream: &mut Chars) -> Option<Instruction> {
  38.         let m_ins = stream.next();
  39.  
  40.         match m_ins {
  41.             Some('F') => Some(Instruction::Forwards),
  42.             Some('R') => Some(Instruction::Right),
  43.             Some('[') => {
  44.                 let mut elements = Vec::new();
  45.  
  46.                 while let Some(ins) = Instruction::parse(stream) {
  47.                     elements.push(Box::new(ins));
  48.                 }
  49.  
  50.                 Some(Instruction::Group { elements })
  51.             }
  52.             Some(']') => None,
  53.             Some('?') => {
  54.                 let first = Box::new(Instruction::parse(stream)?);
  55.                 let second = Box::new(Instruction::parse(stream)?);
  56.  
  57.                 Some(Instruction::Conditional { first, second })
  58.             }
  59.             Some(rep) if rep > '0' && rep <= '9' => {
  60.                 let num = rep.to_digit(10).unwrap() as u8;
  61.                 let element = Box::new(Instruction::parse(stream)?);
  62.  
  63.                 Some(Instruction::Repeat { num, element })
  64.             }
  65.  
  66.             _ => None
  67.         }
  68.     }
  69.  
  70.     fn execute(&self, turtle: &mut dyn Turtle) {
  71.         match self {
  72.             Instruction::Forwards => turtle.move_forwards(),
  73.             Instruction::Right => turtle.turn_right(),
  74.             Instruction::Group { elements } => {
  75.                 for e in elements {
  76.                     e.execute(turtle);
  77.                 }
  78.             }
  79.             Instruction::Repeat { num, element } => {
  80.                 for _ in 0..*num {
  81.                     element.execute(turtle);
  82.                 }
  83.             }
  84.             Instruction::Conditional { first, second } => {
  85.                 if turtle.has_flower() {
  86.                     first.execute(turtle);
  87.                 } else {
  88.                     second.execute(turtle);
  89.                 }
  90.             }
  91.         };
  92.     }
  93. }
  94.  
  95.  
  96. fn decode_google(mut stream: Chars) -> Vec<char> {
  97.     let mut turtle = TestTurtle { moves: Vec::new() };
  98.  
  99.     while let Some(ins) = Instruction::parse(&mut stream) {
  100.         ins.execute(&mut turtle);
  101.     }
  102.  
  103.     turtle.moves
  104. }
  105.  
  106.  
  107. pub fn main() {
  108.     let input = "4F2[RR]".to_string();
  109.     let chars = input.chars();
  110.  
  111.     println!("{:?}", decode_google(chars));
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement