diff options
Diffstat (limited to '2018/src/bin/day_5.rs')
-rw-r--r-- | 2018/src/bin/day_5.rs | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/2018/src/bin/day_5.rs b/2018/src/bin/day_5.rs new file mode 100644 index 0000000..2b24d4c --- /dev/null +++ b/2018/src/bin/day_5.rs @@ -0,0 +1,53 @@ +extern crate advent_of_code_2018; +use advent_of_code_2018::*; + +use std::error::Error; +use std::path::PathBuf; +use std::cmp; + +// cargo watch -cs "cargo run --release --bin day_5" + +fn main() -> Result<(), Box<Error>> { + let input = read_file(&PathBuf::from("inputs/5.txt"))?; + + //println!("Input: {:?}", input); + let polymer = { + let mut polymer: Vec<char> = input[0].chars().collect(); + reduce(&mut polymer); + polymer + }; + + debug!(polymer.len()); + + + let mut min_length = polymer.len(); + for c in "abcdefghijklmnopqrstuvwxyz".chars() { + let mut polymer_without_char = polymer.clone(); + polymer_without_char.retain(|x| x.to_ascii_lowercase() != c); + reduce(&mut polymer_without_char); + min_length = cmp::min(min_length, polymer_without_char.len()); + } + + debug!(min_length); + + Ok(()) +} + + +fn reduce(polymer: &mut Vec<char>) { + let mut had_reductions = true; + while had_reductions { + had_reductions = false; + + for i in 0..polymer.len()-1 { + if polymer[i].to_ascii_lowercase() == polymer[i+1].to_ascii_lowercase() && polymer[i] != polymer[i+1] { + had_reductions = true; + polymer.remove(i+1); + polymer.remove(i); + + // break isn't efficient, but does prevent indexing issues after removing + break; + } + } + } +} |