use std::collections::HashSet; fn next_permutation(lists: &mut Vec) -> bool { let mut n = lists.len(); let mut i: usize = n - 1; while i >= 1 && lists[i - 1] > lists[i] { i -= 1; } if i == 0 { return false; } let mut j: usize = i; while j + 1 < n && lists[i - 1] < lists[j + 1] { j += 1; } lists.swap(i-1, j); n -= 1; while n > i { lists.swap(i, n); i += 1; n -= 1; } true } fn main() { let mut asks: Vec = vec![0,1,2,3]; let mut fixed: HashSet = HashSet::new(); println!("{} {} {} {}", asks[0], asks[1], asks[2], asks[3]); let mut xy = String::new(); std::io::stdin().read_line(&mut xy).ok(); let mut prev: usize = xy.trim().split_whitespace().map(|s| s.parse::().unwrap()).sum(); let mut rank: usize = 3; let mut unused: HashSet = HashSet::new(); (4..9).for_each(|i| { unused.insert(i); }); let mut stacked: Vec = vec![]; loop { println!("{} {} {} {}", asks[0], asks[1], asks[2], asks[3]); let mut xy = String::new(); std::io::stdin().read_line(&mut xy).ok(); let xy: Vec = xy.trim().split_whitespace().map(|s| s.parse().unwrap()).collect(); let x = xy[0]; let y = xy[1]; if x == 4 && y == 0 { return; } if x + y == 4 { break; } if x + y != prev { if x + y > prev { fixed.insert(asks[rank]); } else { match rank { 3 => { fixed.insert(asks[3] - 1); stacked.iter().for_each(|i| { fixed.insert(*i); }); }, _ => { fixed.insert(rank); asks[rank] = rank; }, } } stacked = vec![]; if rank == 3 { prev = x + y; } } else { if rank == 3 && asks[3] > 3 { stacked.push(asks[3] - 1); } } if fixed.len() == 4 { asks = fixed.iter().map(|i| *i).collect(); break; } if rank == 3 { if asks[3] < 9 { asks[3] += 1; } else { rank -= 1; asks[rank] = *unused.difference(&fixed).max().unwrap(); } } else { asks[rank] = rank; rank -= 1; asks[rank] = *unused.difference(&fixed).max().unwrap(); } } asks.sort(); loop { println!("{} {} {} {}", asks[0], asks[1], asks[2], asks[3]); let mut xy = String::new(); std::io::stdin().read_line(&mut xy).ok(); let xy: Vec = xy.trim().split_whitespace().map(|s| s.parse().unwrap()).collect(); let x = xy[0]; let y = xy[1]; if x == 4 && y == 0 { return; } next_permutation(&mut asks); } }