diff --git a/input/day05.txt b/input/day05.txt new file mode 100644 index 0000000..410cf15 --- /dev/null +++ b/input/day05.txt @@ -0,0 +1 @@ +dabAcCaCBAcCcaDA \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 0dbbe0c..58844d7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,4 @@ fn main() { - // aoc_2018::tasks::day04::task1(); - aoc_2018::tasks::day04::task2(); + // aoc_2018::tasks::day05::task1(); + aoc_2018::tasks::day05::task2(); } diff --git a/src/tasks/day05.rs b/src/tasks/day05.rs new file mode 100644 index 0000000..bc4116e --- /dev/null +++ b/src/tasks/day05.rs @@ -0,0 +1,51 @@ +use utils; + +pub fn task1() { + let mut input = utils::read_file("input/day05.txt"); + input = reduce(input); + + println!("resulting polymer: {}", input); //11591 too high + println!("RESULT: {}", input.len()); +} + +pub fn task2() { + let input = utils::read_file("input/day05.txt"); + let best = "abcdefghijklmnopqrstuvwxyz" + .chars() + .map(|c| { + ( + c, + reduce( + input + .chars() + .filter(|ch| ch.to_lowercase().next().unwrap() != c) + .collect(), + ), + ) + }).max_by_key(|it| it.1.len()) + .unwrap(); + + println!("Best: {} (length {})", best.0, best.1.len()); // 11184 too high +} + +fn reduce(mut input: String) -> String { + let mut go_on = true; + while go_on { + go_on = false; + let mut index = 0; + while index < input.len() - 1 { + let a = input.chars().nth(index).unwrap(); + let b = input.chars().nth(index + 1).unwrap(); + if a.is_lowercase() && b.is_uppercase() && a == b.to_lowercase().next().unwrap() + || b.is_lowercase() && a.is_uppercase() && b == a.to_lowercase().next().unwrap() + { + // println!("{} and {} match", a, b); + input.remove(index); + input.remove(index); + go_on = true; + } + index += 1; + } + } + input +} diff --git a/src/tasks/mod.rs b/src/tasks/mod.rs index e051fbd..f290640 100644 --- a/src/tasks/mod.rs +++ b/src/tasks/mod.rs @@ -2,3 +2,4 @@ pub mod day01; pub mod day02; pub mod day03; pub mod day04; +pub mod day05;