fn go(cups: &mut Vec<u32>, mut cur: u32, rounds: usize) {
  assert!(cups.len() >= 5);
  let min_cup = to_u32(cups.iter().position(|&c| c != 0).unwrap());
  let max_cup = to_u32(cups.len() - 1);
  for _ in 0..rounds {
    let pick_up: Vec<_> = (0..3)
      .map(|_| {
        let to_rm = cups[to_usize(cur)];
        let next = cups[to_usize(to_rm)];
        cups[to_usize(cur)] = next;
        to_rm
      })
      .collect();
    let mut dest = cur - 1;
    loop {
      if dest < min_cup {
        dest = max_cup;
      }
      if !pick_up.contains(&dest) {
        break;
      }
      dest -= 1;
    }
    let dest = to_usize(dest);
    for n in pick_up.into_iter().rev() {
      let next = cups[dest];
      cups[dest] = n;
      cups[to_usize(n)] = next;
    }
    cur = cups[to_usize(cur)];
  }
}
// take advantage of the fact that there are no duplicate elements to avoid both
// indexing logic and linked lists, and just map each element to its successor.
// but use a Vec as the map with vec[0] = 0 since we also know there won't be
// a whole lot of wasted space.
take advantage of the fact that there are no duplicate elements to avoid both
indexing logic and linked lists, and just map each element to its successor.
but use a Vec as the map with vec[0] = 0 since we also know there won't be
a whole lot of wasted space.
take advantage of the fact that there are no duplicate elements to avoid both indexing logic and linked lists and just map each element to its successor . but use a Vec as the map with vec [ 0 ] = 0 since we also know there won ' t be a whole lot of wasted space .
fn go(cups: &mut Vec<u32>, mut cur: u32, rounds: usize) {
  assert!(cups.len() >= 5);
  let min_cup = to_u32(cups.iter().position(|&c| c != 0).unwrap());
  let max_cup = to_u32(cups.len() - 1);
  for _ in 0..rounds {
    let pick_up: Vec<_> = (0..3)
      .map(|_| {
        let to_rm = cups[to_usize(cur)];
        let next = cups[to_usize(to_rm)];
        cups[to_usize(cur)] = next;
        to_rm
      })
      .collect();
    let mut dest = cur - 1;
    loop {
      if dest < min_cup {
        dest = max_cup;
      }
      if !pick_up.contains(&dest) {
        break;
      }
      dest -= 1;
    }
    let dest = to_usize(dest);
    for n in pick_up.into_iter().rev() {
      let next = cups[dest];
      cups[dest] = n;
      cups[to_usize(n)] = next;
    }
    cur = cups[to_usize(cur)];
  }
}
fn go ( cups : & mut Vec < u32 > , mut cur : u32 , rounds : usize ) { assert ! ( cups len ( ) 5 ) ; let min_cup = to_u32 ( cups . iter ( ) . position ( | & c | c != 0 ) . unwrap ( ) ) ; let max_cup = to_u32 ( cups . len ( ) - 1 ) ; for _ in 0 .. rounds { let pick_up : Vec < _ > = ( 0 .. 3 ) . map ( | _ | { let to_rm = cups [ to_usize ( cur ) ] ; let next = cups [ to_usize ( to_rm ) ] ; cups [ to_usize ( cur ) ] = next ; to_rm } ) . collect ( ) ; let mut dest = cur - 1 ; loop { if dest < min_cup { dest = max_cup ; } if ! pick_up . contains ( & dest ) { break ; } dest -= 1 ; } let dest = to_usize ( dest ) ; for n in pick_up . into_iter ( ) . rev ( ) { let next = cups [ dest ] ; cups [ dest ] = n ; cups [ to_usize ( n ) ] = next ; } cur = cups [ to_usize ( cur ) ] ; } }
take advantage of the fact that there are no duplicate elements to avoid both
indexing logic and linked lists, and just map each element to its successor.
take advantage of the fact that there are no duplicate elements to avoid both indexing logic and linked lists and just map each element to its successor .
cups
&mut Vec<u32>
cur
u32
rounds
usize
cups
&mut Vec<u32>
cur
u32
rounds
usize