Advertisement
Guest User

Untitled

a guest
Dec 19th, 2016
367
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 1.04 KB | None | 0 0
  1. extern crate itertools;
  2.  
  3. use itertools::Itertools;
  4. use std::collections::VecDeque;
  5.  
  6. fn p1(n: usize) -> usize {
  7.     let mut v: VecDeque<usize> = (1..n + 1).collect();
  8.     while v.len() > 1 {
  9.         let front = v.pop_front().unwrap();
  10.         v.push_back(front);
  11.         v.pop_front();
  12.     }
  13.     v[0]
  14. }
  15.  
  16. fn step_p2(v: Vec<usize>) -> Vec<usize> {
  17.     let mut r = Vec::with_capacity(v.len() * 8 / 9);
  18.     let first_high = (v.len() + 2) / 3;
  19.     let after_last_high = v.len() / 2;
  20.     let first_inc = 2 - v.len() % 2;
  21.     for &e in v[first_high..after_last_high].iter() {
  22.         r.push(e);
  23.     }
  24.     for i in (after_last_high + first_inc..v.len()).step(3) {
  25.         r.push(v[i]);
  26.     }
  27.     for &e in v[..first_high].iter() {
  28.         r.push(e);
  29.     }
  30.     r
  31. }
  32.  
  33. fn p2(n: usize) -> usize {
  34.     let mut v: Vec<usize> = (1..(n + 1)).collect();
  35.     while v.len() > 1 {
  36.         v = step_p2(v);
  37.     }
  38.     v[0]
  39. }
  40.  
  41. fn main() {
  42.     const INPUT: usize = 3012210;
  43.     println!("P1 = {}", p1(INPUT));
  44.     println!("P2 = {}", p2(INPUT));
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement