summary refs log tree commit diff homepage
path: root/src/bin
diff options
context:
space:
mode:
authorJune McEnroe <june@causal.agency>2016-12-21 19:41:41 -0500
committerJune McEnroe <june@causal.agency>2020-11-22 00:14:25 -0500
commitc7545c2a132dabdbff4177f4b45570908515c57d (patch)
tree6ccbb36cbe6a006a79da8591ea951c1ff0ae81f3 /src/bin
parentFix day 20 part 2 test (diff)
downloadaoc-c7545c2a132dabdbff4177f4b45570908515c57d.tar.gz
aoc-c7545c2a132dabdbff4177f4b45570908515c57d.zip
Rewrite day 19 with VecDeque
Diffstat (limited to 'src/bin')
-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() {
546964f6b255dccddcd93&follow=1'>Assert return values in edit testsJune McEnroe 2022-02-20Move mbs out of struct Edit, use a global bufferJune McEnroe 2022-02-20Clear edit buffer before running commandJune McEnroe 2022-02-20Show indicator in status when window has pending inputJune McEnroe 2022-02-20Use separate edit buffers for each IDJune McEnroe 2022-02-20Make sure new cap is actually larger than new lengthJune McEnroe 2022-02-20Remove unused mbs.len field from struct EditJune McEnroe 2022-02-19Remove unneeded includes in ui.cJune McEnroe 2022-02-19Reimplement tab completeJune McEnroe 2022-02-19Handle errors from editFn, etc.June McEnroe 2022-02-19Reimplement text macrosJune McEnroe 2022-02-19Factor out input handling to input.cJune McEnroe 2022-02-19Factor out window management to window.cJune McEnroe 2022-02-19Enable -Wmissing-prototypesJune McEnroe 2022-02-19Fix edit.[ch] license notice additional permissionsJune McEnroe 2022-02-19Run line editing testsJune McEnroe 2022-02-18Implement new line editing "library"June McEnroe 2022-02-18Simplify cursor positioning in inputJune McEnroe 2022-02-18Fix M-f orderingJune McEnroe 2022-02-12Move sandman build to scripts/MakefileJune McEnroe 2022-02-12Use compat_readpassphrase.c on LinuxJune McEnroe 2022-02-12Copy RPP defines from oconfigureJune McEnroe