Advertisement
Guest User

a

a guest
Feb 20th, 2019
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 2.69 KB | None | 0 0
  1. use std::collections::VecDeque;
  2. use std::fs::File;
  3. use std::io::Read;
  4.  
  5. struct Tree<T>(Vec<Node<T>>);
  6.  
  7. struct Node<T> {
  8.     sub_pos: i32,
  9.     value: T,
  10.     subtree: Vec<Node<T>>,
  11. }
  12.  
  13. // Represents a substitution rule
  14. struct Rule {
  15.     from: String,
  16.     to: String,
  17. }
  18.  
  19. fn main() -> Result<(), Box<std::error::Error>> {
  20.     // Parse the input :/
  21.     // I hate this part
  22.     let mut rules = Vec::new();
  23.     let mut input_file = File::open("input.txt")?;
  24.     let mut input_text = String::new();
  25.     input_file.read_to_string(&mut input_text);
  26.     let input_lines: Vec<String> = input_text.lines().map(ToOwned::to_owned).collect();
  27.  
  28.     // Now parse these as rules
  29.     for i in 0..3 {
  30.         let split: Vec<String> = input_lines[i].split(" ").map(ToOwned::to_owned).collect();
  31.         rules.push(Rule {
  32.             from: split[0].clone(),
  33.             to: split[1].clone(),
  34.         });
  35.     }
  36.  
  37.     let split: Vec<String> = input_lines[3].split(" ").map(ToOwned::to_owned).collect();
  38.     let seq_len = split[0].parse::<i32>()?;
  39.     let start = split[1].clone();
  40.     let end = split[2].clone();
  41.  
  42.     // Now we can build up a search tree
  43.     let mut tree = Tree::<String>(vec![Node::<String> {
  44.         sub_pos: 0,
  45.         value: start,
  46.         subtree: Vec::new(),
  47.     }]);
  48.     // Queue of the next layer
  49.     let mut queue: VecDeque<&mut Node<String>> = VecDeque::new();
  50.     queue.push_back(&mut tree.0[0]);
  51.     for i in 0..seq_len {
  52.         for node in &mut queue {
  53.             for rule in &rules {
  54.                 for (new_string, sub_pos) in transform(&node.value, rule) {
  55.                     if i == seq_len - 1 && new_string == end {
  56.                         // Solution found!
  57.                         // search it here - cba
  58.                     }
  59.                     node.subtree.push(Node::<String> {
  60.                         sub_pos: sub_pos,
  61.                         value: new_string,
  62.                         subtree: Vec::new(),
  63.                     });
  64.                 }
  65.             }
  66.         }
  67.     }
  68.  
  69.     Ok(())
  70. }
  71.  
  72. fn transform(input: &str, rule: &Rule) -> Vec<(String, i32)> {
  73.     let mut solutions: Vec<(String, i32)> = Vec::new();
  74.     let length = rule.from.len();
  75.     for idx in 0..input.len() - length {
  76.         let slice = &input[idx..idx + length];
  77.         if slice == rule.from {
  78.             // Avaliable substitution, so go ahead
  79.             let new_string = format!(
  80.                 "{}{}{}",
  81.                 &input[0..idx],
  82.                 rule.to,
  83.                 &input[length + idx..input.len()]
  84.             );
  85.             // Add one to start position - 1 indexed
  86.             solutions.push((new_string, (idx + 1) as i32));
  87.         }
  88.     }
  89.     solutions
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement