Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use std::error::Error;
- use std::fmt;
- const N: u8 = 6;
- //who's meeting who
- type Pair = (u8, u8);
- //generate all meetings needed
- fn generate_pairs() -> Vec<Pair> {
- let mut result = Vec::new();
- for x in 1..N {
- for y in x + 1..N + 1 {
- result.push((x, y));
- }
- }
- result
- }
- //ended up not using any of these this
- //starting from here...
- #[derive(Debug)]
- enum MyErr {
- InvalidPair,
- NonEmpty,
- LocationRepeat,
- TimeRepeat,
- }
- impl fmt::Display for MyErr {
- fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
- match *self {
- MyErr::InvalidPair => write!(f, "Invalid input pair"),
- MyErr::NonEmpty => write!(f, "Target nonempty"),
- MyErr::LocationRepeat => write!(f, "Element from input repeated in location"),
- MyErr::TimeRepeat => write!(f, "Element from input repeated in time"),
- }
- }
- }
- impl Error for MyErr {
- fn description(&self) -> &str {
- "I don't know what is this function"
- }
- }
- //...to here
- //helper function for Board::put_try()
- fn has_repeated(pair1: &Pair, pair2: &Pair) -> bool {
- return pair1.0 == pair2.0 || pair1.1 == pair2.0 || pair1.0 == pair2.1 || pair1.1 == pair2.1;
- }
- //timetable of where meetings take place
- //row = time
- //column = place
- #[derive(Clone, Debug)]
- struct Board {
- data: Vec<Vec<Pair>>,
- }
- impl Board {
- fn new() -> Self {
- Board {
- data: vec![vec![(0, 0); N as usize - 1]; N as usize - 1],
- }
- }
- fn put_raw(&mut self, pair: &Pair, time: usize, place: usize) {
- self.data[time][place] = pair.clone();
- }
- //check if we can put the meeting at slot
- fn put_try(&mut self, pair: &Pair, time: usize, place: usize) -> Result<(), MyErr> {
- //check members of the meeting actually exist
- //and it's not someone meeting with themselves
- if !(pair.0 > 0 && pair.0 <= N) || !(pair.1 > 0 && pair.1 <= N) || pair.0 == pair.1 {
- return Err(MyErr::InvalidPair);
- }
- //check slot is empty
- if self.data[time][place] != (0, 0) {
- return Err(MyErr::NonEmpty);
- }
- for i in 0..(N - 1) as usize {
- //apparently nobody can use the same room twice
- if has_repeated(&self.data[i][place], pair) {
- return Err(MyErr::LocationRepeat);
- }
- //check there are no time travellers
- if has_repeated(&self.data[time][i], pair) {
- return Err(MyErr::TimeRepeat);
- }
- }
- Ok(())
- }
- //splited this function into the above two
- //put_try() and put_raw()
- #[allow(dead_code)]
- fn put(&mut self, pair: &Pair, time: usize, place: usize) -> Result<(), MyErr> {
- match self.put_try(pair, time, place) {
- Err(x) => return Err(x),
- Ok(()) => self.put_raw(pair, time, place),
- }
- Ok(())
- }
- //could've use this instead of cloning the board every time
- //to save memory / perfomance
- #[allow(dead_code)]
- fn remove(&mut self, time: usize, place: usize) -> Pair {
- let tmp = self.data[time][place];
- self.data[time][place] = (0, 0);
- tmp
- }
- }
- //core brute force logic
- fn dynamic_brute(pairs: &Vec<Pair>, board: &mut Board) {
- //if we placed all meetings we're done!
- if pairs.len() == 0 {
- //here's the solution, hurrah!
- println!("{:?}", board);
- return;
- }
- //looping over all slots
- for time in 0..(N - 1) as usize {
- for place in 0..(N - 1) as usize {
- //see if we can put the meeting in the slot
- match board.put_try(&pairs[0], time, place) {
- //do nothing and try next slot
- Err(_) => {}
- //put meeting in that slot
- //and see where can we put the next meeting and so on
- Ok(()) => {
- let mut pairs_clone = pairs.clone();
- let mut board_clone = board.clone();
- board_clone.put_raw(&pairs_clone.remove(0), time, place);
- dynamic_brute(&pairs_clone, &mut board_clone);
- }
- }
- }
- }
- }
- fn initialize() -> (Vec<Pair>, Board) {
- let mut pairs = generate_pairs();
- let mut board = Board::new();
- //fix all meetings of group 1 (doesn't matter by symmetry)
- for i in 0..(N - 1) as usize {
- board.put_raw(&pairs.remove(0), i, i);
- }
- (pairs, board)
- }
- fn main() {
- let (pairs, mut board) = initialize();
- //just checking setup is working
- println!("{:?}\n{:?}", pairs, board);
- dynamic_brute(&pairs, &mut board);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement