mirror of
https://github.com/bertptrs/adventofcode.git
synced 2025-12-25 12:50:32 +01:00
Improve day 22 with Brent's cycle finding
This commit is contained in:
@@ -1,4 +1,3 @@
|
||||
use std::collections::HashSet;
|
||||
use std::collections::VecDeque;
|
||||
use std::io::Read;
|
||||
|
||||
@@ -26,13 +25,16 @@ fn read_input(input: &mut dyn Read) -> (Deck, Deck) {
|
||||
}
|
||||
|
||||
fn play_recursive_game(mut p1: Deck, mut p2: Deck) -> (bool, Deck) {
|
||||
let mut seen = HashSet::new();
|
||||
let mut c1 = p1.clone();
|
||||
let mut c2 = p2.clone();
|
||||
let mut iter = 0u32;
|
||||
|
||||
while !p1.is_empty() && !p2.is_empty() {
|
||||
if !seen.insert((p1.clone(), p2.clone())) {
|
||||
return (true, p1);
|
||||
if iter.count_ones() == 1 {
|
||||
// Power of two
|
||||
c1 = p1.clone();
|
||||
c2 = p2.clone();
|
||||
}
|
||||
|
||||
let v1 = p1.pop_front().unwrap();
|
||||
let v2 = p2.pop_front().unwrap();
|
||||
|
||||
@@ -52,6 +54,12 @@ fn play_recursive_game(mut p1: Deck, mut p2: Deck) -> (bool, Deck) {
|
||||
p2.push_back(v2);
|
||||
p2.push_back(v1);
|
||||
}
|
||||
|
||||
if p1 == c1 && p2 == c2 {
|
||||
return (true, p1);
|
||||
}
|
||||
|
||||
iter += 1;
|
||||
}
|
||||
|
||||
if p1.is_empty() {
|
||||
|
||||
@@ -73,9 +73,11 @@ impl Solution for Day23 {
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
const SAMPLE: &[u8] = b"389125467";
|
||||
|
||||
#[test]
|
||||
fn sample_part1() {
|
||||
let sample = [3, 8, 9, 1, 2, 5, 4, 6, 7];
|
||||
let sample = read_input(&mut SAMPLE.as_ref());
|
||||
assert_eq!("92658374", &play_game(&mut sample.clone(), 10));
|
||||
assert_eq!("67384529", &play_game(&mut sample.clone(), 100));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user