day05 part 1 (2 WIP)

This commit is contained in:
Johannes
2018-12-05 08:44:50 +01:00
parent baacb780ba
commit 6e92d937a9
4 changed files with 55 additions and 2 deletions

1
input/day05.txt Normal file
View File

@@ -0,0 +1 @@
dabAcCaCBAcCcaDA

View File

@@ -1,4 +1,4 @@
fn main() { fn main() {
// aoc_2018::tasks::day04::task1(); // aoc_2018::tasks::day05::task1();
aoc_2018::tasks::day04::task2(); aoc_2018::tasks::day05::task2();
} }

51
src/tasks/day05.rs Normal file
View File

@@ -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
}

View File

@@ -2,3 +2,4 @@ pub mod day01;
pub mod day02; pub mod day02;
pub mod day03; pub mod day03;
pub mod day04; pub mod day04;
pub mod day05;