use std::time::Instant; use utils; pub fn task1() { let mut input = utils::read_file("input/day05.txt"); let start = Instant::now(); input = reduce(input.as_str()); println!("Duration: {:?}", Instant::now() - start); println!("RESULT: {}", input.len()); } pub fn task2() { let input = utils::read_file("input/day05.txt"); let start = Instant::now(); let input = reduce(&input); let best = "abcdefghijklmnopqrstuvwxyz" .chars() .map(|c| { ( c, reduce( &input .chars() .filter(|ch| ch.to_lowercase().next().unwrap() != c) .collect::(), ).len(), ) }).min_by_key(|it| it.1) .unwrap(); println!("Duration: {:?}", Instant::now() - start); println!("Best: {} (length {})", best.0, best.1); } fn reduce(input: &str) -> String { input .chars() .fold(Vec::::new(), |mut stack, c| { let last: Option; { if let Some(c) = stack.last() { last = Some(*c) } else { last = None } } if let Some(last) = last { if c.eq_ignore_ascii_case(&last) && c != last { stack.pop(); } else { stack.push(c); } } else { stack.push(c); } stack }).iter() .collect() }