Advertisement
GeoffreyYeung

Scheduling Problem

Apr 4th, 2018
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 4.71 KB | None | 0 0
  1. use std::error::Error;
  2. use std::fmt;
  3.  
  4. const N: u8 = 6;
  5.  
  6. //who's meeting who
  7. type Pair = (u8, u8);
  8.  
  9. //generate all meetings needed
  10. fn generate_pairs() -> Vec<Pair> {
  11.     let mut result = Vec::new();
  12.     for x in 1..N {
  13.         for y in x + 1..N + 1 {
  14.             result.push((x, y));
  15.         }
  16.     }
  17.     result
  18. }
  19.  
  20. //ended up not using any of these this
  21. //starting from here...
  22. #[derive(Debug)]
  23. enum MyErr {
  24.     InvalidPair,
  25.     NonEmpty,
  26.     LocationRepeat,
  27.     TimeRepeat,
  28. }
  29.  
  30. impl fmt::Display for MyErr {
  31.     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
  32.         match *self {
  33.             MyErr::InvalidPair => write!(f, "Invalid input pair"),
  34.             MyErr::NonEmpty => write!(f, "Target nonempty"),
  35.             MyErr::LocationRepeat => write!(f, "Element from input repeated in location"),
  36.             MyErr::TimeRepeat => write!(f, "Element from input repeated in time"),
  37.         }
  38.     }
  39. }
  40.  
  41. impl Error for MyErr {
  42.     fn description(&self) -> &str {
  43.         "I don't know what is this function"
  44.     }
  45. }
  46. //...to here
  47.  
  48. //helper function for Board::put_try()
  49. fn has_repeated(pair1: &Pair, pair2: &Pair) -> bool {
  50.     return pair1.0 == pair2.0 || pair1.1 == pair2.0 || pair1.0 == pair2.1 || pair1.1 == pair2.1;
  51. }
  52.  
  53. //timetable of where meetings take place
  54. //row = time
  55. //column = place
  56. #[derive(Clone, Debug)]
  57. struct Board {
  58.     data: Vec<Vec<Pair>>,
  59. }
  60.  
  61. impl Board {
  62.     fn new() -> Self {
  63.         Board {
  64.             data: vec![vec![(0, 0); N as usize - 1]; N as usize - 1],
  65.         }
  66.     }
  67.  
  68.     fn put_raw(&mut self, pair: &Pair, time: usize, place: usize) {
  69.         self.data[time][place] = pair.clone();
  70.     }
  71.  
  72.     //check if we can put the meeting at slot
  73.     fn put_try(&mut self, pair: &Pair, time: usize, place: usize) -> Result<(), MyErr> {
  74.         //check members of the meeting actually exist
  75.         //and it's not someone meeting with themselves
  76.         if !(pair.0 > 0 && pair.0 <= N) || !(pair.1 > 0 && pair.1 <= N) || pair.0 == pair.1 {
  77.             return Err(MyErr::InvalidPair);
  78.         }
  79.         //check slot is empty
  80.         if self.data[time][place] != (0, 0) {
  81.             return Err(MyErr::NonEmpty);
  82.         }
  83.         for i in 0..(N - 1) as usize {
  84.             //apparently nobody can use the same room twice
  85.             if has_repeated(&self.data[i][place], pair) {
  86.                 return Err(MyErr::LocationRepeat);
  87.             }
  88.             //check there are no time travellers
  89.             if has_repeated(&self.data[time][i], pair) {
  90.                 return Err(MyErr::TimeRepeat);
  91.             }
  92.         }
  93.         Ok(())
  94.     }
  95.  
  96.     //splited this function into the above two
  97.     //put_try() and put_raw()
  98.     #[allow(dead_code)]
  99.     fn put(&mut self, pair: &Pair, time: usize, place: usize) -> Result<(), MyErr> {
  100.         match self.put_try(pair, time, place) {
  101.             Err(x) => return Err(x),
  102.             Ok(()) => self.put_raw(pair, time, place),
  103.         }
  104.         Ok(())
  105.     }
  106.  
  107.     //could've use this instead of cloning the board every time
  108.     //to save memory / perfomance
  109.     #[allow(dead_code)]
  110.     fn remove(&mut self, time: usize, place: usize) -> Pair {
  111.         let tmp = self.data[time][place];
  112.         self.data[time][place] = (0, 0);
  113.         tmp
  114.     }
  115. }
  116.  
  117. //core brute force logic
  118. fn dynamic_brute(pairs: &Vec<Pair>, board: &mut Board) {
  119.     //if we placed all meetings we're done!
  120.     if pairs.len() == 0 {
  121.         //here's the solution, hurrah!
  122.         println!("{:?}", board);
  123.         return;
  124.     }
  125.     //looping over all slots
  126.     for time in 0..(N - 1) as usize {
  127.         for place in 0..(N - 1) as usize {
  128.             //see if we can put the meeting in the slot
  129.             match board.put_try(&pairs[0], time, place) {
  130.                 //do nothing and try next slot
  131.                 Err(_) => {}
  132.                 //put meeting in that slot
  133.                 //and see where can we put the next meeting and so on
  134.                 Ok(()) => {
  135.                     let mut pairs_clone = pairs.clone();
  136.                     let mut board_clone = board.clone();
  137.                     board_clone.put_raw(&pairs_clone.remove(0), time, place);
  138.                     dynamic_brute(&pairs_clone, &mut board_clone);
  139.                 }
  140.             }
  141.         }
  142.     }
  143. }
  144.  
  145. fn initialize() -> (Vec<Pair>, Board) {
  146.     let mut pairs = generate_pairs();
  147.     let mut board = Board::new();
  148.     //fix all meetings of group 1 (doesn't matter by symmetry)
  149.     for i in 0..(N - 1) as usize {
  150.         board.put_raw(&pairs.remove(0), i, i);
  151.     }
  152.     (pairs, board)
  153. }
  154.  
  155. fn main() {
  156.     let (pairs, mut board) = initialize();
  157.     //just checking setup is working
  158.     println!("{:?}\n{:?}", pairs, board);
  159.     dynamic_brute(&pairs, &mut board);
  160. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement