mirror of
https://github.com/wezm/advent-of-code.git
synced 2024-12-18 10:19:55 +00:00
Add day 6 part 2 solution
This commit is contained in:
parent
8beb899b36
commit
37bef8869f
1 changed files with 14 additions and 10 deletions
|
@ -1,7 +1,7 @@
|
||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
use std::io::{Read};
|
use std::io::{Read};
|
||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
use std::collections::HashSet;
|
use std::collections::HashMap;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let mut buffer = String::new();
|
let mut buffer = String::new();
|
||||||
|
@ -13,7 +13,8 @@ fn main() {
|
||||||
.map(|digit| i32::from_str(&digit).unwrap())
|
.map(|digit| i32::from_str(&digit).unwrap())
|
||||||
.collect::<Vec<_>>();
|
.collect::<Vec<_>>();
|
||||||
|
|
||||||
println!("{}", redistribution_cycles(&mut banks));
|
let result = redistribution_cycles(&mut banks);
|
||||||
|
println!("cycles = {}\nsize of loop = {}", result.0, result.1);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn redistribute(banks: &mut Vec<i32>) {
|
fn redistribute(banks: &mut Vec<i32>) {
|
||||||
|
@ -30,22 +31,25 @@ fn redistribute(banks: &mut Vec<i32>) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn redistribution_cycles(banks: &mut Vec<i32>) -> usize {
|
fn redistribution_cycles(banks: &mut Vec<i32>) -> (usize, usize) {
|
||||||
let mut seen_states = HashSet::new();
|
let mut seen_states = HashMap::new();
|
||||||
seen_states.insert(banks.clone());
|
let mut loop_count: usize = 0;
|
||||||
|
seen_states.insert(banks.clone(), loop_count);
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
redistribute(banks);
|
redistribute(banks);
|
||||||
if seen_states.insert(banks.clone()) == false {
|
loop_count += 1;
|
||||||
break;
|
if let Some(&seen_at_loop) = seen_states.get(banks) {
|
||||||
|
return (seen_states.len(), loop_count - seen_at_loop);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
seen_states.insert(banks.clone(), loop_count);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
seen_states.len()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_example() {
|
fn test_example() {
|
||||||
let mut banks = vec![0, 2, 7, 0];
|
let mut banks = vec![0, 2, 7, 0];
|
||||||
assert_eq!(redistribution_cycles(&mut banks), 5);
|
assert_eq!(redistribution_cycles(&mut banks), (5, 4));
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue