SHARE
TWEET

Untitled

a guest Dec 9th, 2018 59 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. use std::collections::VecDeque;
  2. use std::collections::HashMap;
  3.  
  4. fn cycle(i: i32, circle: &mut VecDeque<usize>) {
  5.   if i > 0 {
  6.     let v = circle.pop_back().unwrap_or(0);
  7.     circle.push_front(v);
  8.     cycle(i - 1, circle);
  9.   } else if i < 0 {
  10.     let v = circle.pop_front().unwrap_or(0);
  11.     circle.push_back(v);
  12.     cycle(i + 1, circle);
  13.   }
  14. }
  15.  
  16. fn main() {
  17.   let players: usize = 413;
  18.   let last_marble_worth: usize = 7108200;
  19.   let mut circle: VecDeque<usize> = VecDeque::new();
  20.   let mut scores = HashMap::new();
  21.  
  22.   circle.push_back(0);
  23.  
  24.   for marble_value in 1..last_marble_worth+1 {
  25.     if marble_value % 23 != 0 {
  26.       cycle(2, &mut circle);
  27.       circle.push_back(marble_value);
  28.     } else {
  29.       let player = ((marble_value as i32) - 1) % (players as i32);
  30.       let score = scores.entry(player).or_insert(0);
  31.  
  32.       cycle(-7, &mut circle);
  33.       *score += marble_value + circle.pop_back().unwrap_or(0);
  34.     }
  35.   }
  36.  
  37.   println!("{}", scores.values().max().unwrap_or(&0));
  38. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top