diff options
author | June McEnroe <june@causal.agency> | 2016-12-21 19:41:41 -0500 |
---|---|---|
committer | June McEnroe <june@causal.agency> | 2020-11-22 00:14:25 -0500 |
commit | c7545c2a132dabdbff4177f4b45570908515c57d (patch) | |
tree | 6ccbb36cbe6a006a79da8591ea951c1ff0ae81f3 | |
parent | Fix day 20 part 2 test (diff) | |
download | aoc-c7545c2a132dabdbff4177f4b45570908515c57d.tar.gz aoc-c7545c2a132dabdbff4177f4b45570908515c57d.zip |
Rewrite day 19 with VecDeque
-rw-r--r-- | src/bin/day19.rs | 26 |
1 files 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<Elf> = (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<Elf> = (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() { |