Skip to content

Instantly share code, notes, and snippets.

@hellow554
Created December 12, 2018 13:47
Show Gist options
  • Save hellow554/f8c84da7cb32b029169c54c679551b3e to your computer and use it in GitHub Desktop.
Save hellow554/f8c84da7cb32b029169c54c679551b3e to your computer and use it in GitHub Desktop.
use std::collections::VecDeque;
fn solve_a(players: u32, last_marble: u32) -> u32 {
let mut score = vec![0; players as usize];
let mut playfield = VecDeque::new();
let mut cur_pos = 1usize;
playfield.push_back(0);
playfield.push_back(1);
for i in 2..=last_marble {
let player = ((i-1) % players) as usize;
if i % 23 == 0 {
let rem_pos = match cur_pos.wrapping_sub(7) {
0 => 0,
e if e % playfield.len() == 0 => playfield.len(),
e if e > playfield.len() => (cur_pos + playfield.len()) - 7,
e => e,
};
score[player] += i + playfield.remove(rem_pos).unwrap();
cur_pos = rem_pos;
} else {
let next_pos = match cur_pos + 2 {
e if e > playfield.len() => e % playfield.len(),
e if e == playfield.len() => e,
e => e,
};
playfield.insert(next_pos, i);
cur_pos = next_pos;
}
// print!("[{}]", player+1);
// for (i, p) in playfield.iter().enumerate() {
// if i == cur_pos {
// print!("\x1b[7m{:2}\x1b[0m ", p);
// } else {
// print!("{:2} ", p);
// }
// }
// println!()
}
score.into_iter().max().unwrap()
}
fn main() {
let players = 455;
let last_marble_points = 71223;
println!("A: {}", solve_a(players, last_marble_points));
println!("B: {}", solve_a(players, last_marble_points*100));
}
#[test]
fn test_a() {
assert_eq!(32, solve_a(9, 25));
assert_eq!(8317, solve_a(10, 1618));
assert_eq!(146373, solve_a(13, 7999));
assert_eq!(2764, solve_a(17, 1104));
assert_eq!(54718, solve_a(21, 6111));
assert_eq!(37305, solve_a(30, 5807));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment