62 lines
1.6 KiB
Rust
62 lines
1.6 KiB
Rust
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::<String>(),
|
|
).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::<char>::new(), |mut stack, c| {
|
|
let last: Option<char>;
|
|
{
|
|
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()
|
|
}
|