summary refs log tree commit diff homepage
path: root/src
diff options
context:
space:
mode:
authorJune McEnroe <programble@gmail.com>2016-12-21 19:41:41 -0500
committerJune McEnroe <programble@gmail.com>2016-12-21 19:41:41 -0500
commit82af53cb8355aa97b0dccf65f6a0b75bc6ce0f13 (patch)
treed09d4f863de9eaf416e0c8ad488be552d86f560b /src
parentFix day 20 part 2 test (diff)
downloadaoc-82af53cb8355aa97b0dccf65f6a0b75bc6ce0f13.tar.gz
aoc-82af53cb8355aa97b0dccf65f6a0b75bc6ce0f13.zip
Rewrite day 19 with VecDeque
Diffstat (limited to '')
-rw-r--r--src/bin/day19.rs26
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() {