From c7545c2a132dabdbff4177f4b45570908515c57d Mon Sep 17 00:00:00 2001 From: June McEnroe Date: Wed, 21 Dec 2016 19:41:41 -0500 Subject: Rewrite day 19 with VecDeque --- src/bin/day19.rs | 26 ++++++++++++-------------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/src/bin/day19.rs b/src/bin/day19.rs index 89013b3..9afbf39 100644 --- a/src/bin/day19.rs +++ b/src/bin/day19.rs @@ -1,27 +1,25 @@ +use std::collections::VecDeque; use std::io::{self, Read}; #[derive(Clone, Copy)] struct Elf { + position: usize, gifts: usize, - next: usize, } fn solve(count: usize) -> usize { - let mut circle: Vec = (0..count).map(|i| Elf { gifts: 1, next: i + 1 }).collect(); - circle.last_mut().unwrap().next = 0; - - let mut index = 0; - - while circle[index].gifts < count { - let next = circle[index].next; - circle[index].gifts += circle[next].gifts; - circle[next].gifts = 0; - circle[index].next = circle[next].next; - - index = circle[index].next; + let mut circle: VecDeque = (0..count) + .map(|i| Elf { position: i + 1, gifts: 1 }) + .collect(); + + while circle.len() > 1 { + let mut current = circle.pop_front().unwrap(); + let next = circle.pop_front().unwrap(); + current.gifts += next.gifts; + circle.push_back(current); } - index + 1 + circle.pop_front().unwrap().position } fn main() { -- cgit 1.4.1