Compare commits

...

2 Commits

Author SHA1 Message Date
Johannes Schaefer
d10d36498c day05 performance push part 2 through more efficient case insensitive comparison 2018-12-05 15:19:12 +01:00
Johannes Schaefer
32843531f8 take times 2018-12-05 15:15:18 +01:00
5 changed files with 27 additions and 10 deletions

16
Cargo.lock generated
View File

@@ -11,6 +11,7 @@ name = "aoc_2018"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"chrono 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", "chrono 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)",
"itertools 0.7.11 (registry+https://github.com/rust-lang/crates.io-index)",
"regex 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)", "regex 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
] ]
@@ -29,6 +30,19 @@ dependencies = [
"time 0.1.40 (registry+https://github.com/rust-lang/crates.io-index)", "time 0.1.40 (registry+https://github.com/rust-lang/crates.io-index)",
] ]
[[package]]
name = "either"
version = "1.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "itertools"
version = "0.7.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"either 1.5.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]] [[package]]
name = "lazy_static" name = "lazy_static"
version = "1.2.0" version = "1.2.0"
@@ -143,6 +157,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
"checksum aho-corasick 0.6.9 (registry+https://github.com/rust-lang/crates.io-index)" = "1e9a933f4e58658d7b12defcf96dc5c720f20832deebe3e0a19efd3b6aaeeb9e" "checksum aho-corasick 0.6.9 (registry+https://github.com/rust-lang/crates.io-index)" = "1e9a933f4e58658d7b12defcf96dc5c720f20832deebe3e0a19efd3b6aaeeb9e"
"checksum cfg-if 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "082bb9b28e00d3c9d39cc03e64ce4cea0f1bb9b3fde493f0cbc008472d22bdf4" "checksum cfg-if 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "082bb9b28e00d3c9d39cc03e64ce4cea0f1bb9b3fde493f0cbc008472d22bdf4"
"checksum chrono 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)" = "45912881121cb26fad7c38c17ba7daa18764771836b34fab7d3fbd93ed633878" "checksum chrono 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)" = "45912881121cb26fad7c38c17ba7daa18764771836b34fab7d3fbd93ed633878"
"checksum either 1.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "3be565ca5c557d7f59e7cfcf1844f9e3033650c929c6566f511e8005f205c1d0"
"checksum itertools 0.7.11 (registry+https://github.com/rust-lang/crates.io-index)" = "0d47946d458e94a1b7bcabbf6521ea7c037062c81f534615abcad76e84d4970d"
"checksum lazy_static 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "a374c89b9db55895453a74c1e38861d9deec0b01b405a82516e9d5de4820dea1" "checksum lazy_static 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "a374c89b9db55895453a74c1e38861d9deec0b01b405a82516e9d5de4820dea1"
"checksum libc 0.2.44 (registry+https://github.com/rust-lang/crates.io-index)" = "10923947f84a519a45c8fefb7dd1b3e8c08747993381adee176d7a82b4195311" "checksum libc 0.2.44 (registry+https://github.com/rust-lang/crates.io-index)" = "10923947f84a519a45c8fefb7dd1b3e8c08747993381adee176d7a82b4195311"
"checksum memchr 2.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "0a3eb002f0535929f1199681417029ebea04aadc0c7a4224b46be99c7f5d6a16" "checksum memchr 2.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "0a3eb002f0535929f1199681417029ebea04aadc0c7a4224b46be99c7f5d6a16"

View File

@@ -6,3 +6,4 @@ authors = ["Johannes <jschaef@mail.uni-paderborn.de>"]
[dependencies] [dependencies]
regex = "1.1.0" regex = "1.1.0"
chrono = "0.4.6" chrono = "0.4.6"
itertools = "0.7.11"

View File

@@ -1,4 +1,5 @@
extern crate chrono; extern crate chrono;
extern crate itertools;
extern crate regex; extern crate regex;
pub mod tasks; pub mod tasks;

View File

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

View File

@@ -1,15 +1,19 @@
use std::time::Instant;
use utils; use utils;
pub fn task1() { pub fn task1() {
let mut input = utils::read_file("input/day05.txt"); let mut input = utils::read_file("input/day05.txt");
let start = Instant::now();
input = reduce(input.as_str()); input = reduce(input.as_str());
println!("resulting polymer: {}", input); println!("Duration: {:?}", Instant::now() - start);
println!("RESULT: {}", input.len()); println!("RESULT: {}", input.len());
} }
pub fn task2() { pub fn task2() {
let input = utils::read_file("input/day05.txt"); let input = utils::read_file("input/day05.txt");
let start = Instant::now();
let input = reduce(&input);
let best = "abcdefghijklmnopqrstuvwxyz" let best = "abcdefghijklmnopqrstuvwxyz"
.chars() .chars()
.map(|c| { .map(|c| {
@@ -18,13 +22,14 @@ pub fn task2() {
reduce( reduce(
&input &input
.chars() .chars()
.filter(|ch| ch.to_lowercase().next().unwrap() != c) .filter(|ch| ch.eq_ignore_ascii_case(&c))
.collect::<String>(), .collect::<String>(),
).len(), ).len(),
) )
}).min_by_key(|it| it.1) }).min_by_key(|it| it.1)
.unwrap(); .unwrap();
println!("Duration: {:?}", Instant::now() - start);
println!("Best: {} (length {})", best.0, best.1); println!("Best: {} (length {})", best.0, best.1);
} }
@@ -42,13 +47,7 @@ fn reduce(input: &str) -> String {
} }
if let Some(last) = last { if let Some(last) = last {
if c.is_lowercase() if c.eq_ignore_ascii_case(&last) && c != last {
&& last.is_uppercase()
&& c == last.to_lowercase().next().unwrap()
|| last.is_lowercase()
&& c.is_uppercase()
&& last == c.to_lowercase().next().unwrap()
{
stack.pop(); stack.pop();
} else { } else {
stack.push(c); stack.push(c);